ZF2:如何在Module类中的事件上附加侦听器? [英] ZF2: How to attach listener on the event in the Module class?

查看:110
本文介绍了ZF2:如何在Module类中的事件上附加侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于给定的请求,我希望将Mvc的每个组件的basePath设置为相同.我的意思是,当我调用这些方法时,我想得到相同的结果,可以说'/spam/ham/':

I want to set a basePath to be the same for every component of my Mvc for a given request. I mean when I call these methods I want to get the same result, lets say '/spam/ham/':

echo $this->headLink()->prependStylesheet($this->basePath() . '/styles.css')  // $this->basePath() has to be '/spam/ham/'

$this->getServiceLocator()
     ->get('viewhelpermanager')
     ->get('headLink')
     ->rependStylesheet($this->getRequest()->getBasePath() . '/styles.css')   // $this->setRequest()->getBasePath() has to be /spam/ham/

如何为我已经发现的第一种情况设置basePath

How to set the basePath for the first case I have found already, here's my question. By the way, the original manual doesn't have any info I received from the answer.

现在第二个-basePath必须在Request中设置:

And now the second one - the basePath has to be set in the Request:

$this->getRequest()->getBasePath()

在这里,我找到了一些答案,实际上根本不起作用此处不推荐使用StaticEventManager,所以我用SharedEventManager进行了更改:

Here I found some answer that in fact doesn't work at all http://zend-framework-community.634137.n4.nabble.com/Setting-the-base-url-in-ZF2-MVC-td3946284.html. As said here StaticEventManager is deprecated so I changed it with SharedEventManager :

// In my Application\Module.php

namespace Application;
use Zend\EventManager\SharedEventManager

    class Module {
        public function init() {             

                $events = new SharedEventManager(); 
                $events->attach('bootstrap', 'bootstrap', array($this, 'registerBasePath')); 
            } 

            public function registerBasePath($e) { 

                $modules = $e->getParam('modules'); 
                $config  = $modules->getMergedConfig(); 
                $app     = $e->getParam('application'); 
                $request = $app->getRequest(); 
                $request->setBasePath($config->base_path); 
            } 
        } 
    }

然后在我的modules/Application/configs/module.config.php中添加:

'base_path' => '/spam/ham/' 

但是它不起作用.问题是:

But it desn't work. The problems are:

1)registerBasePath函数永远不会运行.但这是必须的.我已经在init函数中为侦听器附加了一个事件.

1) The run never comes to the registerBasePath function. But it has to. I've attached an event with the listener in the init function.

2)当我仅将SharedEventManager更改为SharedEventManager时,它碰巧出现在registerBasePath函数上,但是抛出了异常:

2) When I change SharedEventManager for just EventManager it happens to come to the registerBasePath function but an exeption is thrown:

Fatal error: Call to undefined method Zend\EventManager\EventManager::getParam()

我做错了什么?为什么程序的运行没有出现在registerBasePath函数中?如果这是全局设置basePath的唯一方法,那么如何正确设置呢?

What do I do wrong? Why the run of the program doesn't come to the registerBasePath function? If this is the only way to set the basePath globally then how to do it right?

推荐答案

我知道文档中缺少这些内容.但是您正确地解决了这个问题:

I know the documentation is lacking of these kinds of things. But you are right in the way to approach this:

  1. 早点(所以要自举)
  2. 从应用程序中获取请求
  3. 在请求中设置基本路径

文档缺少此信息,您引用的帖子已经很老了.最快,最简单的方法是使用onBootstrap()方法:

The docs are lacking this information and the post you refer to is quite old. The fastest and easiest way to do this is using the onBootstrap() method:

namespace MyModule;

class Module
{
    public function onBootstrap($e)
    {
        $app = $e->getApplication();
        $app->getRequest()->setBasePath('/foo/bar');
    }
}

如果要从配置中获取基本路径,可以在此处加载服务管理器:

If you want to grab the base path from your config, you can load the service manager there:

namespace MyModule;

class Module
{
    public function onBootstrap($e)
    {
        $app = $e->getApplication();
        $sm  = $app->getServiceManager();

        $config = $sm->get('config');
        $path   = $config->base_path;

        $app->getRequest()->setBasePath($path);
    }
}

这篇关于ZF2:如何在Module类中的事件上附加侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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