I'm trying to build a simple MVC framework to better understand certain concepts. The first thing that I thought would be important to address is a front controller that handles all of the requests for my applications.
Once I started to think about it, I wasn't sure of the best way to load the classes that my application would be using. My current thought process is that my autoloader should be located in the front controller since every request utilizes it. Where do most frameworks typically put this? Looking through a few already built frameworks hasn't helped me much as a lot of the functionality far exceeds what I need, complicating it so much that it's hard to understand.
The class loader that I am trying to use can be found here https://gist.github.com/221634
Just trying to figure out how to appropriately build and organize a simple MVC framework.
You should put it in your bootstrap file.
This is how you can do this:
One more thing, you can even register your application Models folder with PSR0 class loader. So lets say that your Models folder looks like this:
application/Models/
- Entities
- Services
Email.php
Cache.php
From inside your controller you can easily get Models like this
public function someController()
{
$email = new \Models\Services\Email();
// Do stuff with email service
}
So short answer to your question is that the best thing to have is first Front Controller that gives you some "wiggle" room, then from there you load your Bootstrap that boots up your app, and first thing in Bootstrap is to require your class loader, and register libraries you want to use through application.
And then you can even register autoloading for your application Controllers and Models folder, and at the end of Bootstrap file when you are about to dispatch request you can ask for Controller like this:
$app = new '\\Application\\Controllers\\' . $class;
// Dispatch request with call_user_func_array or ReflectionMethod and ReflectionClass
No need to require Controller class since its autoloaded, just provide it with correct namespace.
Great question, hope this helps! Nice to see there are other guys playing around with their custome MVC :)