What I normally set the index.php in CodeIgniter
Home » BLOG » Web development » What I normally set the index.php in CodeIgniter

What I normally set the index.php in CodeIgniter

category:  CodeIgniter, Web development

When you are developing the web application, you normally develop on your localhost (your computer). You will set up your ENVIRONMENT differently from production. For example, you will display the PHP report error while you are developing on your localhost but on the production, you will disable the PHP report error. Today I will share how I normally set the index.php for my CodeIgniter project.

Index.php at the webroot

Below is what I normally set the index.php at the webroot. Index.php is an index file (start page) that I set at application/config/config.php/$config[‘index_page’].

index.php

/*
 *---------------------------------------------------------------
 * APPLICATION ENVIRONMENT
 *---------------------------------------------------------------
 */

if( $_SERVER['HTTP_HOST'] == 'MyVirtualDomain.local' ) {
	define('ENVIRONMENT', 'development');	

}elseif( $_SERVER['HTTP_HOST'] == 'MyDomain.com' ){
	define('ENVIRONMENT', 'production');	

}else{
	define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
}

From the code above, I check if the “MyVirtualDomain.local’ is been called on the browser, the ENVIRONMENT is set as development.

But if “MyDomain.com” is been called on the browser, the ENVIRONMENT is set as production.

Below is how I set the ENVIRONMENT for development and production.

/*
 *---------------------------------------------------------------
 * ERROR REPORTING
 *---------------------------------------------------------------
 */
switch (ENVIRONMENT)
{
	case 'development':
		error_reporting(-1);
		ini_set('display_errors', 1);
	        break;
	case 'production':
		ini_set('display_errors', 0);
		if (version_compare(PHP_VERSION, '5.3', '>='))
		{
		        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
		}
		else
		{
			error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
		}
	        break;

	default:
		header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
		echo 'The application environment is not set correctly.';
		exit(1); // EXIT_ERROR
}

The code above is how I set the error reporting for each ENVIRONMENT.

With CodeIgniter, you will see the useful comments on almost every file. It helps you understand what the file is for and how to customize it. This is why I like CodeIgniter for a small web application.

If you are new to CodeIgniter, you can read the CodeIgniter tutorial from here.