Gerben Jacobs
Gerben Jacobs

Reputation: 4583

Have access to a common class in a PHP MVC framework

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:

  1. Global variable
  2. Static class/Registry
  3. 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.

Upvotes: 3

Views: 892

Answers (2)

Matthieu Napoli
Matthieu Napoli

Reputation: 49573

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.

Upvotes: 2

J. Bruni
J. Bruni

Reputation: 20492

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.

Upvotes: -1

Related Questions