Bootstrap PHP Code

Ryan Dube
Bootstrap PHP Code
Bootstrap PHP Code

One of the excellent features of PHP is that you can create what's called "bootstrap PHP code," which serves as sort of a central processing script that sets up your web application in a dynamic and well organized way.

What is Bootstrap PHP Code?

The easiest way to understand how to bootstrap PHP code is to consider the original use of the term "bootstrapping." In the computer industry, bootstrapping a PC relates to the initialization and boot-up of a computer. During the boot process, a small segment of code initializes the hardware and loads the operating system, which then generates the user environment. The initialization of the computer system is bootstrapping, and the same definition applies to your PHP web application.

Why You Should Use PHP Bootstrapping

Just like bootstrapping a computer means initializing the hardware and software, bootstrapping your web application means setting up and initializing libraries, variables, database connections and everything else that's required, and then organizing how you're going to launch your application based upon how the user enters the URL.

Some of the items that the PHP bootstrapping file initializes include:

  • Configuring and setting up the application
  • Creating any necessary sessions or defining user cookies
  • Handling any required caching
  • Configuring database connections
  • Setting up variables for directory and file paths
  • Defining global variables and constants
  • Routing http requests to the appropriate page
  • Handling all feeds and XML/RPC

Configuring an entire PHP bootstrapping file could require an entire book to understand, but it's simple enough to generate a basic PHP bootstrapping file and then building upon that skeleton file as you learn about additional bootstrapping functions.

Setting up PHP Bootstrapping

Creating a bootstrap file for your web application is considered very good web programming practice. This is because having a bootstrap file provides you with an organized central place where you can redefine variables, database connections and other application elements for all pages, rather than configuring those elements all throughout the application. The following steps will walk you through creating a very basic PHP bootstrap file that you can use as your foundation.

Using Rewrite Code in Your Htaccess File

This PHP file is going to become the main entry that every HTTP request passes through. This file is usually the standard index.php file. This PHP bootstrap file can have any number of functionalities depending on what you want your application to do, so make sure you've created an accurate diagram of your web app's features and how you want to handle incoming requests. For example, media or picture files can be processed one way, database requests can be handled another, etc. The possibilities are limited only by your application itself.

However, before you jump into processing the initialization of variables and database connections, you'll want to funnel HTTP requests in a way that "preprocesses" them and prepares them for your bootstrap file. You can do this with what is called "rewrite code" in the .htaccess file stored in the root folder of your application. The most common format of this rewrite code is as follows.

Options FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([a-zA-Z0-9-_/\ ] )$ index.php?route=$1 [L,QSA]

This code routes all incoming HTTP requests to the index.php file and allows the user to add only special characters to the URL. While you could process all file requests using your PHP bootstrap file, many programmers prefer to serve specific files directly, which cuts down on the page load time. You can do this with the following rewrite code.

RewriteEngine on RewriteBase /test/ RewriteRule !\.(js|txt|gif|jpg|png)$ index.php

This just passes all javascript, text, jpg, gif and png files through to the user's browser without having to pass through the PHP script.

Simple Bootstrap Functions

Once you have all HTTP requests routed to your bootstrap file, the file must apply the Model View Component (MVC) framework to handle all configurable components of the application. A basic bootstrap file is set up to load application files, source files, classes, methods, arguments and more. Writing this file from scratch can be time consuming, so you may want to consider using the free code that other programmers share on their websites. A few great sources for bootstrap PHP code includes the following sites below.

  • PHP Snaps lets you either highlight the code, or download it in file format.
  • Doctrine-Project not only provides the bootstrap.php file in sections, but it also walks you through each step of configuring the file yourself.
  • HotScripts offers a number of installation kits with the php file, but Zend Framework in particular provides both the bootstrap and the index controller.
  • Developer.com walks you through the process of setting up your file in very specific step-by-step detail.

Final Words

When it comes to writing a PHP web application, most developers agree that bootstrapping your application is the accepted method for creating a well designed initialization procedure. Such a procedure will insure not only easy troubleshooting for your web application, but also a single location where you can return to further customize the classes, libraries and variables for your application.

Bootstrap PHP Code