0

I'm trying to figure out whether there is a way of adding url parameters to the Route::controller call.

What I have at the moment for my control panel is:

Route::group(
    [
        'prefix' => 'admin',
        'namespace' => 'Admin'
    ],
    function() {

        Route::group(
            [
                'prefix' => '',
                'before' => 'auth.admin'
            ],
            function() {

                Route::controller('page', 'PageController');

                Route::controller('article', 'ArticleController');

            }

        );

        Route::controller('/', 'LoginController');

    }
);

Now - each of the controllers will have the post / getEdit actions, which will require the url id parameter to be passed over in the simple format of /admin/page/edit/{id}.

My question is - is there a way to perhaps add some parameters to the Route::controller method or do I have to do them all using Route::get / Route::post approach?

I know I can do it by adding two extra cases with get and post above the given controller call:

Route::group(
    [
        'prefix' => 'admin',
        'namespace' => 'Admin'
    ],
    function() {

        Route::group(
            [
                'prefix' => '',
                'before' => 'auth.admin'
            ],
            function() {

                Route::get('page/edit/{id}', 'PageController@getEdit');
                Route::post('page/edit/{id}', 'PageController@postEdit');

                Route::controller('page', 'PageController');

                Route::controller('article', 'ArticleController');

            }

        );

        Route::controller('/', 'LoginController');

    }
);

but perhaps there's a better approach?


Sebastian Sulinski
  • 5,815
  • 7
  • 39
  • 61

1 Answers1

1

You can use Route::resource:

Route::resource('resource', 'ResourceController');

This will register the following routes:

GET       /resource                 index   resource.index
GET       /resource/create          create  resource.create
POST      /resource                 store   resource.store
GET       /resource/{resource}      show    resource.show
GET       /resource/{resource}/edit edit    resource.edit
PUT/PATCH /resource/{resource}      update  resource.update
DELETE    /resource/{resource}      destroy resource.destroy

You can use it together with only or except to choose what routes to be included (or excluded):

Route::resource('resource', 'ResourceController', ['only' => ['index', 'show', 'update', 'destroy']]);

Read more about restful resource controllers in the Laravel documentation.

This post might also be interesting: Laravel 4 - Route::resource vs Route::controller. Which to use?

Community
  • 1
  • 1
Thomas Jensen
  • 2,138
  • 2
  • 25
  • 48