Server-Side Magazine

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>

Related Posts

Tags: , ,

About the Author

Server-Side Magazine

Server-Side Magazine is a place where users can contribute articles. Strictly server-side posts are presented in the following programming languages: PHP, Ruby, ASP.Net, Java, Python

Our philosophy is that knowledge should be free and available to anyone. We designed this site to be an open platform, meaning that anyone can contribute and share their knowledge on the above areas.

Although, it's an open platform we don't accept all articles, because it would result in a "just another", mediocre website. A minimum standard of quality is required from every submitted article.

Comments

Leave a Comment

Your email address will not be published. Required fields are marked *

Markdown enabled. Click here to see the syntax.

 
More in PHP (7 of 9 articles)