
Bootstrapping means that every server request are funneled through a single (or a few) PHP file. This file will be the “bootstrapper” of our application. It will help instantiate objects that are needed by every page in general such as starting a session, connecting to a database, defining constants and default variables, etc.
Table of Contents
How to Create a Bootstrap File
Generally it’s a good practice to setup a bootstrap file for every PHP website or web application. This way a developer could easily manage the behavior of his application in a centralized manner.
This file is generally the main entry point on each HTTP request, usually the index.php file in the document root.
It is important to mention that this file usually doesn’t contain any HTML markup, just pure PHP that will load the template files if necessary or a front controller as how most of the MVC frameworks implement it.
Let’s dig in. Create a file in your document root and name it index.php. Don’t write anything in the, just save it.
What to Bootstrap
It is a good plan to create a mockup of functionalities that we wish to assign and implement in our file.
Usually a bootstrap file contains the necessary source code, libraries and logic to start the entire application. From showing figuring out what page to show, how to communicate with the database, etc.
Generally the file should contain the following initializations:
- Configuration
- Session, cookies
- Caching
- Database
- Directory and file paths
- Global variables and constants
- Web application status
- Web page routing
- Feeds
- XML/RPC
Setting Up
A bootstrap file usually starts by including those libraries that are necessary for the file itself to function correctly. This involves setting general directory and file paths, loading configuration files, etc.
1 2 3 4 5 6 7 8 9 | //let's set up a root path constant define('ROOT',getcwd().DIRECTORY_SEPARATOR); //define the includes and config folders define('INCLUDES',ROOT.'includes'.DIRECTORY_SEPARATOR); define('CONFIG',ROOT.'config'.DIRECTORY_SEPARATOR); //load in the main configuration file include_once(CONFIG.'base.inc.php'); |
After the basic configuration we can write additional logic into the file, e.g. loading the session object, database object, etc. It’s worth mentioning that every item from the list above should be an object/class, if we are using OOP, that manages that particular feature.
It’s also encouraged that even for a basic web project we set up different application statuses such as Development, Production or Testing and to load different libraries, logic for every status. E.g. In production is a good practice to disable error display:
1 2 3 4 5 6 7 8 9 10 | //define our status constant define('STATUS','production'); //check what status we have switch (STATUS) { case 'production': { ini_set('display_errors','Off'); //and other specific includes, commands, etc. } } |
Lastly we would set the page routing logic. A class that parses the HTTP request and renders the page by loading the necessary template files if any.
After we set up the bootstrap file, we have to direct all request to this file. This is usually achieved with the help of a few rewrite rules in a .htaccess file in our root directory (or that directory where the index.php file is stored).
1 2 3 4 5 6 7 8 9 10 11 12 13 | <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Rewrite all other URLs to index.php/URL RewriteRule ^(.*)$ index.php/$1 [PT,L] </IfModule> <IfModule !mod_rewrite.c> ErrorDocument 404 /index.php </IfModule> |
Nice article. Will be keeping an eye on this site. I was wondering one thing. Can anyone tell me if there is a theme for Dreamweaver that will change the IDE so that it looks like the code pictured for this article? I just started a new job and am using lovely big iMacs but find that sometimes the screen makes my eyes tired because it is basically all white. I think it would be easier if it were a darker background like that shown above. I know you can change it manually in DW but I guess I just looking for an easy way first. Thanks for any info.
Hello, this is offtopic
But you have to change it manually or use TextMate (for Mac only) and select a beautiful dark theme. Since I use a dark theme my eyes doesn't get tired that much.
Sorry didn't mean to go off topic. Was just looking at the pic thinking how nice it would be to use that for coding. I might have a play with manually changing Dreamweaver then. Thanks.
I's like to see your implementation of the router class. Will there be part 2 and 3 for this article? It seems incomplete.
This was just an introduction.
I will sure continue this article in the near future.
Nice Article, Thank you. Will reference it in the future.
This was a really interesting Article, it gives more insight of how some of these PHP Frameworks work.
This article was useful for me. Thanks! :)
Excellent tutorial, I wouldn't have done better :)
Thanks for the comments :)
Hi. Interesting tutorial but I have one security remark. A full-blown bootstrap file will contain very critical data to set up and run the application. I would consider a non-critical index file in a public map were you can address the hidden and more critical bootstrap data in a non-public map. Then let the non-critical index file be the single point of entry for the whole application.
Yes, what you wrote is true, but you're missing the whole point of the article.
I wanted to present the idea behind a bootstrap file and the fact that how popular frameworks handle bootstrapping.
The devil is in the details sort of speak :)
In zend the bootstrap is located in non public folder..... When I was developing with php back in 2005 before Wordpress and frameworks, bootstrap was such an advanced feature.. I never learned how to make one by scratch.. It Would be great if you can continue the article.. All of these frameworks take the fun out of it.. One question. Do you have to have a class for a bootstrap? I don't think so ??
I will definitively will continue this article. Thanks for your comment
And to answer your question: No. A bootstrap could be just another class which sets up the whole application / framework or it could be procedural code too.
Hi, We have used this article as a quick heads up to our new PHP recruits when creating our new in house framework. Good post. Many Thanks
Nice one. I would like to hear more about your custom framework :)
Not sure what we are going to call it yet. However its going to be like Symfony but much leaner. I'm going to release it on GitHub once I have routing working... Its looking pretty cool though.
I've removed all the fluff like yml, xml and ini files to increase performance. All config is in PHP Arrays for speed.
I'll post up here when I have it on GitHub.
Also, I wanted to ask you this... Why bother with a new framework from scratch? Why don't you just use an existing framework such as Kohana, Laravel as your base and build upon that, modify it, extend etc.
Not sure really, Its really nice knowing the core functionality. Also I'm writing the framework with refactoring projects in mind so you can run old procedural code alongside your new OOP code.