Blueprint PHP Framework Tutorial – Part 2 (Bootstrapping)

As promised in part 1 of this tutorial series, I’ll run through how the bootstrap script works, and in doing so, explain some of the core concepts of the framework. I suggest you open the bootstrap script in another tab while reading this so you can see what I’m talking about and follow along. Or if you have downloaded/cloned the project from the github repository, just open the file in your editor (located in /app/bootstrap.php).

The Blueprint PHP Framework makes use of class autoloading and uses namespaces and the PSR-0 standard for autoloading. This keeps the components compatible with other frameworks like Symfony and other libraries also supporting the standard. This is also good news for you in that you never have to call a require or include when you want to use a class. You just need to instantiate and the framework will work out where the class is based on it’s namespace.

For example if you wanted to use the config class you would simply call:


No need to require; the autoloader will figure out that the config class lives in /lib/Blueprint/Config/Config.php and require it for you. If you’re scared off by namespaces, please don’t be. PHP will extensively make use of namespacing in the future to combat naming clashes and a lot of projects built in PHP are making use of namespaces now, including the upcoming release of the new Zend framework.

Right, so we have autoloading figured out. Evidently we need to require the autoloader class to start with:

Then you need to tell the autoloader which paths to use when looking for class files:

where LIB_DIR and APP_DIR are the full paths to /lib/ and /app/. Finally, we need to register the autoloader with the built-in spl_autoload_register function:

Thats the autoloader dealt with. Now when instantiating any classes that are properly namespaced in the /lib/ or /app/ directories, they will be required automagically. Onto getting the components tied together. First we need to create an $app object that will hold all the core dependencies for the application:

$app has extended the container class which has methods for setting and getting the dependencies which will be very useful as you will see in a moment. Next we will create the config object and inject it into the $app object:

The config object calls loadConfig() which passes the array in /etc/config.php into the object. The elements in the array become properties of the $config object. Finally we call $app->set() to inject the class with a name ‘config’ so we can pull it out later when we need to use it.

Set up the rest of the core components you are going to use in the same way:

As you can see, some of the objects require dependencies, such as the $config object. To instantiate the Database class for example, we need to pass it the $config object. We do this like so: new Database($app->get(‘config’)); The database object itself can then be injected into to the $app object.

The purpose of doing dependency injection is to avoid having all the objects in the global scope in order to use them where we want to, which leads to spaghetti code. Each object only has the access to the dependencies it requires.

Now that we have built all the dependencies for the application, we pass them to the router:

The router’s job is to figure out which controller and method to invoke from an incoming request. The details of this will be explained in the next post. All we need to do now is register the location of the namespaces where the controllers live. Usually, you would only have one location but you can load multiple locations:

The second parameter of the registerRouteMap() method is for the location of an optional routing file which will define custom routes for the namespace. Again, we will go into detail about this in the next post. Finally we dispatch the request to the router:

From the here the controller takes over and does it’s business. Hopefully, this has given you a good quick overview of how the components of the framework come together.

Next time, we’ll go over routing and controller setup. Ciao!

2 thoughts on “Blueprint PHP Framework Tutorial – Part 2 (Bootstrapping)

  1. Thanks for taking your time to write these tutorials
    they really helped me to understand more about mvc and DI. and i really like your framework i am working with it.

Leave a Reply