I am trying to get current route name using Laravel 5.
我正在尝试用Laravel 5获得当前的路线名称。
In laravel 4 i was able to do it using Route::currentRouteName().
在laravel 4中,我可以使用Route::currentRouteName()来完成。
How can i do it in laravel 5?
我怎么用laravel 5呢?
Thanks
谢谢
15 个解决方案
#1
252
Try this
试试这个
Route::getCurrentRoute()->getPath();
or
或
\Request::route()->getName()
from v5.+
从v5 +。
use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();
Laravel 5.3
Laravel 5.3
Route::currentRouteName(); //use Illuminate\Support\Facades\Route;
Or if you need the action name
或者如果你需要动作名。
Route::getCurrentRoute()->getActionName();
You can find everything about laravel Routes in the Laravel API: http://laravel.com/api/5.0/Illuminate/Routing/Router.html http://laravel.com/api/5.0/Illuminate/Routing.html
您可以在laravel API中找到有关laravel路由的所有内容:http://laravel.com/api/5.0/Illuminate/Routing/Router.html http://laravel.com/api/5.0/Illuminate/Routing.html
Retrieving The Request URI
检索请求URI
The path method returns the request's URI. So, if the incoming request is targeted at http://example.com/foo/bar
, the path method will return foo/bar
:
路径方法返回请求的URI。因此,如果传入请求的目标是http://example.com/foo/bar,那么path方法将返回foo/bar:
$uri = $request->path();
The is
method allows you to verify that the incoming request URI matches a given pattern. You may use the *
character as a wildcard when utilizing this method:
is方法允许您验证传入的请求URI是否与给定的模式匹配。使用此方法时,您可以使用*字符作为通配符:
if ($request->is('admin/*')) {
//
}
To get the full URL, not just the path info, you may use the url method on the request instance:
要获得完整的URL,而不仅仅是路径信息,您可以在请求实例上使用URL方法:
$url = $request->url();
#2
25
Using Laravel 5.1, you can use
使用Laravel 5.1,您可以使用
\Request::route()->getName()
#3
19
Found a way to find the current route name works for laravel v5 , v5.1.28 and v5.2.10
找到一种方法来找到laravel v5、v5.1.28和v5.2.10的当前路由名称
Namespace
名称空间
use Illuminate\Support\Facades\Route;
and
和
$currentPath= Route::getFacadeRoot()->current()->uri();
For Laravel laravel v5.3 you can just use:
对于Laravel的v5.3,你可以使用:
use Illuminate\Support\Facades\Route;
Route::currentRouteName();
#4
15
If you need url, not route name, you do not need to use/require any other classes:
如果您需要url,而不是路由名称,则不需要使用/需要任何其他类:
url()->current();
#5
10
If you want to select menu on multiple routes you may do like this:
如果你想在多条路线上选择菜单,你可以这样做:
<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>
Or if you want to select just single menu you may simply do like this:
或者,如果你只想选择一个菜单,你可以这样做:
<li class="{{ (Request::is('/users') ? 'active' : '') }}"><a href="{{url('/')}}"><i class="fa fa-envelope"></i> Users</a></li>
Also tested in Laravel 5.2
在Laravel 5.2中也进行了测试
Hope this help someone.
希望这帮助别人。
#6
7
Laravel 5.2 You can use
Laravel 5.2可以使用
$request->route()->getName()
It will give you current route name.
它会给你当前的路径名。
#7
5
You can use in template:
你可以在模板中使用:
<?php $path = Route::getCurrentRoute()->getPath(); ?>
<?php if (starts_with($path, 'admin/')) echo "active"; ?>
#8
5
Shortest way is Route facade \Route::current()->getName()
最短路径是路由facade \路由::current()->getName()
This also works in laravel 5.4.*
这也适用于laravel 5.4 *
#9
3
Now in Laravel 5.3
I am seeing that can be made similarly you tried:
在Laravel 5。3中,我发现可以做类似的东西
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
https://laravel.com/docs/5.3/routing#accessing-the-current-route
https://laravel.com/docs/5.3/routing accessing-the-current-route
#10
2
In a controller action, you could just do:
在控制器动作中,你可以做:
public function someAction(Request $request)
{
$routeName = $request->route()->getName();
}
$request
here is resolved by Laravel's service container.
这里的$request由Laravel的服务容器解决。
getName()
returns the route name for named routes only, null
otherwise (but you could still explore the \Illuminate\Routing\Route
object for something else of interest).
getName()只返回已命名路由的路由名称,否则为null(但是您仍然可以为其他感兴趣的东西探索\照明的路由对象)。
In other words, you should have your route defined like this to have "nameOfMyRoute" returned:
换句话说,您应该像这样定义您的路由,使“nameOfMyRoute”返回:
Route::get('my/some-action', [
'as' => 'nameOfMyRoute',
'uses' => 'MyController@someAction'
]);
#11
2
Request::path();
is better, and remember to Use Request;
要求::路径();是更好的,记得使用请求;
#12
2
In 5.2, you can use the request directly with:
在5.2中,您可以直接使用以下请求:
$request->route()->getName();
or via the helper method:
或透过辅助方法:
request()->route()->getName();
Output example:
输出的例子:
"home.index"
#13
1
I have used for getting route name in larvel 5.3
我在larvel 5.3中使用过获取路线名称
Request::path()
要求::路径()
#14
0
In a Helper file,
在一个辅助文件中,
Your can use Route::current()->uri()
to get current URL.
您可以使用Route::current()->uri()获取当前URL。
Hence, If you compare your route name to set active class on menu then it would be good if you use
因此,如果您将您的路由名称与在菜单上设置活动类进行比较,那么最好使用
Route::currentRouteName()
to get the name of route and compare
路由:currentRouteName()获取路由的名称并进行比较
#15
0
Looking at \Illuminate\Routing\Router.php
you can use the method currentRouteNamed()
by injecting a Router in your controller method. For example:
看着\照亮\路由\路由器。通过在控制器方法中注入路由器,您可以使用currentRouteNamed()方法。例如:
use Illuminate\Routing\Router;
public function index(Request $request, Router $router) {
return view($router->currentRouteNamed('foo') ? 'view1' : 'view2');
}
or using the Route facade:
或使用路线外立面:
public function index(Request $request) {
return view(\Route::currentRouteNamed('foo') ? 'view1' : 'view2');
}
You could also use the method is()
to check if the route is named any of the given parameters, but beware this method uses preg_match()
and I've experienced it to cause strange behaviour with dotted route names (like 'foo.bar.done'
). There is also the matter of performance around preg_match()
which is a big subject in the PHP community.
您也可以使用方法is()来检查路由是否被命名为任何给定的参数,但是要注意,这个方法使用preg_match(),我曾经经历过它会导致带有点路径名的奇怪行为(比如'foo.bar.done')。还有关于preg_match()的性能问题,这是PHP社区中的一个重要主题。
public function index(Request $request) {
return view(\Route::is('foo', 'bar') ? 'view1' : 'view2');
}
#1
252
Try this
试试这个
Route::getCurrentRoute()->getPath();
or
或
\Request::route()->getName()
from v5.+
从v5 +。
use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();
Laravel 5.3
Laravel 5.3
Route::currentRouteName(); //use Illuminate\Support\Facades\Route;
Or if you need the action name
或者如果你需要动作名。
Route::getCurrentRoute()->getActionName();
You can find everything about laravel Routes in the Laravel API: http://laravel.com/api/5.0/Illuminate/Routing/Router.html http://laravel.com/api/5.0/Illuminate/Routing.html
您可以在laravel API中找到有关laravel路由的所有内容:http://laravel.com/api/5.0/Illuminate/Routing/Router.html http://laravel.com/api/5.0/Illuminate/Routing.html
Retrieving The Request URI
检索请求URI
The path method returns the request's URI. So, if the incoming request is targeted at http://example.com/foo/bar
, the path method will return foo/bar
:
路径方法返回请求的URI。因此,如果传入请求的目标是http://example.com/foo/bar,那么path方法将返回foo/bar:
$uri = $request->path();
The is
method allows you to verify that the incoming request URI matches a given pattern. You may use the *
character as a wildcard when utilizing this method:
is方法允许您验证传入的请求URI是否与给定的模式匹配。使用此方法时,您可以使用*字符作为通配符:
if ($request->is('admin/*')) {
//
}
To get the full URL, not just the path info, you may use the url method on the request instance:
要获得完整的URL,而不仅仅是路径信息,您可以在请求实例上使用URL方法:
$url = $request->url();
#2
25
Using Laravel 5.1, you can use
使用Laravel 5.1,您可以使用
\Request::route()->getName()
#3
19
Found a way to find the current route name works for laravel v5 , v5.1.28 and v5.2.10
找到一种方法来找到laravel v5、v5.1.28和v5.2.10的当前路由名称
Namespace
名称空间
use Illuminate\Support\Facades\Route;
and
和
$currentPath= Route::getFacadeRoot()->current()->uri();
For Laravel laravel v5.3 you can just use:
对于Laravel的v5.3,你可以使用:
use Illuminate\Support\Facades\Route;
Route::currentRouteName();
#4
15
If you need url, not route name, you do not need to use/require any other classes:
如果您需要url,而不是路由名称,则不需要使用/需要任何其他类:
url()->current();
#5
10
If you want to select menu on multiple routes you may do like this:
如果你想在多条路线上选择菜单,你可以这样做:
<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>
Or if you want to select just single menu you may simply do like this:
或者,如果你只想选择一个菜单,你可以这样做:
<li class="{{ (Request::is('/users') ? 'active' : '') }}"><a href="{{url('/')}}"><i class="fa fa-envelope"></i> Users</a></li>
Also tested in Laravel 5.2
在Laravel 5.2中也进行了测试
Hope this help someone.
希望这帮助别人。
#6
7
Laravel 5.2 You can use
Laravel 5.2可以使用
$request->route()->getName()
It will give you current route name.
它会给你当前的路径名。
#7
5
You can use in template:
你可以在模板中使用:
<?php $path = Route::getCurrentRoute()->getPath(); ?>
<?php if (starts_with($path, 'admin/')) echo "active"; ?>
#8
5
Shortest way is Route facade \Route::current()->getName()
最短路径是路由facade \路由::current()->getName()
This also works in laravel 5.4.*
这也适用于laravel 5.4 *
#9
3
Now in Laravel 5.3
I am seeing that can be made similarly you tried:
在Laravel 5。3中,我发现可以做类似的东西
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
https://laravel.com/docs/5.3/routing#accessing-the-current-route
https://laravel.com/docs/5.3/routing accessing-the-current-route
#10
2
In a controller action, you could just do:
在控制器动作中,你可以做:
public function someAction(Request $request)
{
$routeName = $request->route()->getName();
}
$request
here is resolved by Laravel's service container.
这里的$request由Laravel的服务容器解决。
getName()
returns the route name for named routes only, null
otherwise (but you could still explore the \Illuminate\Routing\Route
object for something else of interest).
getName()只返回已命名路由的路由名称,否则为null(但是您仍然可以为其他感兴趣的东西探索\照明的路由对象)。
In other words, you should have your route defined like this to have "nameOfMyRoute" returned:
换句话说,您应该像这样定义您的路由,使“nameOfMyRoute”返回:
Route::get('my/some-action', [
'as' => 'nameOfMyRoute',
'uses' => 'MyController@someAction'
]);
#11
2
Request::path();
is better, and remember to Use Request;
要求::路径();是更好的,记得使用请求;
#12
2
In 5.2, you can use the request directly with:
在5.2中,您可以直接使用以下请求:
$request->route()->getName();
or via the helper method:
或透过辅助方法:
request()->route()->getName();
Output example:
输出的例子:
"home.index"
#13
1
I have used for getting route name in larvel 5.3
我在larvel 5.3中使用过获取路线名称
Request::path()
要求::路径()
#14
0
In a Helper file,
在一个辅助文件中,
Your can use Route::current()->uri()
to get current URL.
您可以使用Route::current()->uri()获取当前URL。
Hence, If you compare your route name to set active class on menu then it would be good if you use
因此,如果您将您的路由名称与在菜单上设置活动类进行比较,那么最好使用
Route::currentRouteName()
to get the name of route and compare
路由:currentRouteName()获取路由的名称并进行比较
#15
0
Looking at \Illuminate\Routing\Router.php
you can use the method currentRouteNamed()
by injecting a Router in your controller method. For example:
看着\照亮\路由\路由器。通过在控制器方法中注入路由器,您可以使用currentRouteNamed()方法。例如:
use Illuminate\Routing\Router;
public function index(Request $request, Router $router) {
return view($router->currentRouteNamed('foo') ? 'view1' : 'view2');
}
or using the Route facade:
或使用路线外立面:
public function index(Request $request) {
return view(\Route::currentRouteNamed('foo') ? 'view1' : 'view2');
}
You could also use the method is()
to check if the route is named any of the given parameters, but beware this method uses preg_match()
and I've experienced it to cause strange behaviour with dotted route names (like 'foo.bar.done'
). There is also the matter of performance around preg_match()
which is a big subject in the PHP community.
您也可以使用方法is()来检查路由是否被命名为任何给定的参数,但是要注意,这个方法使用preg_match(),我曾经经历过它会导致带有点路径名的奇怪行为(比如'foo.bar.done')。还有关于preg_match()的性能问题,这是PHP社区中的一个重要主题。
public function index(Request $request) {
return view(\Route::is('foo', 'bar') ? 'view1' : 'view2');
}