php CakePHP 获取动作名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24608699/
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-08-25 17:23:45  来源:igfitidea点击:

CakePHP get action name

phpcakephpcontrolleraction

提问by arik

In CakePHP, it is possible to get the called function string using the

在 CakePHP 中,可以使用

$this->action

syntax. It returns the literal string of whatever is called, so if the URL is /do_this, it returns do_this, and if it's doThisit'll return doThis. Regardless of the called method's real name.

句法。它返回的任何被称为文字串,所以如果URL /do_this,它返回do_this,并且如果它是doThis,它会返回doThis。与被调用方法的真实名称无关。

What I am looking for, on the other hand, is the called method's actual name, no matter the URL syntax.

另一方面,我正在寻找的是被调用方法的实际名称,无论 URL 语法如何。

Is there a way to find it out?

有没有办法找出来?

I'd preferably be able to do this in the beforeFiltermethod.

我最好能够在beforeFilter方法中做到这一点。

回答by burzum

You should use the request object.

您应该使用请求对象。

CakePHP 3.3 and below

CakePHP 3.3 及以下

$this->request->params['action'];

Since 3.4

自 3.4

$this->request->getParam('action');

I think this should contain the real method name that was called. CakePHPs router resolves the string URL to a controller / action pair and other args, all of that ends up in the request object. Read the documentation and do debug($this->request);in your beforeFilter() to see what else is there.

我认为这应该包含被调用的真实方法名称。CakePHPs 路由器将字符串 URL 解析为控制器/动作对和其他参数,所有这些都以请求对象结束。阅读文档并debug($this->request);在 beforeFilter() 中执行以查看还有什么。

回答by Fernando Herrero Peletero

In CakePHP 2 you can use $this->action, in CakePHP 3 you must use $this->request->params['action']

在 CakePHP 2 中你可以使用 $this->action,在 CakePHP 3 中你必须使用 $this->request->params['action']

回答by thanassis

The paramsarray (CakePHP >= 3.4) is deprecated The correct way to get the current action within a controller is :

所述params阵列(CakePHP的> = 3.4)已过时的正确的方式来获得当前动作中的控制器是:

$currentAction = $this->request->getParam('action');

回答by visualex

Have you taken a look at this? Retrieving the name of the current function in phpThis obviously will not work in the beforeFilter. You can set a variable: private $action_name in the Controller and set it from within the methods and use it afterwards, in afterFilter

你看过这个吗? 在php中检索当前函数的名称这在beforeFilter中显然是行不通的。您可以在 Controller 中设置一个变量:private $action_name 并从方法中设置它,然后在 afterFilter 中使用它