21

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(); } } 
2
  • I think the method is removed because routes will be created with annotations in L5. Commented Nov 10, 2014 at 9:36
  • 1
    @arjan The default in L5 is now the normal routes, the same as L4. Annotations are now optional in L5. Commented Nov 10, 2014 at 10:45

13 Answers 13

27

To get action name, you need to use:

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

and not

echo Route::getActionName(); 
Sign up to request clarification or add additional context in comments.

2 Comments

For me the result is App\Http\Controllers\AdsController@create, so it is not only action name. Is it possible to get only it?
getting error - Class 'App\Http\Controllers\Route' not found
24

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

3 Comments

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
This is WAAAAAAAAAAAAAAAAAAAAAY too automagical to be comfortable with. I like to KNOW EXACTLY what's happening.
@SzczepanHołyszewski - Method injection is a standard way Laravel 5 works. Nothing automagical about it - just standard dependency injection.
11

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

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

or with a facade ...

Route::getActionMethod() 

1 Comment

how to use it in Lumen 8.x in middleware? Kindly suggest Thanks.
7

To get action name only (without controller name):

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

1 Comment

how to use it in Lumen 8.x in middleware? Kindly suggest Thanks.
4

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

Comments

3

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

Comments

2

To get only action name in Laravel 5.4

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

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

Comments

2

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.

1 Comment

It works, but how to get controller name also, please suggest me. Thanks.
1

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.

Comments

1

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

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

1 Comment

This seems to not work in Lumen 5.7. Can anyone tell me why that is? It seems that Illuminate/Routing/Router does not get installed when you install laravel/lumen? Am I wrong in saying this? I want to get the right controller version but I can't change action since $request-route() is an array and there is not member function in that array that calls getAction() method.
1

This working perfectly good for me.

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

Comments

0
$request->route()->getAction()['prefix'] // return 'api' 

Comments

0

I used this, and it is working in view.

A few options.

Pass variables to the view from your controller. Set a global view variable use view composer. You need to choose one of these depending on your use case.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.