我似乎无法启动我的活动。这是我第一次和他们一起玩。如果你看的话,我会在
init
方法中创建一个事件,然后尝试在 onBootstrap
方法中触发它。该事件应该产生带有字符串的骰子,但事实并非如此。任何帮助将不胜感激。
namespace Application;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ControllerProviderInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\InitProviderInterface;
use Zend\ModuleManager\Feature\ViewHelperProviderInterface;
use Zend\ModuleManager\ModuleManagerInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\EventManager\EventInterface;
use Doctrine\DBAL\DriverManager;
class Module implements
AutoloaderProviderInterface,
ControllerProviderInterface,
BootstrapListenerInterface,
ServiceProviderInterface,
ConfigProviderInterface,
InitProviderInterface,
ViewHelperProviderInterface
{
public function init(ModuleManagerInterface $manager)
{
$eventManager = $manager->getEventManager();
$eventManager->attach('do',function($e){
$event = $e->getName();
$target = get_class($e->getTarget());
$params = $e->getParams();
$str = sprintf(
'Handled event \"%s\" on target \"%s\", with parameters %s',
$event,
$target,
json_encode($params)
);
die($str);
});
}
public function onBootstrap(EventInterface $e)
{
$this->attachEventManagerToModuleRouteListener($e);
$this->setupDoctrineConnectionMappings($e);
$eventManager = $e->getApplication()->getEventManager();
$eventManager->trigger('do',$this,array('j','o','n'));
}
public function getConfig()
{
return array_merge(
include __DIR__ . '/config/module.config.php',
include __DIR__ . '/config/routes.config.php'
);
}
public function getServiceConfig()
{
return include __DIR__ . '/config/services.config.php';
}
public function getControllerConfig()
{
return include __DIR__ . '/config/controllers.config.php';
}
public function getViewHelperConfig()
{
return include __DIR__ . '/config/view.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
),
),
);
}
private function attachEventManagerToModuleRouteListener(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
private function setupDoctrineConnectionMappings(MvcEvent $e)
{
$driver = $e->getApplication()->getServiceManager()->get('doctrine.connection.orm_default');
$platform = $driver->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
$platform->registerDoctrineTypeMapping('set', 'string');
}
}
您从
EventManager
获得的 ModuleManager
与您触发事件 EventManager
的 Application
的 EventManager
不同。由于在模块初始化期间应用程序尚不可用,因此您必须通过
do
将侦听器绑定到事件。
所以附加到应用程序的事件
SharedManager
会像这样
do
请注意,共享管理器需要预期触发事件的 EventManager 的标识符,在本例中是(通常是)
$sharedManager = $manager->getEventManager()->getSharedManager();
$sharedManager->attach(Application::class, 'do', function($e) {
// event code
});
的类名。