Faster PHP Web Development With an MVC Approach

How to use CodeIgniter Development Tools for Custom Websites

MVC stands for Model, View, and Controller – the 3 main aspects of any website in today’s Internet. It is a growing preference among coders who wish to have sturdy, easy to maintain code.

The Model portion of MVC frameworks talks to the database, the View is the piece that handles the actual appearance of the pages, and the Controller does just that – it controls the Model and View code to bring it all together.

CodeIgniter

CodeIgniter is yet another MVC framework in PHP. One of its strongest features is the fact that it is light and fast – a must have for any website which requires a bit more processing such as those implementing e-Commerce applications, or travel companies which hold a great amount of itinerary and schedule information. CodeIgniter works with a wide variety of databases such as MySQL, MSSQL, Oracle, PostgreSQL, SQLite natively. It also supports connections through ODBC.

CodeIgniter can be found here. It has a complete user guide, and any and all doubts can be discussed in the forum.

Installing and Configuring CodeIgniter

Installing CodeIgniter only requires that the developer download the latest Zip file and unpack it in the desired directory. There is a single index.php file in the root folder, which acts as a gateway to the Controller. Once downloaded and unpacked, the next step is to modify the application/config/config.php file with the basics for a functioning setup.

The first option to be modified is the variable $config[‘base_url’]. This indicates which domain will hold the website. An example of such value is: http://example.com/. The trailing slash here is important. There are several other options which can be customized, each one very well documented. $config[‘base_url’], however, is the only one required in order for the application to work properly.

If the developer intends to use a database, then he or she must edit application/config/database.php and set the hostname, username, password, and driver to be used.

Basic Building Blocks

The creators of CodeIgniter were thoughtful to add functions for just about every basic requirement of a web application: handling forms, keeping sessions, sending emails, manipulating images, code benchmarking, and much more. They even added a basic Shopping Cart class to speed up building e-Commerce sites.

URI handling

Every serious website takes SEO – Search Engine Optimization – into consideration. One of the primary points to get prime spots in the most popular web search engines is to have friendly URLs. Friendly URLs are ones that do not show the query portion in the traditional way:

http://example.com/index.php?key1=value1&key2=value2but instead show them separated by slashes as if they were files or directories in a path:

http://example.com/index.php/key1/value1/key2/value2

CodeIgniter applies friendly URLs in a way that you can easily identify which class and methods are being called, as well as the parameters passed to them, based on each segment of the URL.

http://example.com/index.php/class/method/parameter1/parameter2

It also provides an option for rerouting methods if more complex logic is required, as well as the ability to pick any given segment by it’s position number at any given time.

HTTP, PHP and Sessions

HTTP is stateless by default. This means that there is no built-in form of remembering data from one page to another. There are several forms of achieving state – sessions are one of them. PHP has built-in session functions which save a session identifier in a cookie in the user’s browser, and the data on the server.

CodeIgniter does not use these built-in session functions. It provides its own session methods which can be configured to save session data either in a relational database or directly inside the user’s cookie. Using the database to store session data is recommended, since cookies can only hold a maximum of 4Kb of data. When using the database option, CodeIgniter behaves like PHP’s native functions – only an identifier is kept in the cookie.

Forms and Security

The most common way for users to interact with web applications is through the use of forms. Forms can also be the most dangerous aspect of a web site, allowing crackers to submit data that can be interpreted as code. This is commonly called XSS – Cross Site Scripting and has been a major source of headache for programmers that haven’t carefully untainted user submitted values.

CodeIgniter provides more than one solution for this problem. Its form validation class allows extensive checks on data. It can take any native PHP function to apply against user submitted values and even chain several together. If the form validation class finds an error, it either returns the default error message or any other defined by the programmer. This significantly improves safety and speed when using forms.

Another option to handle user input is through the input class. Traditionally, PHP programmers rely on $_REQUEST, $_POST, $_GET, $_SERVER, or $_COOKIE arrays. When using CodeIgniter, one should avoid calling these arrays directly and instead call the input class which is always loaded by default.

  • $post_field = $this->input->post(‘some_field’); // fetches from $_POST
  • $get_field = $this->input->get(‘some_field’); // fetches from $_GET
  • $get_or_post = $this->input->get_post(‘some_field’); // fetches from $_REQUEST
  • $server_field = $this->input->server(‘some_field’); // fetches from $_SERVER
  • $cookie_data = $this->input->cookie(‘some_field’); // fetches from $_COOKIE

There are other helpful functions in the input class that fetch the user’s IP address, validate a given IP address, and even quickly returns the user’s browser information.

Conclusion

CodeIgniter is a handy tool to build any web application quickly. There is still much more to it than what was covered in this article such as image manipulation and benchmarking classes. A quick look at the well documented user guide can enlighten PHP programmers to its classes and methods in order to achieve better performance and less development time.