php Laravel 5 如何获取路由操作名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26840278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:04:41  来源:igfitidea点击:

Laravel 5 how to get route action name?

phplaravellaravel-5

提问by Rob

I'm trying to get the current route action, but I'm not sure how to go about it. In Laravel 4 I was using Route::currentRouteAction()but now it's a bit different.

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

I'm trying to do Route::getActionName()in my controller but it keeps giving me method not found.

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

<?php namespace App\Http\Controllers;

use Route;

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

回答by Marcin Nabia?ek

To get action name, you need to use:

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

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

and not

并不是

echo Route::getActionName();

回答by Laurence

In Laravel 5 you should be using Method or Constructor injection. This will do what you want:

在 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();
    }
}

回答by Limon Monte

To get action name only (without controller name):

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

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

回答by Ilario Engler

To get only the method name you can use ...

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

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

or with a facade ...

或者有一个门面......

Route::getActionMethod()

回答by leoalmar

Instead

反而

use Illuminate\Routing\Route;

Use this

用这个

use Illuminate\Support\Facades\Route;

If you want to get the alias of the route, you can use:

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

Route::getCurrentRoute()->getName()

回答by CagunA

To get the route action name on Middleware i do that:

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

<?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);
    }
}

EDIT: You will don't get the routes which are protected by this middleware :(

编辑:您将不会获得受此中间件保护的路由:(

回答by greaterKing

In Laravel 5.5 if you just want the method/action name i.e. show, edit, custom-method etc... do this

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

Route::getCurrentRoute()->getActionMethod() 

No need to use explode or list to get the actual method to be called. Thanks to Laravel who thought of this.

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

回答by Todor Todorov

For Laravel 5.1 use:

对于 Laravel 5.1 使用:

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

There are lots of useful methods in this class. Just check the code for more details.

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

回答by Gediminas

To get only action name in Laravel 5.4

在 Laravel 5.4 中只获取动作名称

explode('@', Route::getCurrentRoute()->getActionName())[1]

explode('@', Route::getCurrentRoute()->getActionName())[1]

Can't find any better way, to use in view, in one line...

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

回答by Seadon Francis Pinto

You may use to get the controller details form the request itself

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

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