How to Bootstrap PHP Code

So what the heck is this Bootstrap PHP thing anyway?

Bootstrap means to load a small program that eventually calls the desired program into the computer, similar to an operating system being called by a BIOS program. The word bootstrap also has different meaning in different fields like science, medical, etc.

With regards to computer technology, “bootstrap PHP code” means creating a bootstrapper that handles all the dynamic requests coming to a server and apply the true MVC (Model View Component) framework so that in future you can change the functionality for each unique component or application without changing the entire code or application.

Below you will find some steps to get the process started;

1. Go to the home directory of your website and create a “Website_Src” folder and put all your application files inside it.

2. Inside the “Website_Src” directory, create an index.php file and add the following code to it.

class index
{
	public function _construct()
	{
	}

	public function index($args)
	{
		echo 'This is default index page visible to every request that can’t be routed';
	}
}

3. Now, create a second file and name it “welcome.php” & add the following lines of code to it.

class welcome
{
	public function _construct()
	{
	}
	public function index($args)
	{
		// redirecting it to test function()
		$this->test($args);
	}

	public function test($args)
	{
		if (isset($args[0] )) echo $args[0];
		if (isset($args[1] )) echo ' '.$args[1];
	}
}

4. Now, create a “.htaccess” file (if you don’t already have one) and add the following code to it. This will allow only specific characters in URI.

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

5. Finally, paste the following code into you “index.php” file present in the “public_html” directory and test your bootstrap php code by clicking on your website link say: http://www.mywebsite.com/welcome/testing/hello

// This bootstrap php code will work only with php5 with no template engine.
// Stops from reporting any error
error_reporting(0);

// This is the folder where all your application files are present.
define('CLASSDIR', ' Website_Src ');

// Absolute path to the source files, just one level behind public folder
define('BASEDIR', @realpath( dirname (__FILE__).'/../'.CLASSDIR).'/' );
// Automatic loading of classes by using a function so that you can get rid of all include() calls.

function _autoload($class)
{
	$file = BASEDIR.$class.'.php';
	if (!file_exists ($file) )
	{
		echo 'Requested module ''.$class.'' is missing. Execution stopped.';
		exit();
	}
	require($file);
}

// breaking requesting URI to parts & retrieving the specific arguments, methods & class
$route = '';
$class = '';
$method = '';
$args = null;
$cmd_path = BASEDIR;
$fullpath = '';
$file = '';

if (empty($_GET['route']) ) $route = 'index'; else $route = $_GET['route'];
$route = trim($route, '/\');
$parts = explode('/', $route);

foreach($parts as $part)
{
	$part = str_replace('-', '_', $part);
	$fullpath .= $cmd_path.$part;

	if (is_dir($fullpath))
	{
		$cmd_path .= $part.'/';
		array_shift($parts);
		continue;
	}

	if (is_file($fullpath.'.php') )
	{
		$class = $part;
		array_shift($parts);
		break;
	}
}

if (empty($class) ) $class = 'index';
	$action = array_shift($parts);
	$action = str_replace('-', '_', $action);

if (empty($action) ) $action = 'index';
	$file = $cmd_path.$class.'.php';
	$args = $parts;

// now that we have the parts , let's run a few more test and then execute the function in the class file
if (is_readable($file) == false)
{
	echo 'Requested module ''.$class.'' is missing. Execution stopped.';
	exit();
}

// load the requested file
$class = new $class();

if (is_callable(array($class, $action) ) == false )
{
	// function not found in controller , set it as index and send it to args
	array_unshift($args, $action);
	$action = 'index';
}

// Run Action
$class->$action($args);

}
?> 


One Response to “How to Bootstrap PHP Code”

  1. Janine Ketting says:

    function __autoload($class) needs 2 underscores!

Leave a Reply

You must be logged in to post a comment.