StuBlackett
StuBlackett

Reputation: 3857

Laravel Routes ordering

I am using Laravel 4.2 on a project.

I have setup routes for :

slug, category, services

In terms of ordering, If I move services above slug, It can't find the slug and ultimately goes to to the services controller and same, If I keep the slug method above services. It can't find that either.

My current routes is as follows :

Route::get('{slug?}', 'HomeController@index');
Route::get('{category?}', 'HomeController@category');
Route::get('{services?}', 'ServicesController@index');

Is there a way to fix this, The category one works fine, But switching slug and services around creates a problem.

Thanks

Upvotes: 0

Views: 914

Answers (1)

Glad To Help
Glad To Help

Reputation: 5387

They way you wrote the routes, Laravel has no way to tell which request should go to which route, because you wrote three routes with one optional parameter which could be anything. The way routing works is: it applies the first route that matches the pattern. That is why when it hits the first route pattern that says "one param that can be anything" it picks that route.

You should give Laravel some kind of hint, like:

Route::get('{slug?}', 'HomeController@index')->where('slug', '(blog|home|contact)');

Route::get('/category/{category?}', 'HomeController@category');

Route::get('/services/{services?}', 'ServicesController@index');

If you restrict the possible values or if you put additional part in the URL, Laravel will be able to recognize which route should go to which handler.

Take a look at the docs to get more ideas

Upvotes: 3

Related Questions