Question:
How can I identify which method was called in my controller
constructor ?
I'm using Laravel 5.3
and my controller
follows the resource
pattern ( index, show, edit
…).
Answer:
In the route files, it is configured:
Route::get('/paginas', 'PaginasController@index');
and in the builder use:
$controlerAndAction = \Route::currentRouteAction();
exit:
string(44) "App\Http\Controllers\PaginasController@index"
Where
$controlerAndAction = \Route::current()->getActionName();
exit:
string(44) "App\Http\Controllers\PaginasController@index"
To get the action
which in this case is the index
a function like this solves:
$actionName = function ($value)
{
return substr($value, strrpos($value, '@') + 1 );
};
echo $actionName($controlerAndAction);
I didn't see anything in the documentation directly, it even has a getAction
but it doesn't bring the action directly ( Action
) and often, depending on the configuration of the route ( Route
), it brings its value to zero .