Zend Framework 2 中 init() 和 onBootStrap() 之间的区别? [英] Difference between init() and onBootStrap() in Zend Framework 2?

查看:21
本文介绍了Zend Framework 2 中 init() 和 onBootStrap() 之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一本关于 ZF2 的书,它将 init() 和 onBootStrap() 都称为 Module.php 中的函数,在每次页面加载期间都会调用这些函数,因此应该尽可能轻量级.

I'm reading a book on ZF2 and it refers to both init() and onBootStrap() as functions in Module.php that are called during every page load and therefore should be as lightweight as possible.

除了签名略有不同:

init(ModuleManager m)
onBootStrap(MvcEvent e)

我正在努力确定何时应该使用哪个,以及出于什么原因.在本书中,这两种方法都用于附加到(不同的)事件.有人可以明确定义两者之间的区别,并提供一些具体示例,其中我会使用一个而不使用另一个(以及为什么)?

I'm struggling to determine when I should use which, and for what reason. In the book, both methods are being used to attach to (different) events. Can someone provide a clear definition of the difference between the two, and some concrete examples where I would use one but not the other (and why)?

谢谢!

推荐答案

问题的答案取决于时间和目的.init() 函数总是出现在 onBootstrap() 函数之前.由于 init() 的目的是初始化模块(例如,使用它自己的、独立的配置选项),在 init() 时可能还没有加载其他模块> 为给定的模块运行.但是,onBootstrap() 是在所有模块初始化之后运行的,它可以监听不同的事件.

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.

对此的更详尽的解释可以在http://framework.zend.com/manual/2.3/en/modules/zend.module-manager.module-manager.html以及文档中的下一页http://framework.zend.com/manual/2.3/en/modules/zend.module-manager.module-class.html

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

就我个人而言,我使用 init() 在一个我创造性地命名为 Propel 的模块中初始化 Propel 库,方法是使用 http://4zend.com/integrate-propel-orm-with-zend-framework-2/.

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/.

我使用 onBootstrap() 检查我的访问控制列表(哪些用户可以访问哪些资源)并相应地限制访问,如下所示:

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'));
}

这篇关于Zend Framework 2 中 init() 和 onBootStrap() 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆