Spencer Mark
Spencer Mark

Reputation: 5311

Laravel routing : nested Route::group

I know there are tickets here, which are talking about the similar scenario, but I can't find any answer that would satisfy my case.

I'm trying to do the following:

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

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


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


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

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


            }
        );


    }
);

When I call /admin I get the LoginController and it's getIndex() view, but when I call /admin/page - I get:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

Controller method not found.

I know you can nest Route::group calls, but it doesn't seem to be documented well anywhere how to achieve it. From my understanding, you have to have 'prefix' specified with each Route::group call - In the nested one I've just used blank string '' - as it doesn't require any additional prefix apart from the parent one. The encapsulated calls to controllers within the nested group require admin.auth filter - and that's the reason why I wanted to enclose them in the nested group - rather than specifying filter for each controller separately.

Any idea what needs to be done to make this scenario work?

Also - even if I change the code so that it calls controllers directly under the parent group like so:

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

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

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

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

    }
);

I seem to be getting the same error when I call /admin/page - PageController looks like this:

namespace Admin;

use BaseController; use View;

class PageController extends BaseController {



    public function getIndex() {

        return View::make('Admin.page.index');

    }



} 

Upvotes: 0

Views: 1868

Answers (2)

Andreas
Andreas

Reputation: 8019

I'll just chime in to say that by being explicit in your routing - only using Route::get/post/delete etc and not Route::controller or Route::resource - you avoid this sort of problem and lots of others. Route::controller especially is considered bad practice.

Upvotes: 2

Spencer Mark
Spencer Mark

Reputation: 5311

Ok - after experimenting with it for a while the answer appears to be in the order that you put the calls within your group.

When I move call to Route::controller('/', 'LoginController'); after the nested group - then everything seem to work fine:

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

    }
);

It's a shame that such an important aspect isn't documented anywhere - nevertheless - it works!

Upvotes: 0

Related Questions