Laravel 5 route::group more than one controller issue

- 1 answer

Ad

I'm new to Laravel. I have url like this example.com/admin, everything works fine with this. But when i want to make a route in admin like this example.com/admin/users then i have a problem. My routes looks like this:

Route::group(['namespace' => 'Admin'], function() { Route::resource('admin', 'AdminController'); Route::resource('admin/users', 'UsersController'); });

So i generated controllers via artisan command tool, and if i try to load ../admin/create it works, but if i try to load .../admin/users it show a blank page, but the strangest part is that .../admin/users/create works.

I'm new to Laravel so maybe my understanding about routes is wrong and i cant do like i'm trying to do. Thanks in advance.

Ad

Answer

Ad

Laravel routing works like greedy match. In your route file if you have two routes like this

Route::group(['namespace' => 'Admin'], function() {
    Route::resource('admin', 'AdminController');
    Route::resource('admin/users', 'UsersController');
});

Laravel router for urls like admin/users it will match against admin route.

So a good tip for Laravel routing would be you create route in descending order. The url has most backslashes value you write that at top of you route file & then to less.

Route::group(['namespace' => 'Admin'], function() {
    Route::resource('admin/users/**/**', '**');
    Route::resource('admin/users/****', '***');
    Route::resource('admin/users', 'UsersController');
    Route::resource('admin', 'AdminController');
});

So here you solution will be, put your second route up & first one down

Route::group(['namespace' => 'Admin'], function() {
    Route::resource('admin/users', 'UsersController');
    Route::resource('admin', 'AdminController');
});
Ad
source: stackoverflow.com
Ad