Laravel: 如何获取当前路由名称? (v5 ... v7)
Laravel: How to Get Current Route Name? (v5 ... v7)
在 Laravel v4 中,我能够使用...
获取当前路由名称
Route::currentRouteName()
如何在 Laravel v5 和 Laravel v6 中实现?
找到了一种查找当前路由名称的方法适用于 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();
试试这个
Route::getCurrentRoute()->getPath();
或
\Request::route()->getName()
来自 v5.1
use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();
Laravel v5.2
Route::currentRouteName(); //use Illuminate\Support\Facades\Route;
或者如果您需要操作名称
Route::getCurrentRoute()->getActionName();
Laravel 5.2 route documentation
正在检索请求 URI
路径方法 return请求的 URI。因此,如果传入请求的目标是 http://example.com/foo/bar
,路径方法将 return foo/bar
:
$uri = $request->path();
is
方法允许您验证传入请求 URI 是否与给定模式匹配。使用此方法时,您可以使用 *
字符作为通配符:
if ($request->is('admin/*')) {
//
}
要获取完整的 URL,而不仅仅是路径信息,您可以在请求实例上使用 url 方法:
$url = $request->url();
Laravel v5.3 ... v5.8
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
Laravel 5.3 route documentation
Laravel v6.x...7.x
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
** 当前截至 2019 年 11 月 11 日 - 版本 6.5 **
Laravel 6.x route documentation
有一个选项可以使用请求获取路由
$request->route()->getName();
在控制器操作中,您可以这样做:
public function someAction(Request $request)
{
$routeName = $request->route()->getName();
}
$request
这里由Laravel的服务容器解析
getName()
returns 仅 named routes 的路线名称,null
否则(但您仍然可以探索 \Illuminate\Routing\Route
对象以获得其他感兴趣的东西).
换句话说,您应该像这样定义您的路线以返回 "nameOfMyRoute":
Route::get('my/some-action', [
'as' => 'nameOfMyRoute',
'uses' => 'MyController@someAction'
]);
使用Laravel 5.1,您可以使用
\Request::route()->getName()
您可以在模板中使用:
<?php $path = Route::getCurrentRoute()->getPath(); ?>
<?php if (starts_with($path, 'admin/')) echo "active"; ?>
Request::path();
比较好,记得use Request;
如果您需要url,而不是路线名称,您不需要use/require任何其他类:
url()->current();
Laravel 5.2 你可以使用
$request->route()->getName()
它会给你当前的路线名称。
如果您想 select 在多条路线上选择菜单,您可以这样做:
<li class="{{ (Request::is('products/*') || Request::is('products') || Request::is('product/*') ? 'active' : '') }}"><a href="{{url('products')}}"><i class="fa fa-code-fork"></i> Products</a></li>
或者,如果您只想 select 单个菜单,您可以这样做:
<li class="{{ (Request::is('/users') ? 'active' : '') }}"><a href="{{url('/')}}"><i class="fa fa-envelope"></i> Users</a></li>
还在Laravel 5.2
中测试
希望这对某人有所帮助。
在 5.2 中,您可以直接使用请求:
$request->route()->getName();
或通过辅助方法:
request()->route()->getName();
输出示例:
"home.index"
最短路线是 Route facade
\Route::current()->getName()
这也适用于 laravel 5.4。*
现在 Laravel 5.3
我看到可以像您尝试的那样制作:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
https://laravel.com/docs/5.3/routing#accessing-the-current-route
在帮助程序文件中,
您可以使用 Route::current()->uri()
获取当前 URL。
因此,如果您比较路线名称以在菜单上设置活动 class,那么使用
会更好
Route::currentRouteName()
获取路由名称并比较
我已经在 laravel 5.3 中用于获取路由名称
Request::path()
查看 \Illuminate\Routing\Router.php
,您可以通过在控制器方法中注入路由器来使用方法 currentRouteNamed()
。例如:
use Illuminate\Routing\Router;
public function index(Request $request, Router $router) {
return view($router->currentRouteNamed('foo') ? 'view1' : 'view2');
}
或使用 Route facade:
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');
}
访问当前路由(v5.3 以上)
您可以使用 Route facade 上的 current、currentRouteName 和 currentRouteAction 方法来访问有关处理传入请求的路由的信息:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
请参阅 API 文档以了解 Route facade 和 Route 实例的底层 class 以查看所有可访问的方法。
参考:https://laravel.com/docs/5.2/routing#accessing-the-current-route
访问当前路由
获取 Blade 模板中的当前路由名称
{{ Route::currentRouteName() }}
了解更多信息https://laravel.com/docs/5.5/routing#accessing-the-current-route
出于某些原因,我无法使用这些解决方案中的任何一个。所以我只是在 web.php
中将我的路线声明为 $router->get('/api/v1/users', ['as' => 'index', 'uses' => 'UserController@index'])
并且在我的控制器中我使用 $routeName = $request->route()[1]['as'];
获得了路线的名称,其中 $request
是 \Illuminate\Http\Request $request
中的类型提示参数index
UserController
方法
使用 Lumen 5.6。希望对大家有所帮助。
解决方案:
$routeArray = app('request')->route()->getAction();
$controllerAction = class_basename($routeArray['controller']);
list($controller, $route) = explode('@', $controllerAction);
echo $route;
$request->route()->getName();
在我看来,最简单的解决方案是使用这个助手:
request()->route()->getName()
有关文档,请参阅 this link
您可以使用以下方法:
Route::getCurrentRoute()->getPath();
在Laravel版本> 6.0,您可以使用以下方法:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
访问控制器中的当前路由名称
即-http:///localhost/your_project_name/edit
$request->segment(1); // edit
(或)
$request->url(); // http://localhost/your_project_name/edit
您可以使用下面的代码在 blade 文件中获取路由名称
request()->route()->uri
您可以使用这行代码:url()->current()
在 blade 文件中:{{url()->current()}}
有很多方法可以做到这一点。您可以输入:
\Illuminate\Support\Facades\Request::route()->getName()
获取路线名称。
如果直接查看路线名称或 url 需要 id,则无人回答
直接查看路线名称
$routeName = Request::route()->getName();
来自 url 的 ID 在视图
$url_id = Request::segment(2);
您可能要做的第一件事是在 class.
的顶部导入 namespace
use Illuminate\Support\Facades\Route;
laravelv8
$route = Route::current(); // Illuminate\Routing\Route
$name = Route::currentRouteName(); // RouteName
$action = Route::currentRouteAction(); // Action
Laravel v7,6 和 5.8
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
\Request::path()
我用它来获取当前 uri
使用laravel辅助和魔法方法
request()->route()->getName()
在laravel 7或8中使用辅助函数
Get Current Route Name
request()->route()->getName()
To check if route is current better to create your own method for request class using macro
在AppServiceProvider
中在boot
方法中:
use Illuminate\Http\Request;
public function boot()
{
Request::macro('isCurrentRoute', function ($routeNames) {
return in_array(
request()->route()->getName(),
is_array($routeNames) ? $routeNames : explode(",", $routeNames)
);
});
}
You can used this method in blade or controller
request()->isCurrentRoute('foo') // string route
request()->isCurrentRoute(['bar','foo']) //array routes
request()->isCurrentRoute('blogs,foo,bar') //string route seperated by comma
You can use inbuilt laravel route method
route()->is('home');
route()->is('blogs.*'); //using wildcard
这是我用的,我不知道为什么没有人提到它,因为它对我来说非常好用。
Route::getCurrentRoute()->uri ; // this returns a string like '/home'
所以我在我的 master.blade.php
文件中使用它:
...
@if ( Route::getCurrentRoute()->uri =='/dashbord' )
@include('navbar')
@endif
...
确实帮助我重新使用相同的视图而无需重复代码。
在 Laravel v4 中,我能够使用...
获取当前路由名称Route::currentRouteName()
如何在 Laravel v5 和 Laravel v6 中实现?
找到了一种查找当前路由名称的方法适用于 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();
试试这个
Route::getCurrentRoute()->getPath();
或
\Request::route()->getName()
来自 v5.1
use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();
Laravel v5.2
Route::currentRouteName(); //use Illuminate\Support\Facades\Route;
或者如果您需要操作名称
Route::getCurrentRoute()->getActionName();
Laravel 5.2 route documentation
正在检索请求 URI
路径方法 return请求的 URI。因此,如果传入请求的目标是 http://example.com/foo/bar
,路径方法将 return foo/bar
:
$uri = $request->path();
is
方法允许您验证传入请求 URI 是否与给定模式匹配。使用此方法时,您可以使用 *
字符作为通配符:
if ($request->is('admin/*')) {
//
}
要获取完整的 URL,而不仅仅是路径信息,您可以在请求实例上使用 url 方法:
$url = $request->url();
Laravel v5.3 ... v5.8
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
Laravel 5.3 route documentation
Laravel v6.x...7.x
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
** 当前截至 2019 年 11 月 11 日 - 版本 6.5 **
Laravel 6.x route documentation
有一个选项可以使用请求获取路由
$request->route()->getName();
在控制器操作中,您可以这样做:
public function someAction(Request $request)
{
$routeName = $request->route()->getName();
}
$request
这里由Laravel的服务容器解析
getName()
returns 仅 named routes 的路线名称,null
否则(但您仍然可以探索 \Illuminate\Routing\Route
对象以获得其他感兴趣的东西).
换句话说,您应该像这样定义您的路线以返回 "nameOfMyRoute":
Route::get('my/some-action', [
'as' => 'nameOfMyRoute',
'uses' => 'MyController@someAction'
]);
使用Laravel 5.1,您可以使用
\Request::route()->getName()
您可以在模板中使用:
<?php $path = Route::getCurrentRoute()->getPath(); ?>
<?php if (starts_with($path, 'admin/')) echo "active"; ?>
Request::path();
比较好,记得use Request;
如果您需要url,而不是路线名称,您不需要use/require任何其他类:
url()->current();
Laravel 5.2 你可以使用
$request->route()->getName()
它会给你当前的路线名称。
如果您想 select 在多条路线上选择菜单,您可以这样做:
<li class="{{ (Request::is('products/*') || Request::is('products') || Request::is('product/*') ? 'active' : '') }}"><a href="{{url('products')}}"><i class="fa fa-code-fork"></i> Products</a></li>
或者,如果您只想 select 单个菜单,您可以这样做:
<li class="{{ (Request::is('/users') ? 'active' : '') }}"><a href="{{url('/')}}"><i class="fa fa-envelope"></i> Users</a></li>
还在Laravel 5.2
中测试希望这对某人有所帮助。
在 5.2 中,您可以直接使用请求:
$request->route()->getName();
或通过辅助方法:
request()->route()->getName();
输出示例:
"home.index"
最短路线是 Route facade
\Route::current()->getName()
这也适用于 laravel 5.4。*
现在 Laravel 5.3
我看到可以像您尝试的那样制作:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
https://laravel.com/docs/5.3/routing#accessing-the-current-route
在帮助程序文件中,
您可以使用 Route::current()->uri()
获取当前 URL。
因此,如果您比较路线名称以在菜单上设置活动 class,那么使用
会更好Route::currentRouteName()
获取路由名称并比较
我已经在 laravel 5.3 中用于获取路由名称
Request::path()
查看 \Illuminate\Routing\Router.php
,您可以通过在控制器方法中注入路由器来使用方法 currentRouteNamed()
。例如:
use Illuminate\Routing\Router;
public function index(Request $request, Router $router) {
return view($router->currentRouteNamed('foo') ? 'view1' : 'view2');
}
或使用 Route facade:
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');
}
访问当前路由(v5.3 以上)
您可以使用 Route facade 上的 current、currentRouteName 和 currentRouteAction 方法来访问有关处理传入请求的路由的信息:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
请参阅 API 文档以了解 Route facade 和 Route 实例的底层 class 以查看所有可访问的方法。
参考:https://laravel.com/docs/5.2/routing#accessing-the-current-route
访问当前路由
获取 Blade 模板中的当前路由名称
{{ Route::currentRouteName() }}
了解更多信息https://laravel.com/docs/5.5/routing#accessing-the-current-route
出于某些原因,我无法使用这些解决方案中的任何一个。所以我只是在 web.php
中将我的路线声明为 $router->get('/api/v1/users', ['as' => 'index', 'uses' => 'UserController@index'])
并且在我的控制器中我使用 $routeName = $request->route()[1]['as'];
获得了路线的名称,其中 $request
是 \Illuminate\Http\Request $request
中的类型提示参数index
UserController
使用 Lumen 5.6。希望对大家有所帮助。
解决方案:
$routeArray = app('request')->route()->getAction();
$controllerAction = class_basename($routeArray['controller']);
list($controller, $route) = explode('@', $controllerAction);
echo $route;
$request->route()->getName();
在我看来,最简单的解决方案是使用这个助手:
request()->route()->getName()
有关文档,请参阅 this link
您可以使用以下方法:
Route::getCurrentRoute()->getPath();
在Laravel版本> 6.0,您可以使用以下方法:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
访问控制器中的当前路由名称
即-http:///localhost/your_project_name/edit
$request->segment(1); // edit
(或)
$request->url(); // http://localhost/your_project_name/edit
您可以使用下面的代码在 blade 文件中获取路由名称
request()->route()->uri
您可以使用这行代码:url()->current()
在 blade 文件中:{{url()->current()}}
有很多方法可以做到这一点。您可以输入:
\Illuminate\Support\Facades\Request::route()->getName()
获取路线名称。
如果直接查看路线名称或 url 需要 id,则无人回答 直接查看路线名称
$routeName = Request::route()->getName();
来自 url 的 ID 在视图
$url_id = Request::segment(2);
您可能要做的第一件事是在 class.
的顶部导入 namespaceuse Illuminate\Support\Facades\Route;
laravelv8
$route = Route::current(); // Illuminate\Routing\Route
$name = Route::currentRouteName(); // RouteName
$action = Route::currentRouteAction(); // Action
Laravel v7,6 和 5.8
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
\Request::path()
我用它来获取当前 uri
使用laravel辅助和魔法方法
request()->route()->getName()
在laravel 7或8中使用辅助函数
Get Current Route Name
request()->route()->getName()
To check if route is current better to create your own method for request class using macro
在AppServiceProvider
中在boot
方法中:
use Illuminate\Http\Request;
public function boot()
{
Request::macro('isCurrentRoute', function ($routeNames) {
return in_array(
request()->route()->getName(),
is_array($routeNames) ? $routeNames : explode(",", $routeNames)
);
});
}
You can used this method in blade or controller
request()->isCurrentRoute('foo') // string route
request()->isCurrentRoute(['bar','foo']) //array routes
request()->isCurrentRoute('blogs,foo,bar') //string route seperated by comma
You can use inbuilt laravel route method
route()->is('home');
route()->is('blogs.*'); //using wildcard
这是我用的,我不知道为什么没有人提到它,因为它对我来说非常好用。
Route::getCurrentRoute()->uri ; // this returns a string like '/home'
所以我在我的 master.blade.php
文件中使用它:
...
@if ( Route::getCurrentRoute()->uri =='/dashbord' )
@include('navbar')
@endif
...
确实帮助我重新使用相同的视图而无需重复代码。