Example: Create some static pages in codeigniter

The first thing you’re going to do is set up a controller to handle static pages. A controller is simply a class that helps delegate work. It is the glue of your web application.

For example, when a call is made to: http://example.com/news/latest/10

We might imagine that there is a controller named “news”. The method being called on news would be “latest”. The news method’s job could be to grab 10 news items, and render them on the page. Very often in MVC, you’ll see URL patterns that match:http://example.com/%5Bcontroller-class%5D/%5Bcontroller-method%5D/%5Barguments%5D

As URL schemes become more complex, this may change. But for now, this is all we will need to know.

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

<?php

class Pages extends CI_Controller {

public function view($page = ‘home’)
{

}
}

You have created a class named “pages”, with a view method that accepts one argument named $page. 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 (system/core/Controller.php).

The controller is what will become the center of every request to your web application. In very technical CodeIgniter discussions, it may be referred to as the super object. Like any php class, you refer to it within your controllers as $this. Referring to $this is how you will load libraries, views, and generally command the framework.

Now you’ve created your first method, it’s time to make some basic page templates. We will be creating two “views” (page templates) that act as our page footer and header.

Create the header at application/views/templates/header.php and add the following code.

<html>
<head>
<title><?php echo $title ?> – CodeIgniter 2 Tutorial</title>
</head>
<body>
<h1>CodeIgniter 2 Tutorial</h1>

The header contains the basic HTML code that you’ll want to display before loading the main view, together with a heading. It will also output the $title variable, which we’ll define later in the controller.

Now create a footer at application/views/templates/footer.php that includes the following code:

<strong>&copy; 2011</strong>
</body>
</html>

Adding logic to the controller

Earlier you set up a controller with a view() method. The method accepts one parameter, which is the name of the page to be loaded. The static page templates will be located in the application/views/pages/ directory.

In that directory, create two files named home.php and about.php. Within those files, type some text − anything you’d like − and save them. If you like to be particularly un-original, try “Hello World!”.

In order to load those pages, you’ll have to check whether the requested page actually exists:

public function view($page = 'home')
{

	if ( ! file_exists('application/views/pages/'.$page.'.php'))
	{
		// Whoops, we don't have a page for that!
		show_404();
	}

	$data['title'] = ucfirst($page); // Capitalize the first letter

	$this->load->view('templates/header', $data);
	$this->load->view('pages/'.$page, $data);
	$this->load->view('templates/footer', $data);

}

Now, when the page does exist, it is loaded, including the header and footer, and displayed to the user. If the page doesn’t exist, a “404 Page not found” error is shown.

The first line in this method checks whether the page actually exists. PHP’s native file_exists() function is used to check whether the file is where it’s expected to be.show_404() is a built-in CodeIgniter function that renders the default error page.

In the header template, the $title variable was used to customize the page title. The value of title is defined in this method, but instead of assigning the value to a variable, it is assigned to the title element in the $data array.

The last thing that has to be done is loading the views in the order they should be displayed. The second parameter in the view() method is used to pass values to the view. Each value in the $data array is assigned to a variable with the name of its key. So the value of $data[‘title’] in the controller is equivalent to $title in the view.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s