2016-03-04 154 views
0

我使用Laravel 3,我知道。它的舊。它是一個傳統項目。 但我正在用允許在控制器映射路由中的路由在laravel

Route::controller(Controller::detect()); 

檢測我的控制器自動然而,這不會讓我利用蛞蝓上一般網址,而不試圖映射到控制器。

因此可以說我有兩個控制器,Test1和Test2。

www.mysite.com/test1 
www.mysite.com/test2 

這樣很好。

但我也希望能夠做到

www.mysite.com/{theusersprofilename} //example 

而且把它調出資料。 我知道我可以做

www.mysite.com/users/profile/{user_id} 

但我希望能夠在網站的網址後,只需輸入用戶名,並有路線的工作。我怎麼才能檢索slug沒有Laravel重定向到401,並認爲我想加載名爲'theusersprofilename'的控制器

回答

0

首先,你需要有一個捕獲所有控制器或路由來重定向請求。

Route::any("(:all?)" , function($slug){ //check if it matches any user profile $user = User::where("slug", "=" , $slug)->first(); if($user->exists) // do your user stuff or redirect to user's profile // do nothing after this }); Route::controller(Controller::detect()); 然後它會被你的控制器捕獲。我沒有測試代碼片,但邏輯上它應該工作,因爲路由器::路由陣列讓他們爲了他們在你的代碼。通過GET/POST ..命令。