I am new to Laravel, and i want to get name of requested controller and action in beforefilter middelware.
Thanks, DJ
For route specific filtering place the 'Path\To\Middleware', within the middleware array within RouteServiceProvider. php within the App\Providers folder. You can also access the route object via app()->router->getCurrentRoute() .
Laravel incorporates a middleware that confirms whether or not the client of the application is verified. If the client is confirmed, it diverts to the home page otherwise, it diverts to the login page. All controllers in Laravel are created in the Controllers folder, located in App/Http/Controllers.
In Laravel, a controller is in the 'app/Http/Controllers' directory. All the controllers, that are to be created, should be in this directory. We can create a controller using 'make:controller' Artisan command.
Laravel 5.6:
class_basename(Route::current()->controller);
Laravel 5.5 and lower:
You can retrieve the current action name with Route::currentRouteAction()
. Unfortunately, this method will return a fully namespaced class name. So you will get something like:
App\Http\Controllers\FooBarController@method
Then just separate method name and controller name:
$currentAction = \Route::currentRouteAction();
list($controller, $method) = explode('@', $currentAction);
// $controller now is "App\Http\Controllers\FooBarController"
$controller = preg_replace('/.*\\\/', '', $controller);
// $controller now is "FooBarController"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With