Codeigniter MVC Framework PHP

Static Pages In CodeIgniter

Static Pages In CodeIgniter

The primary thing you’re going to do is set up a controller to handle static pages. A controller is basically a class that helps retain work. It is the stick of your web application.

For example, when a call is made to:

http://abc.com/cities/news/10

We might imagine that there is a controller named “cities”. The method being called on cities would be “news”. The cities method’s job could be to grab 10 news items.

http://abc.com/[controller-class]/[controller-method]/[arguments]

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/StaticPages.php with the following code.

<?php

class StaticPages extends CI_Controller {

public function viewPage($page = ‘home’)

{

}

}

You created a class named StaticPages, with a view method that accepts one argument named $page. The StaticPages 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 will become the midpoint 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 transfer 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).

Adding logic to the controller

Earlier you set up a controller with a viewPage() 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/view/ directory.

In that directory, create two files named home.php and aboutus.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 viewPage($page = ‘home’)

{

if ( ! file_exists(APPPATH.’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(‘pages/’.$page, $data);

}

Now, when the page does exist, it is loaded, 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 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.

The last thing that has to be done is loading the views in the order they should be displayed. The second parameter in the viewPage() 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.

Leave a Reply

Your email address will not be published. Required fields are marked *