Introduction

Today we’re going to be setting up and routing a basic web application using one of the most popular enterprise web application frameworks for PHP, CodeIgniter. We’ll create a couple of templates and pages, route them dynamically through the framework’s routing engine, and then set a couple of .htaccess rules to make our URIs more appealing.

Let’s get to it!

Dependencies

First we’re going to need an Apache and PHP stack on our local machine, if you don’t already have Apache and PHP installed check out the following resources and install the package corresponding with your operating system.

Installing CodeIgniter

To install CodeIgniter navigate to the Ellis Lab Website. Download the CodeIgniter zipped package. Once your download is finished unzip the contents of the package into your htdocs folder of your MAMP, XAMPP, or LAMP instance.

You should be able to start your local server and navigate to it in your browser by typing in “localhost”. You’ll notice that there’s a default informational page being displayed. Congratulations CodeIgniter is installed and ready for your development project.

Creating Views

We’re going to create a couple of templates to wrap our views with to keep our code DRY. Create a directory named ‘templates’ in your ‘application/views/’ directory and within that new ‘templates’ directory create two files; header.php and footer.php.

Type the following within header.php:

We’ll be passing a title variable through our controller to dynamically populate the page title.

Type the following within footer.php:

We’re going to be using these template files as a dynamic header and footer for each of our pages views.

Next, we need to create a directory within the views directory called page. So now we should have two directories inside the ‘/views’ directory, ‘/page’ and ‘/templates’.

We’re going to create a home page and an about page inside the ‘/page’ directory.

Create home.php within ‘/page’ and write the following:

Do the same for about.php:

Now we have two pages inside ‘/page’ that only consist of one h1 tag. We also have two template files within ‘/templates’. For the purpose of this tutorial, we’re just going to leave these files as our views and move onto the controller and routes to demonstrate how to display these views in our browser.

Creating A Controller

CodeIgniter is an MVC framework, which means it uses the Model:View:Controller architecture. We’re going to create a Controller to delegate which Views are displayed when a user navigates through our URI structure. It’s best practice in CodeIgniter to use the same name of your directory within views as the name for your corresponding controller so we’re going to name this controller ‘Page’.

Navigate to the ‘application/controllers/’ directory and create a new file called page.php, this will be our controller for static page requests. Write the following within page.php:

Let’s go over the code above.

First, we’re declaring our Page class and we want to extend the class CI_Controller so that we have access to all of CI_Controller’s methods.

Our function view takes a parameter $page and we set that default parameter to home. This parameter will be passed in through our route definition as part of the URI request.

Next, we check to see if $page has a corresponding file name within our ‘/page/’ directory. If it doesn’t we call the show_404() method. If there is a corresponding file for the request we display that file by first passing in our title data in the form of an array. Then, we load our header, corresponding page, and our footer views to dynamically create a full page view. CodeIgniter will append each view to the previous view if multiple views are called within a controller. Notice how we’re passing in the array $data as our second parameter in the method view. This is necessary to populate the title tag of each page as that information is stored within the $data array.

Configuring our App Routes

Open up /application/config/routes.php in your IDE and you will notice that there are two routes already specified within the file.

Delete the default 404 route definition as we have programmatically handled our 404 errors in our Page controller.

Next we’re going to change our default controller to this:

That will select our Page controller as the default when we navigate to our app’s url.

Next we need to define a route for our existing pages as well as any other pages we might create in the future by writing the following:

Above, we’re using a special CodeIgniter string to define this URI route as anything. This specific part of the URI is what get’s passed into our controller as the $page parameter.

Now our application should resolve in the browser.

In order to get to the about page you must type in index.php/about because this is the way CodeIgniter is built to handle URIs. There is a way to fix this with apache mod rewrites.

Rewrites

The last step in this process is creating our rewrites within .htaccess to make our URIs pretty. We can do this by opening up the .htaccess file and then typing the following:

Now if we save and reload our page, we should be able to navigate to /about and see our pages dynamically generate with our header and footer templates along with the data we passed in as title.

Conclusion

That wraps up this introductory tutorial to CodeIgniter, as you can see it’s a great framework for MVC applications in PHP. It’s used widely across the web, and it’s fairly easy to get started developing on. If you have any questions regarding the CodeIgniter framework please reference the documentation Ellis Lab has provided.

Download Source Files