Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get name of requested controller and action in middleware Laravel

I am new to Laravel, and i want to get name of requested controller and action in beforefilter middelware.

Thanks, DJ

like image 330
Daljeet Singh Avatar asked May 25 '15 16:05

Daljeet Singh


People also ask

How do I check my middleware route?

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() .

Can we apply middleware on controller in Laravel?

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.

Where are controllers file located in Laravel?

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.


1 Answers

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"
like image 176
Limon Monte Avatar answered Oct 13 '22 00:10

Limon Monte