How to identify in the constructor the method that was called?

3

How do I in the constructor of my controller identify which method was called?

I'm using Laravel 5.3 and my controller follows the default resource ( index, show, edit ...).

    
asked by anonymous 20.12.2016 / 15:23

1 answer

2

In the route files, it is configured:

Route::get('/paginas', 'PaginasController@index');    

and constructor use:

$controlerAndAction = \Route::currentRouteAction();

output:

string(44) "App\Http\Controllers\PaginasController@index"

or

$controlerAndAction = \Route::current()->getActionName();

output:

string(44) "App\Http\Controllers\PaginasController@index"

To get the action that in the case is the index a function like this resolves:

$actionName = function ($value)
{
   return substr($value, strrpos($value, '@') + 1 );
};
echo $actionName($controlerAndAction);

I have not seen anything in the documentation directly, until it has a getAction but it does not bring the action directly ( Action ) and often depending on the configuration of the route ( Route ) brings null its value.

    
20.12.2016 / 15:45