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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17764995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:09:53  来源:igfitidea点击:

get controller and action name in Laravel 4

phplaravel

提问by user391986

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.

我正在尝试访问当前控制器的名称和当前方法以将其作为变量传递给我的视图。我从网上找到的指针中尝试了几种方法,但它们不起作用,所以我认为它们适用于 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()

采纳答案by searsaw

The Routerinstance in each Request has the following methods that may be of use:

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

/**
 * 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;
}

回答by malhal

Route::currentRouteAction()

returns e..g "RecordAPIController@show"

返回例如“RecordAPIController@show”

回答by fideloper

Try naming your routes as per the route docsand then using $name = Route::currentRouteName();

尝试根据路线文档命名您的路线,然后使用$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?

在什么情况下你不知道提前触发了什么控制器/路由?你能告诉我们你的用例是什么吗?

回答by Zcythe

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

Laravel 使用“Request”作为 'Illuminate\Support\Facades\Request' 的别名(可以在 app.php 中找到)。我的建议是避免使用“请求”作为您的模型/控制器/视图名称。

回答by Leon

You could use basic php constants / functions, rather than Laravel:

您可以使用基本的 php 常量/函数,而不是 Laravel:

__LINE__, __FILE__, __FUNCTION__, __CLASS__, __METHOD__

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

将为您提供当前行、文件、函数、类或方法

get_class()

gives you the current class

给你当前的课程