Creating a Web Application with PHP and CodeIgniter Part 3
Home » BLOG » Web development » Creating a Web Application with PHP and CodeIgniter – Part 1

Creating a Web Application with PHP and CodeIgniter – Part 1

category:  CodeIgniter, Web development

A few weeks ago, I was assigned a new project. The requirements were to create a new small application in a tight deadline. The application needed to be secure, lightweight, fast, and quickly adding new features in a small amount of time. I came up with some PHP frameworks (CodeIgniter3, Yii2, CakePHP, and Laravel) and JS frameworks (Reactjs). In the end, the winner was CodeIgniter.

If you are a PHP Developer, you may hear one of the popular PHP frameworks which are CodeIgniter. CodeIgniter started in 2006. The latest version just released in February 2020 is CodeIgniter4. CodeIgniter4 is brand new and still has some issues that need to be fixed before using in production. So, I will write this post using only CodeIgniter3. Let’s start!

Installation

Server Requirements

Before install CodeIgniter3, you should check the server requirements below.

  • PHP version 5.6 or newer is recommended

I would suggest running on PHP version 7.x to avoid security and performance issues.

Download CodeIgniter3

Download the current version of CodeIgniter3 from this link

Set Up CodeIgniter3 at your web root

After downloading the files from the link in Download CodeIgniter3 section above, extract the zip file and copy all files that you extract to your webroot. The files are;

  • application folder
  • system folder
  • user_guide folder (I remove this folder since it is documentation)
  • .editorconfig
  • .gitignore
  • composer.json
  • contributing.md
  • index.php
  • license.txt
  • readme.rst

I use a wamp server so my webroot is the www folder. You can use any web server you like. In my case, I copy all files above to my www folder.

Configure your application

To configure your application, you need a text editor. I use VSCode. Feel free to use any text editors you like.

Set your base URL

  • Open the application/config/config.php
  • Set your base URL at $config[‘base_url’]. In my case, I create a virtual host as http://codeigiter3-tutorial.local. So I will add my base_url as http://codeigiter3-tutorial.local/. You must set the base URL and adding a trailing slash at the end of your base URL.

Set your database

  • Open the application/config/database.php
  • Set your database settings in $db[‘default’] array. I use MariaDB so my hostname will be “localhost:3307”. If you use MySQL, your hostname will be “localhost”. If you use other databases, you need to check what is your hostname and port for the database.
$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost:3307',
	'username' => 'root',
	'password' => '',
	'database' => 'ci3_tutorial',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8mb4_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);

Note that, your CodeIgniter3 application can connect multiple databases and use them in the same application.

Increase security by changing the file structure.

By default, the application and system folders are under the webroot. Both folders have their own .htaccess file to prevent directly accessible via a browser. However, it may be some case that the web server configuration changes or don’t follow the .htaccess file. So, we will move the application and system folders to be placed above the webroot.

Also, we will move the view folder under the application folder to be outside the application folder and rename it as “public”. So the browser can access the view folder after moving the application folder outside the webroot. You don’t need to rename as “public” since it is my personal preference.

Next, after moving the application, system, and view folder, we need to set the associated path in the index.php at the webroot. You should set the full path.

Before moving the application, system and view folders:

  • webroot (public_html or public or www folder depends on your web hosting setting)
    • application folder
    • system folder
    • index.php
    • etc

After moving the folders:

  • application folder
  • system folder
  • webroot (public_html or public or www folder depends on your web hosting setting)
    • public folder (view folder)
    • index.php
    • etc

Below showing where to set the system, application and view folder path:

Edit the $system_path$application_folder and $view_folder variables with full path in the index.php. You will need to find what is your full path from your web hosting.

  • Open the index.php
  • Set the $system_path as “/www/yourdomainname/system
  • Set the $application_folder as “/www/yourdomainname/application
  • Set the $view_folder as “/www/yourdomainname/public

Load different configuration depending on your current environment

When you are developing the application, you normally develop on your localhost. On your localhost, you want to enable PHP error reporting and any other development functionality you want.

In CodeIgniter, you can load different configurations depending on your current environment. CodeIgniter offers 3 environments (development, testing, production) as the default. Of cause, you can set more environments you want.

In index.php, you can set the environment you want to work on. Simply set the $_SERVER[‘CI_ENV’] to development, testing, or production. By default, if $_SERVER[‘CI_ENV’] is not set, the default environment is development.

The code in the index.php will look like this.

/*
 *---------------------------------------------------------------
 * APPLICATION ENVIRONMENT
 *---------------------------------------------------------------
 *
 * You can load different configurations depending on your
 * current environment. Setting the environment also influences
 * things like logging and error reporting.
 *
 * This can be set to anything, but default usage is:
 *
 *     development
 *     testing
 *     production
 *
 * NOTE: If you change these, also change the error_reporting() code below
 */
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

/*
 *---------------------------------------------------------------
 * ERROR REPORTING
 *---------------------------------------------------------------
 *
 * Different environments will require different levels of error reporting.
 * By default development will show errors but testing and live will hide them.
 */
switch (ENVIRONMENT) {
	case 'development':
		error_reporting(-1);
		ini_set('display_errors', 1);
		break;

	case 'testing':
	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
}

In the switch command, you can create more environments or edit some settings associate with the default environments.

Run the application

Now you are ready to view your application on your browser. On your browser, type your base URL and hit Enter. In my case, my base URL is “http://codeigiter3-tutorial.local/“. The result I see on my browser is:

CodeIgniter3 First App

Troubleshooting

If you have any issues with REQUEST_URI, you may need to add a question mark to your URLs as below.

  • Open application/config/config.php
  • change $config[‘index_page’] = “index.php”; to $config[‘index_page’] = “index.php?”;

Conclusion

In CodeIgniter3 tutorial part 1, we learn how to download and install the CodeIgniter application at the webroot. We also learn how to configure the base URL and database. Plus, we learn how to set up the file structure for the best practice security.

CodeIgniter3 tutorial series: