How to get action name in laravel?

by tressie.damore , in category: PHP Frameworks , 6 months ago

How to get action name in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by haylee.mertz , 5 months ago

@tressie.damore 

In Laravel, you can get the current action name using the Action facade or by using the action method on the Route facade.


Option 1: Using the Action facade:

1
2
3
use IlluminateSupportFacadesAction;

$action = Action::current()->getName();


Option 2: Using the action method on the Route facade:

1
2
3
use IlluminateSupportFacadesRoute;

$action = Route::currentRouteAction();


Note: The Route::currentRouteAction() method will return the fully qualified action name including the controller namespace and the method name. If you only need the name of the method, you can extract it using the class_basename helper function:

1
2
3
4
5
use IlluminateSupportFacadesRoute;
use IlluminateSupportStr;

$action = Route::currentRouteAction();
$methodName = Str::afterLast($action, '@');


Make sure to import the necessary classes and helpers at the top of your file.