ziben69's avatar

Laravel route conflict

Hello guys,

I have route:

Route::get('/{slug}', 'PageController@show');

and second route:

Route::get('catalog/', 'CatalogController@index');

When I try to go in localhost/catalog I get an error indicating the use of a function from PageController called "show" and should use the "index" function of the CatalogController.

How can I fix it?

0 likes
8 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Laravel reads from the top and uses first match. Your routes need to be specific first, then wildcard later

Route::get('catalog/', 'CatalogController@index');
Route::get('/{slug}', 'PageController@show');
smartnathan's avatar

Let me see the error message, also how are you running your application. Is it as a virtual host or do you have it running in the root directory of your xampp, lamp or wamp server? Or you are using the Php built in http server via laravel aritisan command. I will need clarity, inorder to assist.

ziben69's avatar

@sinnbeck

However, reordering didn't help. The subpage /catalog is now displayed to me, but the subpages /{slug} are not displayed to me.

Let me add complete routing and if you can, take a look again.

Sinnbeck's avatar

Sure. Post the routes and I will take a look :) Also post example of a url that does not work

ziben69's avatar

And I think I found a conflict :)

Route::get('/{id}', 'CatalogController@show');~

I changed to:

Route::get('catalog/{id}', 'CatalogController@show');

and it worked as it should;

the reason is probably:

Route::get('/{slug}', 'PageController@show');~
Sinnbeck's avatar

Yes that will never work. You should consider using a prefix for your routes

Route::get('/pages/{slug}', 'PageController@show');
Route::get('/catelogues/{id}', 'CatalogController@show');

Take a look at laracasts for instance. If you go to Journeys, they are all prefixed with /skills/. And lessons are prefixed with /series/

Please or to participate in this conversation.