我想設置一個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