在Laravel v4中,我可以使用…
Route::currentRouteName()
我如何在Laravel v5和Laravel v6中做到这一点?
在Laravel v4中,我可以使用…
Route::currentRouteName()
我如何在Laravel v5和Laravel v6中做到这一点?
当前回答
访问当前路由(v5.3起)
你可以在Route虚包上使用current, currentRouteName和currentRouteAction方法来访问处理传入请求的路由信息:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
参考Route虚包和Route实例的底层类的API文档,查看所有可访问的方法。
参考资料:https://laravel.com/docs/5.2/routing#accessing-the-current-route
其他回答
在我看来,最简单的解决方案是使用这个助手:
request()->route()->getName()
有关文档,请参见这个链接
现在在Laravel 5.3中,我看到可以做出类似的尝试:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
https://laravel.com/docs/5.3/routing#accessing-the-current-route
$request->route()->getName();
\Request::path()
我用这个来获取当前uri
找到了一种查找当前路由名称的方法,适用于laravel v5, v5.1.28和v5.2.10
名称空间
use Illuminate\Support\Facades\Route;
and
$currentPath= Route::getFacadeRoot()->current()->uri();
对于Laravel Laravel v5.3,你可以使用:
use Illuminate\Support\Facades\Route;
Route::currentRouteName();