How can we simply create a static page in CodeIgniter

| | 1 min read

Some times we need static pages on sites. In CodeIgniter, to show a static page in a site, you have to create a Controller for handling the page display. A Controller is a class which has methods to handle the HTTP request, that is Models and Views in a site.

You need a view for a page to display the html content. For example, create a static page named about_us.php. For this, create a PHP file in [application/controller] folder, and the path will be 'application/controller/about_us.php'.

<?php
class About_us extends CI_Controller {
  public function view() {
    if ( ! file_exists(APPPATH.'/views/pages/about_us.php')) {
      //Whoops, we don't have a page for that!
      show_404();
    }       
    $data['title'] = ucfirst('about us); 
    $this->load->view('templates/header', $data);
    $this->load->view('pages/about_us.php', $data);
    $this->load->view('templates/footer', $data);
  }
}

Create the files such as header.php and footer.php for setting page header and footer, store the files inside the [application/view/templates/] folder. Now, create a view for this controller to show the contents, ie., application/views/pages/about_us.php. Also add the necessary contents for about us page using necessary HTML tags. Route this page in your router file, ie., edit the router file application/config/routes.php as,

$route['about_us'] = 'about_us/view';

Now, when you call www.mysite.com/about_us, it will display the about page with your added content.

I think you have some doubts now, do you? Ping us now from here for support.