简体   繁体   中英

Routing laravel conflict

I have to config route to respond to this url:

  • /SomeStaticString
  • /{category}/{id}

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?

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$).*');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM