Displaying Static Pages Using CodeIgniter part 2


Displaying Static Pages Using CodeIgniter part 2


On this part of my CodeIgniter notes, I'll still be using the files used on my previous notes and modify them based on the Codeigniter users manual. In fact, this part of my notes is simply an explanation of the codes in the CodeIgniter User's manual, and showing the advantage

In the pages.php (the controller class), I created different methods to load the home.php and about.php. There's no problem in this convention as it is, however, it would be a little bit inconvinient if we are going to make a site with let's say, 5 pages or more.




A better alternative in loading the pages is to create a method that would allow us to select the page that will be loaded. To do this let's modify the Pages controller (pages.php). Instead of having a method for each page, let's make a method that would accept the name of the page to be loaded as an arguement.

Let's name the method 'view'.

class Pages extends CI_Controller {

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

$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);

}
}

Let's break down the code.

public function view($page = 'home')

As you can see the method view accepts an arguement named $page. This means to load a specific page the url would be:

http://localhost/index.php/pages/view/about.

The arguement $page is initialized with the value 'home'. With this initial value, if the url does not contain an arguement (e.g.: http://localhost/index.php/pages/view/), the value 'home' will be used which will, of course, load the home page. This is to prevent CodeIgniter from displaying error messages if there is no arguement on the url.


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

The ucfirst() function capitalized the first letter of the value of the arguement $page and saves it on the array $data['title']. This array is used as a parameter on next three lines which loads the three parts of our page to be displayed, namely the header, the main content and the footer.

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

Checking if the Page exists.

One good practice in web development is always trapping the potential error. To do let's insert a few lines of code and using the 'if' and file_exists functions.



if ( ! file_exists('application/views/pages/'.$page.'.php'))
{

show_404();
}
 


For those who are not quite familiar with the '!' before the file_exists(), it means 'not'. meaning if the file does not exists, it will display the Error 404 page or, you can have some fun and customize your error message.

The Complete Code

class Pages extends CI_Controller{
 
 
 public function view($page = 'home')
{
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{

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);
}

}


Next: Removing the controller and method names from the URL through routing.

Reference: The CodeIgniter Users' Guide.

Comments