Get controller name and actions in Laravel 4

I am trying to access the name of the current controller and the current method in order to pass it to my view as a variable. I tried several methods from pointers that I found on the Internet, but they do not work, so I assume they are for Laravel 3.

Here is what I 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() 
+6
source share
5 answers

The Router instance in each request has the following methods that may be useful:

 /** * Retrieve the entire route collection. * * @return \Symfony\Component\Routing\RouteCollection */ public function getRoutes() { return $this->routes; } /** * Get the current request being dispatched. * * @return \Symfony\Component\HttpFoundation\Request */ public function getRequest() { return $this->currentRequest; } /** * Get the current route being executed. * * @return \Illuminate\Routing\Route */ public function getCurrentRoute() { return $this->currentRoute; } /** * Get the controller inspector instance. * * @return \Illuminate\Routing\Controllers\Inspector */ public function getInspector() { return $this->inspector ?: new Controllers\Inspector; } 
+2
source
 Route::currentRouteAction() 

returns e..g "RecordAPIController @show"

+10
source

Try assigning routes according to, and then using $name = Route::currentRouteName();

In what state do you not know that the controller / route was fired ahead of time? Can you tell us what is your use case?

+7
source

Laravel used "Request" as an alias for "Illuminate \ Support \ Facades \ Request" (can be found in app.php). My advice is to avoid using "Query" as the model / controller / view name.

0
source

You can use the basic constants / functions of php and not Laravel:

 __LINE__, __FILE__, __FUNCTION__, __CLASS__, __METHOD__ 

will give you the current line, file, function, class or method

 get_class() 

provides the current class

-3
source

Source: https://habr.com/ru/post/949901/


All Articles