2

I have routes laravel like this :

Route::prefix('member')->middleware('auth')->group(function(){
    Route::prefix('purchase')->group(function(){
        Route::get('/', 'Member\PurchaseController@index')->name('member.purchase.index');
        Route::get('order', 'Member\PurchaseController@order')->name('member.purchase.order');
        Route::get('transaction', 'Member\PurchaseController@transaction')->name('member.purchase.transaction');
    });
});

My controller like this :

<?php
...
class PurchaseController extends Controller
{
    ...
    public function index()
    {
        ...
    }
    public function order()
    {
        ...
    }
    public function transaction()
    {
        ...
    }
}

I want to change it to Resource Controllers(https://laravel.com/docs/5.6/controllers#resource-controllers)

So I only use 1 routes

From my case, my routes to be like this :

Route::prefix('member')->middleware('auth')->group(function(){
    Route::resource('purchase', 'Member\PurchaseController');
});

If I using resouce controller, I only can data in the index method or show method

How can I get data in order method and transaction method?

3
  • 1
    Route::resource is for pre-defined index, create, store, show, edit, update and delete methods. if you want to use custom methods (like you used here), you need to declare them individually. Commented Apr 1, 2018 at 3:37
  • @Success Man So do you think it is not suitable to use resource controller for my case? Commented Apr 1, 2018 at 3:58
  • 1
    For custom functions, you need to define each route. Commented Apr 1, 2018 at 4:25

3 Answers 3

3

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');
});
Sign up to request clarification or add additional context in comments.

4 Comments

This is the best way. You can override any route resource by adding it above the resource call. In all web.php and api.php it is first come, first serve.
Perhaps you want to explain why this happens? When creating a resource controller for purchase, it will define a route with a catch all param on /purchase/{purchase}, which is the route to view a single purchase by identifier. Since the resource route does not define what type of data this is, it will match every GET request on /purchase/xxxx, including order and transaction if those routes are defined after the resource route.
@MD Iyasin Arafat Why order and transaction are out of group purchase? I want the order and transaction to be in group purchase
@Success Man, Then you just remove 'member' from your naming and then run 'php artisan route:list' to see route group.
2

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

Shown at below table.

Action handled by resource controller

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.

Comments

1

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');

            });

    });

});

3 Comments

Why order and transaction are out of group purchase? I want the order and transaction to be in group purchase
You don't really need to create another nested group unless you are going to have a lot of custom functions for /purchase/*, I have added an example of such further nestings to the bottom of my answer.
I fixed the upper example, I forgot to manually inline prefix the nested gets with purchase/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.