0

在 na 动作中获取动作名称很简单——我可以使用“actionName”参数。但是有没有办法在Controller的构造函数中获取动作名称呢?

    public function __construct()
    {
        parent::__construct();
        $action = $this->params('actionName');
    }
4

1 回答 1

1

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);
        
    }
}
于 2021-09-21T04:29:48.447 回答