如何从ZF2模块中的Route获取参数?(类模块,函数 onBootstrap()) [英] How to get parameters from Route in ZF2 module? (class Module, function onBootstrap())

查看:15
本文介绍了如何从ZF2模块中的Route获取参数?(类模块,函数 onBootstrap())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在控制器中,我可以使用 $this->params()->fromRoute('param1')
我如何在 Module OnBootstrap() 函数中做到这一点?

In controller I can get parameters from route using $this->params()->fromRoute('param1')
How can I do that in Module OnBootstrap() function?

namespace MyModule;

use Zend\EventManager\EventInterface;

class Module
{
    public function onBootstrap(EventInterface $event)
    {
        // here I need to get parameter from route
    }
}

推荐答案

正如 user2257808 在他的评论中所说,onBootstrap 在路由发生之前被调用,因此没有任何 RouteMatch 可以获取.他建议附加到 EVENT_RENDER,这对您来说可能为时已晚.

As user2257808 said in his comment, onBootstrap is called before routing takes place, so there is not any RouteMatch to get. He suggested attaching to EVENT_RENDER, that may be too late in your case.

我会做这样的事情,附加到 MvcEvent::EVENT_DISPATCH.

I would do something like this, attaching to MvcEvent::EVENT_DISPATCH.

MyModule\Module.php

class Module {
    public function onBootstrap(MvcEvent $e) {

        $e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_DISPATCH,
            function($e){
               var_dump($e->getRouteMatch());
                exit;
            }
         );

    }
}

这篇关于如何从ZF2模块中的Route获取参数?(类模块,函数 onBootstrap())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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