可以访问PHP MVC框架中的公共类
I have read a lot of SO topics about this already, but still haven't found (or have been able to create) a proper answer.
I am working on a small MVC framework and I want some global class/object that I can call from my controllers (but maybe models too).
There are a couple of routes I can take:
- Global variable
- Static class/Registry
- Dependency injection
The internet seems to agree that the DI is the best route to take. I think I have grasped the idea, but am not comfortable yet. So I want to throw in some background information; I will probably be the only one working on the project, it is a small project, all my controllers extend the main Controller (so I could just load one library like class there).
As a concrete example, I want to have an array of categories. So I started out with putting that array in the CategoryController. But now I noticed I kinda want to use that array in my frontview and in ProductController as well. Obviously I don't want to load all of CategoryController into ProductController. You could also say I could put that array in some kind of configuration or settings file, because of the simpleness of this example, but that's why it's an example. I will probably expand on it with more functionality.
So to summarize: In PHP (specifically inside a MVC model) how can you give your classes (mainly Controllers) access to some kind of common class or other sharable functionality.
我已经阅读了很多关于此的SO主题,但仍未找到(或已经能够 创建一个正确的答案。 p>
我正在研究一个小型的MVC框架,我想要一些可以从我的控制器调用的全局类/对象(但也许是模型)。 p >
我可以采取以下几种方式: p>
- 全局变量 li>
- 静态类/注册表
- 依赖注入 li>
ol>
互联网似乎同意DI是最佳路径。 我想我已经掌握了这个想法,但还不舒服。 所以我想提供一些背景信息; 我可能是唯一一个在这个项目上工作的人,它是一个小项目,我的所有控制器都扩展了主控制器(所以我可以加载一个像那里的类一样的库)。 p>
As 一个具体的例子,我想要一个类别数组。 所以我开始将该数组放在CategoryController中。 但现在我注意到我有点想在我的前视图和ProductController中使用该数组。 显然我不想将所有的CategoryController加载到ProductController中。 你也可以说我可以把那个数组放在某种配置或设置文件中,因为这个例子很简单,但这就是为什么它就是一个例子。 我可能会用更多功能扩展它。 p>
总结一下:在PHP中(特别是在MVC模型中)你如何让你的类(主要是控制器)访问某种常见的 类或其他可共享的功能。 p> div>
Your controllers are created by "something" (usually a front controller). So when the controller is created, you could inject a dependency injection container.
And in your configuration/bootstrap (before the controller is created), you should add you categories to the container.
That way you can access the categories from every controller.
Please note that this is a simple example that doesn't totally fit the spirit of dependency injection. The best solution would be to inject directly the categories (instead of injecting the container). But that can become a lot of work if you generalize that pattern (lots of dependencies to handle in your front controller).
A solution would be to use a dependency injection framework that could do that for you.
For example I work on a DI container that lets you inject stuff with annotations (PHP-DI), but there are several other libraries for DI so you have a choice.
My 2 cents:
In a small self-made mini-framework I have done some time ago, I have created a global singleton class named Application, and anything/everything which should be accessible from anywhere/everywhere was a property or method of this class.
In my case, there was a $db
property for database access, a $user
property to access the user data and methods, an $input
property for a "powered" $_REQUEST
access, and so on.
Of course, you have many other options, suitable for different scenarios. This approach simply worked fine for me on that occasion.
Now, if you want to access a controller from another controller, this really sounds strange. This "thing" that you want to access should be a model class, a library class, or anything else, but it should not be "locked" inside a controller class. Indeed, the controller should be "as thin as possible", and focus on calling the appropriated methods from other classes, based on the user input (request) and then calling some output method to generate and send the answer (response).
Finally, although I've read some criticism and complaints about it (as well as praises too), I do make use of static
methods a lot, mainly for classes which are more "helpers" than anything else.