在Laravel 4中获取控制器和操作名称

use*_*986 6 php laravel

我正在尝试访问当前控制器的名称和当前方法,以将其作为变量传递给我的视图.我已经尝试了几种方法,我在网上发现但是它们没有用,所以我认为它们是用于Laravel 3.

这是我尝试过的

Request::$route->controller
Run Code Online (Sandbox Code Playgroud)

Access to undeclared static property: Illuminate\Support\Facades\Request::$route
Run Code Online (Sandbox Code Playgroud)

Request::route()->controller
Run Code Online (Sandbox Code Playgroud)

Call to undefined method Illuminate\Http\Request::route()
Run Code Online (Sandbox Code Playgroud)

mal*_*hal 10

Route::currentRouteAction()
Run Code Online (Sandbox Code Playgroud)

返回e..g"RecordAPIController @ show"


fid*_*per 7

尝试根据路线文档命名您的路线,然后使用$name = Route::currentRouteName();

在什么条件下你不知道什么时候控制器/路由被解雇?你能告诉我们你的用例吗?


sea*_*saw 2

每个 Request 中的实例Router都有以下可能有用的方法:

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 如何访问“Router”实例? (6认同)