【问题标题】:Laravel 5 how to get route action name?Laravel 5如何获取路线动作名称?
【发布时间】:2023-03-10 12:29:01
【问题描述】:

我正在尝试获取当前的路线操作,但我不确定如何去做。在 Laravel 4 中,我使用的是 Route::currentRouteAction(),但现在有点不同了。

我正在尝试在我的控制器中执行Route::getActionName(),但它一直给我找不到方法。

<?php namespace App\Http\Controllers;

use Route;

class HomeController extends Controller
{
    public function getIndex()
    {
        echo 'getIndex';
        echo Route::getActionName();
    }
}

【问题讨论】:

  • 我认为该方法已被删除,因为路由将在 L5 中使用注释创建。
  • @arjan L5 中的默认路由现在是普通路由,与 L4 相同。注释现在在 L5 中是可选的。

标签: php laravel laravel-5


【解决方案1】:

我用过这个,它在视图中工作。

几个选项。

将变量从控制器传递给视图。 设置全局视图变量 使用视图作曲家。 您需要根据自己的用例选择其中一种。

Route::getCurrentRoute()->getAction();
Route::currentRouteAction();
Route::currentRouteName();

【讨论】:

    【解决方案2】:

    这对我来说非常好。

    $request->route()->getActionMethod()
    

    【讨论】:

      【解决方案3】:
      $request->route()->getAction()['prefix'] // return 'api'
      

      【讨论】:

        【解决方案4】:

        在 Laravel 5.5 中,如果您只想要方法/动作名称,即显示、编辑、自定义方法等...请执行此操作

        Route::getCurrentRoute()->getActionMethod() 
        

        无需使用explode 或list 来获取要调用的实际方法。感谢 Laravel 想到这一点。

        【讨论】:

        • 它有效,但如何获取控制器名称也请建议我。谢谢。
        【解决方案5】:

        您可以使用从请求本身获取控制器详细信息

        $request->route()->getAction()
        

        【讨论】:

        • 这似乎不适用于 Lumen 5.7。谁能告诉我这是为什么?安装 laravel/lumen 时似乎没有安装 Illuminate/Routing/Router?我这样说有错吗?我想获得正确的控制器版本,但我无法更改操作,因为 $request-route() 是一个数组,并且该数组中没有调用 getAction() 方法的成员函数。
        【解决方案6】:

        在 Laravel 5.4 中仅获取动作名称

        explode('@', Route::getCurrentRoute()-&gt;getActionName())[1]

        找不到更好的方法,在视图中使用,在一行中......

        【讨论】:

          【解决方案7】:

          仅获取您可以使用的方法名称...

          $request->route()->getActionMethod()
          

          或带有门面......

          Route::getActionMethod()
          

          【讨论】:

          • Lumen 8.x 中间件如何使用?请建议谢谢。
          【解决方案8】:

          仅获取动作名称(没有控制器名称):

          list(, $action) = explode('@', Route::getCurrentRoute()->getActionName());
          

          【讨论】:

          • Lumen 8.x 中间件如何使用?请建议谢谢。
          【解决方案9】:

          要在中间件上获取路由操作名称,我会这样做:

          <?php
          namespace App\Http\Middleware;
          
          use Closure;
          use Illuminate\Routing\Router;
          
          class HasAccess {
          
              protected $router;
          
              public function __construct(User $user, Router $router)
              {
                  $this->router = $router;
              }
          
              public function handle($request, Closure $next)
              {
                  $action_name = $this->router->getRoutes()->match($request)->getActionName();
                  //$action_name will have as value 'App\Http\Controllers\HomeController@showWelcome'
                  //Now you can do what you want whit the action name 
                  return $next($request);
              }
          }
          

          编辑:你不会得到受此中间件保护的路由:(

          【讨论】:

            【解决方案10】:

            相反

            use Illuminate\Routing\Route;
            

            使用这个

            use Illuminate\Support\Facades\Route;
            

            如果要获取路由的别名,可以使用:

            Route::getCurrentRoute()->getName()
            

            【讨论】:

            • 不适合我。我正在使用 Laravel 8。谢谢。
            【解决方案11】:

            对于 Laravel 5.1 使用:

            $route = new Illuminate\Routing\Route();
            $route->getActionName(); // Returns App\Http\Controllers\MyController@myAction
            $route->getAction(); // Array with full controller info
            

            这个类中有很多有用的方法。只需检查代码以了解更多详细信息。

            【讨论】:

              【解决方案12】:

              在 Laravel 5 中,您应该使用方法或构造函数注入。这会做你想做的事:

              <?php namespace App\Http\Controllers;
              
              use Illuminate\Routing\Route;
              
              class HomeController extends Controller
              {
                  public function getIndex(Route $route)
                  {
                      echo 'getIndex';
                      echo $route->getActionName();
                  }
              }
              

              【讨论】:

              • 这很有效,很好,还有一个问题,如何在中间件中做到这一点。例如,我想根据路线在我的布局中自动设置“视图”。我尝试在 handle__construct 方法中做同样的事情,但它不起作用。 Unresolvable dependency resolving [Parameter #0 [ &lt;required&gt; $methods ]] in class Illuminate\Routing\Route
              • 这是 WAAAAAAAAAAAAAAAAAAAAAY 太自然而无法适应。我喜欢确切地知道发生了什么。
              • @SzczepanHołyszewski - 方法注入是 Laravel 5 工作的标准方式。它没有什么自动的 - 只是标准的依赖注入。
              【解决方案13】:

              要获取动作名称,您需要使用:

              echo Route::getCurrentRoute()->getActionName();
              

              而不是

              echo Route::getActionName();
              

              【讨论】:

              • 对我来说结果是App\Http\Controllers\AdsController@create,所以它不仅仅是动作名称。是不是只能得到它?
              • 出现错误 - 找不到类 'App\Http\Controllers\Route'
              猜你喜欢
              • 2015-02-12
              • 1970-01-01
              • 2019-07-23
              • 2015-07-14
              • 2015-04-26
              • 1970-01-01
              • 2016-07-11
              • 2016-05-09
              • 2021-10-24
              相关资源
              最近更新 更多