Difference between init() and onBootStrap() in Zend Framework 2?

怎甘沉沦 提交于 2019-12-05 14:10:09

The answer to your question is a matter of timing and purpose. The init() function always occurs before the onBootstrap() function. Since the purpose of init() is to initialize the module (eg. with its own, independent configuration options), other modules may not have been loaded at the time init() is run for a given module. However, onBootstrap() is run after all modules have been initialized, and it can listen for different events.

A much more thorough explanation of this can be found at http://framework.zend.com/manual/2.3/en/modules/zend.module-manager.module-manager.html and the next page in the documentation http://framework.zend.com/manual/2.3/en/modules/zend.module-manager.module-class.html

Personally, I use the init() to initialize a Propel library in one module I creatively named Propel by using the technique at http://4zend.com/integrate-propel-orm-with-zend-framework-2/.

I use onBootstrap() to check my access control list (which users have access to what resources) and restrict access accordingly, like this:

public function onBootstrap(\Zend\Mvc\MvcEvent $e) {
    // After the route event occurs, run the checkAcl method of this class
    $e->getApplication()->getEventManager()->attach('route', array($this, 'checkAcl'));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!