在 na 动作中获取动作名称很简单——我可以使用“actionName”参数。但是有没有办法在Controller的构造函数中获取动作名称呢?
public function __construct()
{
parent::__construct();
$action = $this->params('actionName');
}
在 na 动作中获取动作名称很简单——我可以使用“actionName”参数。但是有没有办法在Controller的构造函数中获取动作名称呢?
public function __construct()
{
parent::__construct();
$action = $this->params('actionName');
}
Controller-Class 中的构造函数还不知道调用了哪个动作。但是,您可以在AbstractActionController.php中扩展onDispatch
-method,该方法在调用任何操作之前但在调用 -method 之后调用。像这样的东西:__construct()
<?php
...
use Laminas\Mvc\MvcEvent; # <-- add this
class YourController extends AbstractActionController
{
public function onDispatch(MvcEvent $e)
{
$this->action = $this->params()->fromRoute('action');
// Or somethink like e.g.:
if ($this->params()->fromRoute('action') == 'foo') {
// do something
}
return parent::onDispatch($e);
}
}