Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get controller and action name in Laravel 4

Tags:

php

laravel

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()
like image 395
user391986 Avatar asked Jul 20 '13 17:07

user391986


People also ask

How can I get current action name in Laravel?

echo Route::getActionName();

Where can I find controller 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.

How can I get route name in Laravel?

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

What is Single Action Controller in Laravel?

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.


2 Answers

Route::currentRouteAction()

returns e..g "RecordAPIController@show"

like image 163
malhal Avatar answered Sep 22 '22 12:09

malhal


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?

like image 32
fideloper Avatar answered Sep 23 '22 12:09

fideloper