php - 如何获取当前路由名称?
在Laravel 4中,我能够使用...获取当前路线名称
Route::currentRouteName()
我怎么能在Laravel 5中做到这一点?
18个解决方案
297 votes
试试这个
Route::getCurrentRoute()->getPath();
要么
\Request::route()->getName()
来自v5。+
use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();
Laravel 5.3
Route::currentRouteName(); //use Illuminate\Support\Facades\Route;
或者如果您需要动作名称
Route::getCurrentRoute()->getActionName();
您可以在Laravel API中找到有关laravel Routes的所有信息:[http://laravel.com/api/5.0/Illuminate/Routing/Router.html][http://laravel.com/api/5.0/Illuminate/Routing.html]
检索请求URI
path方法返回请求的URI。 因此,如果传入请求的目标是http://example.com/foo/bar,则路径方法将返回is:
$uri = $request->path();
is方法允许您验证传入请求URI是否与给定模式匹配。 使用此方法时,可以使用*字符作为通配符:
if ($request->is('admin/*')) {
//
}
要获取完整的URL,而不仅仅是路径信息,您可以在请求实例上使用url方法:
$url = $request->url();
Adnan answered 2019-05-10T15:25:22Z
29 votes
使用Laravel 5.1,您可以使用
\Request::route()->getName()
loranger answered 2019-05-10T15:25:49Z
21 votes
找到一种方法来查找当前路由名称适用于laravel v5,v5.1.28和v5.2.10
命名空间
use Illuminate\Support\Facades\Route;
和
$currentPath= Route::getFacadeRoot()->current()->uri();
对于Laravel laravel v5.3,您可以使用:
use Illuminate\Support\Facades\Route;
Route::currentRouteName();
Md Rashedul Hoque Bhuiyan answered 2019-05-10T15:26:25Z
18 votes
如果您需要url而不是路由名称,则不需要使用/ require任何其他类:
url()->current();
Fusion answered 2019-05-10T15:26:50Z
14 votes
如果您想在多条路线上选择菜单,您可以这样做:
Products或者,如果您只想选择单个菜单,您可以这样做:
'as' => 'nameOfMyRoute',
'uses' => 'MyController@someAction'
]);
Bogdan Ghervan answered 2019-05-10T15:31:35Z
1 votes
我已经习惯在laravel 5.3中获取路由名称
Request::path()
Dipak Kolhe answered 2019-05-10T15:32:09Z
1 votes
查看is(),您可以通过在控制器方法中注入路由器来使用方法preg_match()。 例如:
use Illuminate\Routing\Router;
public function index(Request $request, Router $router) {
return view($router->currentRouteNamed('foo') ? 'view1' : 'view2');
}
或使用路线外观:
public function index(Request $request) {
return view(\Route::currentRouteNamed('foo') ? 'view1' : 'view2');
}
您还可以使用方法is()来检查路由是否被命名为任何给定参数,但要注意此方法使用preg_match()并且我经历过它会导致使用虚线路由名称的奇怪行为(如'foo.bar.done')。 preg_match()也有表现问题 这是PHP社区的一个重要主题。
public function index(Request $request) {
return view(\Route::is('foo', 'bar') ? 'view1' : 'view2');
}
Ken answered 2019-05-10T15:32:55Z
1 votes
访问当前路线(v5.3以后)
您可以使用Route facade上的current,currentRouteName和currentRouteAction方法来访问有关处理传入请求的路由的信息:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
有关Route facade和Route实例的基础类,请参阅API文档以查看所有可访问的方法。
参考:[https://laravel.com/docs/5.2/routing#accessing-the-current-route]
Amitesh answered 2019-05-10T15:33:46Z
0 votes
在帮助文件中,
您可以使用Route::currentRouteName()获取当前URL。
因此,如果您比较您的路线名称以在菜单上设置活动类,那么如果您使用它将是好的
Route::currentRouteName()获取路线名称并进行比较
ankit patel answered 2019-05-10T15:34:38Z
0 votes
访问当前路线
在Blade模板中获取当前路由名称
{{Route :: currentRouteName()}}获取路由名称
了解更多信息[https://laravel.com/docs/5.5/routing#accessing-the-current-route]
yogesh chatrola answered 2019-05-10T15:35:30Z
0 votes
由于某些原因,我无法使用任何这些解决方案。 所以我刚刚在web.php宣布我的路线为$router->get('/api/v1/users', ['as' => 'index', 'uses' => 'UserController@index'])并且在我的控制器中我得到的路线名称使用$routeName = $request->route()[1]['as'];其中$request是\Illuminate\Http\Request $request类型参数index方法UserController
使用流明5.6。 希望它能帮助别人。
MajidJafari answered 2019-05-10T15:36:13Z