允许在控制器映射路由中的路由在laravel
问题描述:
我使用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'的控制器
答
首先,你需要有一个捕获所有控制器或路由来重定向请求。
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 ..命令。