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

前端 未结 1 1696
慢半拍i
慢半拍i 2021-02-07 08:25

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

Thanks, DJ

相关标签:
1条回答
  • 2021-02-07 09:02

    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"
    
    0 讨论(0)
提交回复
热议问题