Popular Posts

 Bootstrap PHP Code

Boostrap Image

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

  1. How to Create a Bootstrap File
  2. What to Bootstrap
  3. Setting up

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>

Enjoyed this article? Please share it.

Comments
  1. Richard
    November 6th, 2008

    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.

  2. feketegy
    November 6th, 2008

    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.

  3. Richard
    November 6th, 2008

    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.

  4. PHPSnaps
    November 22nd, 2008

    I’s like to see your implementation of the router class. Will there
    be part 2 and 3 for this article? It seems incomplete.

  5. Gyorgy Fekete
    November 22nd, 2008

    This was just an introduction.

    I will sure continue this article in the near future.

  6. Boris Smirnov
    November 23rd, 2008

    Nice Article, Thank you. Will reference it in the future.

  7. Andrew Smith
    December 1st, 2008

    This was a really interesting Article, it gives more insight of how some of these PHP Frameworks work.

  8. Henrich
    April 21st, 2009

    This article was useful for me. Thanks! :)

  9. reboltutorial
    July 12th, 2009

    Excellent tutorial, I wouldn’t have done better :)

  10. Gyorgy Fekete
    April 15th, 2010

    Thanks for the comments :)

  11. Trond
    August 21st, 2010

    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.

  12. Gyorgy Fekete
    August 21st, 2010

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

Leave your reply
  • (required)
  • (will not be published)
  • Show
    ;) :| :virus: :sparta: :ridecursor: :pinkelephant: :love: :linux: :epicfall: :duel: :alien: :P :D :)) :) :( 8O 8)
  • Allowed tags: <a href="" title="">, <b>, <i>, <strike>, <em>, <strong>, <code>