CodeIgniter User Guide Version 2.1.4


Tutorial - Hard coded pages

The first thing we're going to do is setting up a controller to handle our hard coded pages. A controller is a class with a collection of methods that represent the different actions you can perform on a certain object. In our case, we want to be able to view a page.

Note: This tutorial assumes you've downloaded CodeIgniter and installed the framework in your development environment.

Create a file at application/controllers/pages.php with the following code.

If you're familiar with PHP classes you see that we create a Pages class with a view method that accepts one parameter, $page. Another interesting observation is that the Pages class is extending the CI_Controller class. This means that the new Pages class can access the methods and variables defined in the CI_Controller class. When you look at this class in system/core/controller.php you can see this class is doing something really important; assigning an instance from the CodeIgniter super object to the $this object. In most of your code, $this is the object you will use to interact with the framework.

Now we've created our first method, it is time to do some basic templating. For this tutorial, we will be creating two views to acts as our footer and header. Let's create our header at application/views/templates/header.php and ad the following code.

Our header doesn't do anything exciting. It contains the basic HTML code that we will want to display before loading the main view. You can also see that we echo the $title variable, which we didn't define. We will set this variable in the Pages controller a bit later. Let's go ahead and create a footer at application/views/templates/footer.php that includes the following code.

Adding logic to the controller

Now we've set up the basics so we can finally do some real programming. Earlier we set up our controller with a view method. Because we don't want to write a separate method for every page, we made the view method accept one parameter, the name of the page. These hard coded pages will be located in application/views/pages/. Create two files in this directory named home.php and about.php and put in some HTML content.

In order to load these pages we'll have to check whether these page actually exists. When the page does exist, we load the view for that pages, including the header and footer and display it to the user. If it doesn't, we show a "404 Page not found" error.

The first thing we do is checking whether the page we're looking for does actually exist. We use PHP's native file_exists() to do this check and pass the path where the file is supposed to be. Next is the function show_404(), a CodeIgniter function that renders the default error page and sets the appropriate HTTP headers.

In the header template you saw we were using the $title variable to customize our page title. This is where we define the title, but instead of assigning the value to a variable, we assign it to the title element in the $data array. The last thing we need to do is loading the views in the order we want them to be displayed. We also pass the $data array to the header view to make its elements available in the header view file.

Routing

Actually, our controller is already functioning. Point your browser to index.php/pages/view to see your homepage. When you visit index.php/pages/view/about you will see the about page, again including your header and footer. Now we're going to get rid of the pages/view part in our URI. As you may have seen, CodeIgniter does its routing by the class, method and parameter, separated by slashes.

Open the routing file located at application/config/routes.php and add the following two lines. Remove all other code that sets any element in the $route array.

CodeIgniter reads its routing rules from top to bottom and routes the request to the first matching rule. These routes are stored in the $route array where the keys represent the incoming request and the value the path to the method, as described above.

The first rule in our $routes array matches every request - using the wildcard operator (:any) - and passes the value to the view method of the pages class we created earlier. The default controller route makes sure every request to the root goes to the view method as well, which has the first parameter set to 'home' by default.