I’m building a small framework that I can use for repeated mundane stuff on future small projects.
I’m stuck on the best way to access libraries from inside a controller. I originally implemented a system similar to CodeIgniter’s whereby my main controller class is basically a super object and loads all the classes into class variables which are then accessed by extending the controller and doing like $this->class->method()
I find that a little ugly, though. So I thought of just loading each class individually on a per-use basis in each controller method.
What’s the best (cleanest) way of doing this?
To only ever have one instance of each class, you could create a simple service container.
Then create one ServiceContainer instance per application. Inject the container into all of your controllers and use
Simple example, and obviously needs a lot of work to make useable (for instance autoloading needed and handling of file paths for ease of use, or easier way to add services without getting them, etc).