像 Zend 一样的 Codeigniter 引导程序
有什么方法可以在初始化时创建一个适用于 Codeigniter 中所有控制器的函数吗?
在 Zend 中有一个 application/Bootstrap.php,我需要一些类似的解决方案。
Is there any way to create a function that works for all controllers in Codeigniter at init?
In Zend there is a application/Bootstrap.php, i need some solution like that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以扩展本机
CI_Controller
类并创建一个MY_Controller
类,您的应用程序的所有控制器都将扩展该类。然后,MY_Controller
类中的方法将可供扩展它的每个控制器使用。您还可以将代码放入MY_Controller
构造函数中,每次构造子控制器时都会执行该构造函数。我不记得 Bootstrap 文件在 Zend 中是如何工作的,但如果这听起来像是一个可行的解决方案 文档的创建核心系统类部分解释了如何扩展本机控制器。
You could extend the native
CI_Controller
class and create aMY_Controller
class that all of your application's controllers would extend. Methods in theMY_Controller
class would then be available to every controller that extends it. You could also put code in theMY_Controller
constructor that would be executed each time a child controller was constructed.I don't remember exactly how the Bootstrap file works in Zend, but if this sounds like a viable solution the Creating Core System Classes section of the documentation explains how to extend the native controller.
您可以将 New_controller 扩展到 CI_Controller。在New_controller中你可以编写你想要的常用函数。有关新扩展控制器的使用,您可以查看此链接:
手册中的几个地方简要讨论了扩展核心控制器的主题 - 特别是在核心类和创建库页面中。
扩展核心控制器的目的是为所有普通控制器提供方法和属性。还有其他方法可以提供这种普遍的功能 - 包括 Hooks、Libraries 和 Helpers。在假设以下内容是您问题的答案之前,您应该熟悉这些替代方案的方法和优点。
最后,假设您有一个可以执行某些操作的应用程序 - 做什么并不重要,只要您有一个我们可以在此处使用的现有控制器即可。
-extend_the_CI_Controller
You can extend your New_controller to CI_Controller. In New_controller you can write common function which you want. For use about new extended controller you can see this link:
The subject of extending core controllers is discussed briefly in a few places in the manual - specifically in the Core Classes and Creating Libraries pages.
The intent of extending the core Controller is to provide methods and attributes to all your normal Controllers. There are other ways of providing this kind of pervasive functionality - including Hooks, Libraries and Helpers. You should familiarise yourself with the methods and benefits of those alternatives before assuming the following is the answer to your question.
Finally, it’s assumed that you have an application that does something - it doesn’t matter what, merely that you have an existing Controller that we can work with here.
-extend_the_CI_Controller