2017-02-09 158 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 –