Zend Framework 2 : Move out your listeners from Module class
来源:互联网 发布:美丽折淘宝客辅助器 编辑:程序博客网 时间:2024/06/05 23:03
As we already knew. We can have ‘listeners’ with array callback that placed on Module class which can be called via onBootstrap() method. When our application is growing, to many methods on Module class will make project maintenance take harder, and make our application less-readable.
Here is a sample if we don’t have move out the method yet :
Here is a sample if we don’t have move out the method yet :
class Module{ public function onBootstrap(MvcEvent $e) { $app = $e->getApplication(); $eventManager = $app->getEventManager(); $eventManager->attach('dispatch.error', array($this, 'handleDispatchErrorWithLogger'), 100); } public function handleDispatchErrorWithLogger(MvcEvent $e) { $exception = $e->getParam('exception'); //it is just a sample, you can create service for logger $writer = new \Zend\Log\Writer\Stream('./data/logs/'.date('Y-m-d').'-log.txt'); $log = new \Zend\Log\Logger(); $log->addWriter($writer); $log->err($exception); } public function getConfig(){/*common code*/} public function getAutoloaderConfig(){/*common code*/}}
And many more when application growing, so, this is how it can be moved out :
1. Create a class that has __invoke method that will be fired when event triggered
class DispatchErrorHandlerListener{ public function __invoke(MvcEvent $e) { $exception = $e->getParam('exception'); //it is just a sample, you can create service for logger $writer = new \Zend\Log\Writer\Stream('./data/logs/'.date('Y-m-d').'-log.txt'); $log = new \Zend\Log\Logger(); $log->addWriter($writer); $log->err($exception); }}
2. Make the listener as object
class Module{ public function onBootstrap(MvcEvent $e) { $app = $e->getApplication(); $eventManager = $app->getEventManager(); $eventManager->attach('dispatch.error', new \Tutorial\Listener\DispatchErrorHandlerListener, 100); } public function getConfig(){/*common code*/} public function getAutoloaderConfig(){/*common code*/}}
and if you like the listener as service, you can pass like this :
class Module{ public function onBootstrap(MvcEvent $e) { $app = $e->getApplication(); $eventManager = $app->getEventManager(); $service = $app->getServiceManager(); $eventManager->attach('dispatch.error', $sm->get('YourRegisteredErrorHandlerListener'), 100); } public function getConfig(){/*common code*/} public function getAutoloaderConfig(){/*common code*/}}
Done, now your Module class is simplified ;)
0 0
- Zend Framework 2 : Move out your listeners from Module class
- Zend Framework 2 Module.php 简要说明
- 快速创建一个zend framework 2的module
- Zend Framework 2 module.config.php配置详解
- Zend Framework Module之多模块配置
- Getting Listeners from JavaBeansTM
- zend framework入门2
- Zend Framework and Firebug – Log and Debug your Projects
- zend framework php 小结2
- Zend Framework 1.11.2 发布
- Zend Framework 2 抢先体验
- Zend Framework 2 抢先体验
- zend framework 2 相关文章
- zend framework 2 前端过滤器
- zend Framework 2 bug 收集
- zend framework 2f安装
- Zend Framework suffers from a SQL configuration file disclosure vulnerability.
- zend framework
- 在ListView中添加item为WebView
- asp.net web api帮助生成插件
- (转)网络采集软件核心技术剖析系列(1)---如何使用C#语言获取博客园某个博主的全部随笔链接及标题
- Java 异常处理的误区和经验总结
- linux(Ubuntu)安装QQ2013
- Zend Framework 2 : Move out your listeners from Module class
- FragmentTabHost切换Fragment时避免重复加载UI,导致切换后重绘页面的问题解决
- 汇总iOS开发中需要用到的开源库
- Activity中获取view的高度和宽度为0的原因以及解决方案
- 线程、创建线程、终止线程、线程同步
- Android核心基础——Day05_1
- EventQueue.invokeLater
- XSuperNEST全自动套料解决方案
- 关于InnoDB表的page利用率和optimize table