我想设置一个basePath
对于给定请求的我的Mvc的每个组件都是一样的。我的意思是,当我调用这些方法我想获得相同的结果,可以说'/spam/ham/'
:ZF2:如何在Module类中的事件上附加监听器?
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
对于第一种情况,我发现已经,here's my question。顺便说一句,原来的手册没有我从答案中收到的任何信息。
而现在的第二个 - 在basePath
已在Request
进行设置:
$this->getRequest()->getBasePath()
在这里,我找到了一些答案,其实完全不http://zend-framework-community.634137.n4.nabble.com/Setting-the-base-url-in-ZF2-MVC-td3946284.html工作。至于说hereStaticEventManager
被弃用,所以我用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/'
但desn't工作。问题是:
1)运行永远不会进入registerBasePath
函数。但它必须。我在init
函数中附加了一个事件。
2)当我改变SharedEventManager
只是EventManager
碰巧来到registerBasePath
功能,但一个exeption抛出:
Fatal error: Call to undefined method Zend\EventManager\EventManager::getParam()
我该怎么办错了吗?为什么程序的运行没有进入registerBasePath
函数?如果这是全球设置basePath
的唯一方法,那么如何做到这一点?
非常感谢你非常清楚的解释! – Green