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.

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

<?php namespace App\Http\Controllers;

use Route;

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

10 Answers 正确答案 见下面的红字部分

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

<?php namespace App\Http\Controllers;

use Illuminate\Routing\Route;

class HomeController extends Controller
{
    public function getIndex(Route $route)
    {
        echo 'getIndex';
        echo $route->getActionName();
    }
}
  • This works, great, one more question, how to do this in middleware. For instance I want to auto set the "view" in my layout based on the route. I tried doing the same thing there in the handle and __construct method but it's not working. Unresolvable dependency resolving [Parameter #0 [ <required> $methods ]] in class Illuminate\Routing\Route – Rob Nov 11 '14 at 8:54
  • This is WAAAAAAAAAAAAAAAAAAAAAY too automagical to be comfortable with. I like to KNOW EXACTLY what's happening. – Szczepan Hołyszewski Apr 21 '17 at 15:35
  • 1
    @SzczepanHołyszewski - Method injection is a standard way Laravel 5 works. Nothing automagical about it - just standard dependency injection. – Laurence Apr 22 '17 at 21:01

To get action name, you need to use:

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

list(, $action) = explode('@', Route::getCurrentRoute()->getActionName());
得到控制器 和 action

$currentAction = \Route::currentRouteAction(); list($controller, $method) = explode('@', $currentAction); // $controller now is "App\Http\Controllers\FooBarController" $controller = preg_replace('/.*\\\/', '', $controller);

and not

echo Route::getActionName();

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 :(

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

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

or with a facade ...

Route::getActionMethod()

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

To get action name only (without controller name):

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

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

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.

For Laravel 5.1 use:

$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.


来自   https://stackoverflow.com/questions/26840278/laravel-5-how-to-get-route-action-name