Zend Framework 2 调度事件不会在操作前运行 [英] Zend Framework 2 dispatch event doesn't run before action

查看:29
本文介绍了Zend Framework 2 调度事件不会在操作前运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要帮助.我想在控制器的操作运行之前在 Zend Framework 2 中运行一个方法.我将我的方法放在 Module.php 的 onBootstrap 中,但它不会在操作启动之前运行.


I need some help. I want to run a method in Zend Framework 2 before the controller's action runs. I putted my method in Module.php's onBootstrap, but it doesn't run before action initated.

在 Module.php 中:

In Module.php:

public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);

    $app  = $e->getApplication();
    $em = $app->getEventManager();

    $em->attach(MvcEvent::EVENT_DISPATCH, function($e) {
        $controller = $e->getTarget();
        $controller->Init();
    });
}

我想将 Init() 方法运行到我的适配器将在操作运行之前初始化,但它不起作用,我总是收到此消息:
可捕获的致命错误:传递给 Application\Model\Members::__construct() 的参数 1 必须是 Zend\Db\Adapter\Adapter 的实例,给定 null,在 PATH\module\Application\src\Application\Controller 中调用\AdminController.php 第 39 行,定义在 PATH\module\Application\src\Application\Model\Members.php 第 17 行

I want to run the Init() method to my Adapter would be initialized before action runs but it didn't work and I always get this message:
Catchable fatal error: Argument 1 passed to Application\Model\Members::__construct() must be an instance of Zend\Db\Adapter\Adapter, null given, called in PATH\module\Application\src\Application\Controller\AdminController.php on line 39 and defined in PATH\module\Application\src\Application\Model\Members.php on line 17

成员类在应该运行的动作中,它的 __construct 需要有一个有效的 Adapter 对象,应该在 Init() 方法中初始化.

The members class is in the action which should run and its __construct need to have a valid Adapter object that should be initialized in Init() method.

有人可以帮我吗?非常感谢!

Could anyone help me? Thanks a lot!

推荐答案

尝试不同的方法:

我假设您的控制器扩展了 Zend\Mvc\Controller\AbstractActionController.在你的控制器中覆盖父级的 onDispatch 方法,做你需要做的事情:

I'm assuming your controller extends the Zend\Mvc\Controller\AbstractActionController. Override the parent's onDispatch method in your controller, to do what you need to do:

例如:

class YourController extends AbstractActionController {

    public function onDispatch($event){
        $this->Init();
        return parent::onDispatch($event);
    }
    //your other actions/init methods etc...

}

这篇关于Zend Framework 2 调度事件不会在操作前运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆