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

Asked

Viewed 83 times

3

How do I stop at builder of my controller identify which method was called?

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

  • 1

    Just one question? Why do you need this to happen?

1 answer

2


In the route files, it is configured:

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

and in the builder utilize:

$controlerAndAction = \Route::currentRouteAction();

exit:

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

or

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

exit:

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

To catch 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, until there’s a getAction but, does not bring direct action (Action) and often depending on the route configuration (Route) tail null its value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.