How can I implement Resource Controllers if I use many “get” on the laravel?

前端 未结 3 1011
攒了一身酷
攒了一身酷 2020-12-02 02:36

I have routes laravel like this :

Route::prefix(\'member\')->middleware(\'auth\')->group(function(){
    Route::prefix(\'purchase\')->group(functio         


        
相关标签:
3条回答
  • 2020-12-02 03:14

    You could try like this, Just put your resource controller custom method up resource route.

    Route::prefix('member')->middleware('auth')->group(function(){
        Route::get('order', 'Member\PurchaseController@order')->name('member.purchase.order');
        Route::get('transaction', 'Member\PurchaseController@transaction')->name('member.purchase.transaction')
        Route::resource('purchase', 'Member\PurchaseController');
    });
    
    0 讨论(0)
  • 2020-12-02 03:14

    For the resource controller, it is pre-defined by the Laravel, which contain only the 7 method.

    Shown at below table.

    So, if you want any other method, you have to definde by youself.

    php artisan route:list
    

    You can use this to check all the route you defined.

    0 讨论(0)
  • 2020-12-02 03:24

    The other answers on here are pretty much correct.

    From my other answer you linked this question in from, here that way based on what MD Iyasin Arafat has suggested, if you are using laravel 5.5+:

    # Group all routes requiring middleware auth, thus declared only once
    
    Route::middleware('auth')->group(function(){
    
        # Suffix rules in group for prefix,namespace & name with "member"
    
        Route::namespace('Member')->prefix('member')->name('member.')->group(function () {
    
                Route::get('purchase/order', 'PurchaseController@order')->name('purchase.order');
                Route::get('purchase/transaction', 'PurchaseController@transaction')->name('purchase.transaction');
                Route::resource('purchase', 'PurchaseController');
    
        });
    
    });
    

    Grouping Methods ( ->group() ) :


    Controller Namespace ( ->namespace('Member') )

    Prepends to 'PurchaseController' to give 'Member\PurchaseController'


    Route Name (->name('member.'))

    Prepends to name('purchase.order') to give route('member.purchase.order')


    URI Request (->prefix('member'))

    Prepends to /purchase to give example.com/member/purchase

    As you can see, using the methods above with group() reduces repetition of prefix declarations.

    Hint

    Custom routes must always be declared before a resource never after!

    Example to use if you have a lot of custom routes for Purchase Controller and how a second controller looks for member group:

    # Group all routes requiring middleware auth, thus declared only once
    
    Route::middleware('auth')->group(function(){
    
        # Suffix rules in group for prefix,namespace & name with "member"
    
        Route::namespace('Member')->prefix('member')->name('member.')->group(function () {
    
                Route::prefix('purchase')->name('purchase.')->group(function() {
    
                    Route::get('order', 'PurchaseController@order')->name('order');
    
                    Route::get('transaction', 'PurchaseController@transaction')->name('transaction');
    
                    Route::get('history', 'PurchaseController@history')->name('history');
    
                    Route::get('returns', 'PurchaseController@returns')->name('returns');
    
                    Route::get('status', 'PurchaseController@status')->name('status');
    
                    Route::resource('/', 'PurchaseController');
    
                });
    
                Route::prefix('account')->name('account.')->group(function() {
    
                    Route::get('notifications', 'AccountController@notifications')->name('notifications');
    
                    Route::resource('/', 'AccountController');
    
                });
    
        });
    
    });
    
    0 讨论(0)
提交回复
热议问题