I'm trying to access the name of the current controller and the current method to pass it to my view as a variable. I've tried several ways from pointers I found online but they don't work so I assume they were for Laravel 3.
Here is what I've tried
Request::$route->controller
gives
Access to undeclared static property: Illuminate\Support\Facades\Request::$route
and
Request::route()->controller
gives
Call to undefined method Illuminate\Http\Request::route()
echo Route::getActionName();
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.
You may use the current, currentRouteName, and currentRouteAction methods on the Route facade to access information about the route handling the incoming request: $route = Route::current(); $name = Route::currentRouteName(); $action = Route::currentRouteAction();
If you need to structure a specific module that has one action, then the single-action controller comes into play. Firstly, define a single __invoke method in your controller : <? php.
Route::currentRouteAction()
returns e..g "RecordAPIController@show"
Try naming your routes as per the route docs and then using $name = Route::currentRouteName();
In what condition do you not know what Controller / Route is being fired ahead of time? Can you let us know your use case is?
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