2017-02-09 179 views
1

我必须配置路由到这个网址回应:路由laravel冲突

  • /SomeStaticString
  • /{类别}/{ID}

要做到这一点,我在写这个代码web.php文件:

Route::get('/SomeStaticString','[email protected]'); 
Route::get('/{main_category}/{slug}','[email protected]')->where('main_category', '^(?!SomeStaticString$).*'); 

这是正确的方式来处理这些网址? 如果我必须添加其他网址“SomeStaticString”到我的应用程序什么是最好的路线实施?

+0

您的意思是' - >在哪里( 'main_category',“^(?!addClickBanner $ )。*');'?如果'id_banner'是一个数字,而'slug'不是,你可以用'[0-9] +' –

+0

'来限制'id_banner'只使用路由参数,比如在Route :: get('/ {main_category}/{slug}'...)将抓取每一个uri,如果在这条路线之前没有明确指出uri。 – Birdman

+0

你应该在每条你关心的路线下放一条类似'/ {main_category}/{slug}'的路线。像SomeStaticString这样的新路线应放置在这条路线上方。 –

回答

1

您一定要确保在通配符之前定义静态路由,否则路由器会将静态路由误认为wilcard。下面有一个简单的例子:

//This will work 
Route::get('foo', ' [email protected]'); 
Route::get('{bar}', '[email protected]'); 

//In this order, foo will never get called 
Route::get('{bar}', '[email protected]');  
Route::get('foo', ' [email protected]'); 

当你开始增加更多的细分到您的路径,你总是希望保持通配符在各自的子组的后面。这就是如何逐步增加路线树。

//This is good 
Route::get('foo', ' [email protected]'); 
Route::get('foo/bar', ' [email protected]'); 
Route::get('foo/{id}', '[email protected]'); 
Route::get('foo/bar/stuff', '[email protected]'); 
Route::get('foo/bar/{id}', '[email protected]'); 
Route::get('foo/{bar}/stuff', '[email protected]'); 
Route::get('foo/{bar}/{id}', '[email protected]'); 

特别是对于你的情况,你会希望有你的路由组织这样开始:

Route::get('/addClickBanner/{id_banner}','[email protected]'); 
Route::get('someStaticString/{main_category}/{slug}','[email protected]')->where('main_category', '^(?!SomeStaticString$).*'); 
+0

完美!它像一个魅力!谢谢@Zach –