4

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.

ohiock
  • 646
  • 3
  • 7
  • 22
  • 3
    The autoloader is not technically a part of the MVC pattern, it is just a mechanism to find and load your classes on demand. You could just as well load statically all your MVC classes and do not use an autoloader at all; on the other hand, you could use an autoloader for any classes you use, even if they have nothing to do with the MVC pattern. – lanzz Oct 18 '12 at 18:38
  • Sorry, I didn't intend to sound as if I thought the autoloader was specific to a MVC pattern. I'm just unsure of where it is generally located in this setting. – ohiock Oct 18 '12 at 18:43
  • The `index.php` file includes `../app/boostrap.php`. And bootstrap includes `../lib/someautoloader.php`, then initializes the autoloader. – tereško Oct 18 '12 at 18:44
  • 1
    You should initialize your autoloader as early as possible, so that you minimize the need to include your class definitions statically. There are no MVC-specific concerns regarding your autoloader initialization. If your autoloader itself depends on any classes defined in separate files, you _will_ have to load them statically just to get the autoloader running, but as soon as all its dependencies are initialized, you should set it up. – lanzz Oct 18 '12 at 18:46

3 Answers3

3

You should put it in your bootstrap file.

This is how you can do this:

  1. Force every HTTP request to your front controller, index.php, app.php or how ever you want to call it.
  2. Front Controller can define some constants used in framework and then include your Bootstrap.php file. Bootstrap will boot up your application.
  3. Now, first thing in I do in Bootstrap is to register autoloading. This way I can easily get \System\Router\Router class, or \System\Router\Dispatcher class, you get the point.

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 :)

otporan
  • 1,345
  • 2
  • 12
  • 29
  • 1
    Thanks for providing simple examples - they certainly helped me understand things easier. – ohiock Oct 22 '12 at 18:12
2

Definitely in the bootstrap stage!

The autoloader should be part of every PHP application and it should be (one of) the first classes/code initialized.

My MVC initialization steps:

  • index is the entry point
  • bootstrap for initializing error handling, autoloader and IoC
  • application, mostly a MVC app
  • routing mechanism
  • controller
  • model
  • view
tereško
  • 58,060
  • 25
  • 98
  • 150
JvdBerg
  • 21,777
  • 8
  • 38
  • 55
  • "bootstrapping usually refers to a self-starting process that is supposed to proceed without external input" https://en.wikipedia.org/wiki/Bootstrapping – Yousha Aleayoub Jun 17 '20 at 14:48
1

Well, the question "Where does it go?" to me suggests two more precise questions:

  1. Where is the file stored that contains the autoloader function/class definition?
  2. Where in your request dispatch cycle should it be instantiated, configured, and allowed to do its magic?

The first question - "Where to place the file containing the class?" - is probably not so critical for you since you have identified an autoloader class you want to use. The precise answer depends upon your own framework app structure, but for an externally developed class like the one you cite, someplace within a lib or vendor directory probably makes sense.

For the second question - "Where to instantiate, configure, etc?" - the answer is: as early as possible in the request cycle, so you can get the benefit of autoloading for all classes that are referenced later. In practical terms, that probably means somewhere in your bootstrap process.

Of course, this usually means that in order to load your autoloader class, you will probably have to do a manual require/include call, instantiate your autoloader object, and configure it with with namespaces and paths.

David Weinraub
  • 14,144
  • 4
  • 42
  • 64
  • So the best approach for me would be to create the autloader file, place it in a directory outisde of my MVC files. Then, instantiate the class inside of my front controller, and from there I should be good to utilize any class as I please, in any file in my MVC? – ohiock Oct 18 '12 at 19:14
  • Right, bootstrap can set up autoloading, both of which could be potentially usable outside the context of a web request (perhaps for a CLI request). Then, for a web request, you can create any MVC components you envision for your framework (ex: front-controller, router, dispatcher, controller, view, etc). – David Weinraub Oct 18 '12 at 20:33