jack_0
jack_0

Reputation: 37

Routing laravel conflict

I have to config route to respond to this url:

To do this i wrote this code in the web.php file:

Route::get('/SomeStaticString','PagesController@SomeStaticString');
Route::get('/{main_category}/{slug}','PagesController@getArticle')->where('main_category', '^(?!SomeStaticString$).*');

Is this the right way to handle these url? If I have to add other url "SomeStaticString" to my application what is the best route implementation?

Upvotes: 2

Views: 946

Answers (1)

Zach Fine
Zach Fine

Reputation: 86

You always want to make sure that static routes are defined before wildcard ones because otherwise the router will mistake a static route as a wilcard. Heres a quick example:

//This will work
Route::get('foo', ' YourController@foo');
Route::get('{bar}', 'YourController@bar');

//In this order, foo will never get called
Route::get('{bar}', 'YourController@bar');    
Route::get('foo', ' YourController@foo');

When you start add more segments to your path, you always want to keep wildcards at the back of their respective sub-groups. This is how a progressively growing route tree should be ordered.

//This is good
Route::get('foo', ' YourController@foo');
Route::get('foo/bar', ' YourController@bar');
Route::get('foo/{id}', 'YourController@foobar');
Route::get('foo/bar/stuff', 'YourController@extrafoobar');
Route::get('foo/bar/{id}', 'YourController@megafoobar');
Route::get('foo/{bar}/stuff', 'YourController@uberfoobar');
Route::get('foo/{bar}/{id}', 'YourController@finalfoobar');

For your case in particular you would want to have your routes organized like this to begin:

Route::get('/addClickBanner/{id_banner}','PagesController@SomeStaticString');
Route::get('someStaticString/{main_category}/{slug}','PagesController@getArticle')->where('main_category', '^(?!SomeStaticString$).*');

Upvotes: 7

Related Questions