【问题标题】:Laravel: How to Get Current Route Name? (v5 ... v7)Laravel:如何获取当前路线名称? (v5 ... v7)
【发布时间】:2015-07-14 19:35:32
【问题描述】:

在 Laravel v4 中,我能够使用...获取当前路线名称...

Route::currentRouteName()

如何在 Laravel v5Laravel v6 中做到这一点?

【问题讨论】:

  • 我应该使用哪个命名空间来查找路由名称?我使用过 Illuminate\Support\Facades\Route 但结果为空。
  • 那是正确的类。您的路线可能没有指定名称。注意路由名和URI不一样。
  • 你为什么需要它?

标签: php laravel laravel-5 laravel-routing laravel-6


【解决方案1】:

试试这个

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

path 方法返回请求的 URI。所以,如果传入的请求是针对http://example.com/foo/bar,那么path方法会返回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();

【讨论】:

  • 你知道如何过滤这个,例如,如果一个人只想在视图中打印 api 路由api/...
  • Route::currentRouteName(); 完美:)
  • @Daniel Dewhurst:也许它适用于 v request()->route()->getName() 是要走的路。
  • 使用request() 辅助函数在视图中特别有用。 request()->route()->getName() 是最好的选择。
  • 我在 Laravel 8 版本中使用了所有 3 种解决方案,但它们都不起作用。 // echo $route = Route::current(); echo $name = Route::currentRouteName(); echo $action = Route::currentRouteAction();
【解决方案2】:

使用 Laravel 5.1,可以使用

\Request::route()->getName()

【讨论】:

  • 当您将它作为 {{ route(\Request::route()->getName()) }} 放在视图上时,这也有效。非常感谢!
【解决方案3】:

找到一种方法来查找适用于 laravel v5v5.1.28v5.2.10

的当前路线名称

命名空间

use Illuminate\Support\Facades\Route;

$currentPath= Route::getFacadeRoot()->current()->uri();

对于 Laravel laravel v5.3,您可以使用:

use Illuminate\Support\Facades\Route;

Route::currentRouteName();

【讨论】:

  • @Jonathan 我相信最好使用完整的命名空间以避免任何潜在的冲突。
  • 对 laravel 8 有什么建议吗?
【解决方案4】:

如果你想在多条路线上选择菜单,你可以这样做:

<li class="{{ (Request::is('products/*') || Request::is('products') || Request::is('product/*') ? 'active' : '') }}"><a href="{{url('products')}}"><i class="fa fa-code-fork"></i>&nbsp; Products</a></li>

或者,如果您只想选择单个菜单,您可以这样做:

<li class="{{ (Request::is('/users') ? 'active' : '') }}"><a href="{{url('/')}}"><i class="fa fa-envelope"></i>&nbsp; Users</a></li>

也在 Laravel 5.2

中进行了测试

希望这对某人有所帮助。

【讨论】:

  • 也在 Laravel 5.3 中测试过
  • 也在 Laravel 7.5.2 中测试过
  • 在 Laravel 5.7 中测试
【解决方案5】:

如果您需要 url,而不是 路由名称,则不需要使用/需要任何其他类:

url()->current();

【讨论】:

  • 这会返回错误:“调用非对象上的成员函数 current()”。 url() 返回一个字符串,而不是一个对象,所以我认为这不可能奏效。也许您正在考虑其他方法或对象,而不是 url()?
  • 不,我每天都用这个。检查official docs
  • 我明白了。这仅适用于5.2 或更高版本。但它相当不错。
【解决方案6】:

Laravel 5.2 可以使用

$request->route()->getName()

它会给你当前的路线名称。

【讨论】:

  • 这实际上是不正确的。 name() 方法将添加或更改名称,而 getName() 方法将返回它。
【解决方案7】:

在 5.2 中,您可以直接使用请求:

$request->route()->getName();

或通过辅助方法:

request()->route()->getName();

输出示例:

"home.index"

【讨论】:

  • 辅助方法是最好的。适用于 laravel-5.6
【解决方案8】:

最短的方法是 Route 门面 \Route::current()-&gt;getName()

这也适用于 laravel 5.4.*

【讨论】:

    【解决方案9】:

    访问当前路线

    在 Blade 模板中获取当前路由名称

    {{ Route::currentRouteName() }}
    

    更多信息https://laravel.com/docs/5.5/routing#accessing-the-current-route

    【讨论】:

    • 感谢您的正确回答,浪费了 30 分钟尝试无用的建议。
    【解决方案10】:

    在控制器操作中,您可以这样做:

    public function someAction(Request $request)
    {
        $routeName = $request->route()->getName();
    }
    

    $request 这里由 Laravel 的服务容器解析。

    getName() 仅返回 named routes 的路由名称,否则返回 null(但您仍然可以探索 \Illuminate\Routing\Route 对象以获取其他感兴趣的内容)。

    换句话说,您应该像这样定义您的路线以返回“nameOfMyRoute”:

    Route::get('my/some-action', [
        'as' => 'nameOfMyRoute',
        'uses' => 'MyController@someAction'
    ]);
    

    【讨论】:

      【解决方案11】:

      您可以在模板中使用:

      <?php $path = Route::getCurrentRoute()->getPath(); ?>
      <?php if (starts_with($path, 'admin/')) echo "active"; ?>
      

      【讨论】:

        【解决方案12】:

        您可以使用以下代码在刀片文件中获取路线名称

        request()->route()->uri
        

        【讨论】:

          【解决方案13】:

          现在在 Laravel 5.3 我看到你尝试过类似的方法:

          $route = Route::current();
          
          $name = Route::currentRouteName();
          
          $action = Route::currentRouteAction();
          

          https://laravel.com/docs/5.3/routing#accessing-the-current-route

          【讨论】:

            【解决方案14】:

            访问当前路线(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

            【讨论】:

              【解决方案15】:
              $request->route()->getName();
              

              【讨论】:

                【解决方案16】:

                Request::path();更好,记得use Request;

                【讨论】:

                  【解决方案17】:

                  在 laravel 7 或 8 中使用辅助函数

                  Get Current Route Name
                  
                  request()->route()->getName()
                  

                  要检查路由是否当前更好,最好使用宏为请求类创建自己的方法

                  AppServiceProviderboot 方法中:

                  use Illuminate\Http\Request;
                  public function boot()
                      {
                          Request::macro('isCurrentRoute', function ($routeNames) {
                              return in_array(
                                  request()->route()->getName(),
                                  is_array($routeNames) ? $routeNames : explode(",", $routeNames)
                              );
                          });
                      }
                  

                  您可以在刀片或控制器中使用此方法

                  request()->isCurrentRoute('foo') // string route
                  request()->isCurrentRoute(['bar','foo']) //array routes
                  request()->isCurrentRoute('blogs,foo,bar') //string route seperated by comma
                  

                  你可以使用内置的 laravel 路由方法

                  route()->is('home');
                  route()->is('blogs.*'); //using wildcard
                  

                  【讨论】:

                  • 在这种情况下检查路由名称应该是request()-&gt;routeIs('home')request()-&gt;is('home')。后者Determines if the current request URI matches a pattern,而前者Determines if the route name matches a given pattern。所以我建议使用前routeIs 方法。绝对不是route()-&gt;is('home')。您将不会收到此类方法错误或 route() 方法,期望给出 1 个参数 0。 [来源][laravel.com/api/8.x/Illuminate/Http/Request.html]
                  【解决方案18】:

                  在我看来,最简单的解决方案是使用这个助手:

                  request()->route()->getName()
                  

                  有关文档,请参阅this link

                  【讨论】:

                  • 我认为它是刀片中更好的选择。
                  【解决方案19】:

                  您可以做的第一件事是在 class 的顶部导入 namespace

                  use Illuminate\Support\Facades\Route;
                  

                  laravel v8

                  $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();
                  

                  【讨论】:

                    【解决方案20】:

                    我在 larvel 5.3 中用于获取路由名称

                    Request::path()

                    【讨论】:

                      【解决方案21】:

                      查看\Illuminate\Routing\Router.php,您可以通过在控制器方法中注入路由器来使用方法currentRouteNamed()。例如:

                      use Illuminate\Routing\Router;
                      public function index(Request $request, Router $router) {
                         return view($router->currentRouteNamed('foo') ? 'view1' : 'view2');
                      }
                      

                      或使用 Route 门面:

                      public function index(Request $request) {
                         return view(\Route::currentRouteNamed('foo') ? 'view1' : 'view2');
                      }
                      

                      您也可以使用方法is() 来检查路由是否被命名为任何给定的参数,但要注意此方法使用preg_match(),我已经体验过它会导致带有点路由名称的奇怪行为(如@ 987654327@)。还有preg_match()周围的性能问题 这是 PHP 社区的一个大主题。

                      public function index(Request $request) {
                          return view(\Route::is('foo', 'bar') ? 'view1' : 'view2');
                      }
                      

                      【讨论】:

                        【解决方案22】:

                        解决方案:

                        $routeArray = app('request')->route()->getAction();
                        $controllerAction = class_basename($routeArray['controller']);
                        list($controller, $route) = explode('@', $controllerAction);
                        echo $route;
                        

                        【讨论】:

                          【解决方案23】:

                          您可以使用以下方法:

                          Route::getCurrentRoute()->getPath();
                          

                          在 Laravel 版本 > 6.0 中,您可以使用以下方法:

                          $route = Route::current();
                          
                          $name = Route::currentRouteName();
                          
                          $action = Route::currentRouteAction();
                          

                          【讨论】:

                          • 获取错误:调用未定义的方法 Laravel\Lumen\Routing\Router::current()
                          【解决方案24】:

                          在控制器中访问当前路由名称

                          ie - http://localhost/your_project_name/edit

                          $request->segment(1);  // edit
                          

                          (或)

                          $request->url();  // http://localhost/your_project_name/edit
                          

                          【讨论】:

                            【解决方案25】:

                            使用 laravel 助手和魔法方法

                            request()->route()->getName()
                            

                            【讨论】:

                              【解决方案26】:

                              在帮助文件中,

                              您可以使用Route::current()-&gt;uri() 获取当前网址。

                              因此,如果您将路线名称与在菜单上设置活动类进行比较,那么使用

                              Route::currentRouteName()获取路由名称并进行比较

                              【讨论】:

                                【解决方案27】:

                                由于某些原因,我无法使用这些解决方案中的任何一个。所以我只是在web.php 中将我的路由声明为$router-&gt;get('/api/v1/users', ['as' =&gt; 'index', 'uses' =&gt; 'UserController@index']) 并在我的控制器中使用$routeName = $request-&gt;route()[1]['as']; 获得了路由的名称,其中$request\Illuminate\Http\Request $requestindexUserController 方法中的类型提示参数

                                使用流明 5.6。希望它会对某人有所帮助。

                                【讨论】:

                                  【解决方案28】:

                                  你可以使用这行代码:url()-&gt;current()

                                  在刀片文件中:{{url()-&gt;current()}}

                                  【讨论】:

                                    【解决方案29】:

                                    有很多方法可以做到这一点。您可以输入:

                                    \Illuminate\Support\Facades\Request::route()->getName()
                                    

                                    获取路线名称。

                                    【讨论】:

                                      【解决方案30】:

                                      如果直接查看需要路由名称或 url id,没有人有答案 直接查看路线名称

                                      $routeName = Request::route()->getName();
                                      

                                      查看 url 中的 id

                                      $url_id = Request::segment(2);
                                      

                                      【讨论】:

                                        猜你喜欢
                                        • 2015-09-14
                                        • 2020-04-02
                                        • 2016-05-08
                                        • 2016-08-03
                                        • 2015-12-04
                                        • 2020-04-19
                                        • 2013-08-20
                                        • 1970-01-01
                                        • 2021-10-24
                                        相关资源
                                        最近更新 更多