在 MVC 设置中处理配置变量的正确方法 (PHP)
我正在用 PHP 编写第一个基本的 MVC 模式设置。我知道全局变量不好,而且我也知道我不希望我的所有类都可以访问我的所有配置变量。
我有一个 settings.php 文件,我想在其中定义一堆常量,例如我的数据库连接信息、目录结构信息、电子邮件地址等。这将是一个保存我所有重要信息的集中位置。
实现配置类的最佳方法是什么,以便我的控制器和模型的每个基类只能访问它们需要的配置变量?例如,我的模型基类应该可以通过数据库连接信息进行访问。
基本上我只是问如何在不声明全局变量的情况下部署自己的 MVC 设置来处理配置信息,就像我们在程序时代那样。
谢谢。
I'm writing my first basic bare bones MVC patterns setup in PHP. I know global variables are bad, and I aslo know I don't want all of my classes to have access to all of my config vars.
I have a settings.php file that I would like to define a bunch of constants in, like my db connection info, directory structure info, email addresses and so on. It will be one centralized location that holds all of my important information.
What's the best way to implement a config class so that each of my base classes for my controller and model only have access to the config vars they need? For example, my model base class should have access to by db connection info.
Basically I am just asking how anybody whole rolls their own MVC setup handles config information without declaring global variables, like we used to back in the procedural days.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你会得到很多关于这个问题的答案,因为它基本上可以归结为偏好。
就我个人而言,我使用了配置数组。示例:
然后将 byref 您需要的片段传递到它们需要去的地方。
You're going to get a bunch of answers on this as it basically boils down to preference.
Personally, ive used a config array. example:
Then just pass byref the pieces you need into where they need to go.
我推出了自己的 MVC 设置。 (仍然强劲。可能会开源)。我这样做的方法是使用一个初始化脚本将所有这些常量传递给控制类。
控制类是单例类。因此,每当另一个类需要访问它时,它只需获取控制类的现有实例,并且所有变量都可以从中获取。
I rolled my own MVC setup. (Still going strong. Might open source it). The way I do it is to have an init script that passes all those constants to a controlling class.
The controlling class is a Singleton. So anytime another class needs access to it, it just gets the existing instance of the controlling class and all the variables are available from it.