CodeIgniter – Extend core CI_Controller

Codeigniter is one of easiest and light weight framework, Codeigniter provides very easy way to extend its core libraries. In many different applications we need to extend core feature of the codeigntier, in this tutorial we will be extending the CI_Controller with very easy and simple steps.

Additionally, I will add view and layout functions in extended core controller so that, we can use those function through the application.

Steps for extending Controller class

1. Download and configure codeigniter latest framework.

2. Create one PHP file under core folder as My_Controller.php

3. Add following code in the file.

<?php if ( !defined('BASEPATH') )
{
    exit('No direct script access allowed');
}

/**
 * MY_Controller extends CI_Controller from the core of the codeigniter, this controller
 * will include default functions which will be used throughout the application
 *
 */
class MY_Controller extends CI_Controller
{

    var $layout = "layout/default"; //default layout
    var $data;

    /**
     * Constructor of the class
     */
    function __construct()
    {
        parent::__construct();
    }

    /**
     * Sets layout of the page
     */
    function _layout($layout)
    {
        $this->layout = (!empty($layout)) ? 'layout/' . trim(strtolower($layout)) : 'layout/default';
    }

    /**
     * Sets view of the page
     */
    function _view($view = 'index', $output = false)
    {
        $this->data['view'] = $view;
        $this->load->view($this->layout, $this->data, $output);
    }
    
}

4. Now create controller file in controllers folder. and put following code there.

<?php if ( !defined( 'BASEPATH' ) )
{
    exit( 'No direct script access allowed' );
}

/**
 * Admin controller manage admin login related functionalities
 *
 * @author Jaidev Bangar <solaxes@gmail.com>
 * @link   http://www.solaxes.com
 */
class Home extends MY_Controller
{
    /**
     * Constructor of the file
     */
    function __construct()
    {
        parent::__construct();
    }

    /**
     * Loads index view with default layout
     */
    function index()
    {       
        $this->_view( 'index' );
    }
    
    /**
     * Load about view with main layout
     */
    function about()
    {        
        $this->_layout( 'main' );
        $this->_view( 'about' );
    }
}
/* End of file Home.php */
/* Location: ./application/controllers/home.php */

5. Create a folder named “layout” in views folder.

6. Create another folder parallel to layout in views named “includes”

7. Add two view files named index.php and about.php in views folder. after creating files and folders we will be having following file structure

8. Add your header and footer code in the “includes/header.php” and “includes/footer.php” file. You can create different header and footer for different layouts as per requirements

9. Add following code to different layouts.

default.php
<?php $this->load->view("includes/header"); ?>
<!-- header file end here -->
<h1>Default Layout</h1> <!-- you can remove this line -->
<?php $this->load->view( $view )?>
<!-- footer start here -->
<?php $this->load->view("includes/footer"); ?>
<!-- footer end here -->

main.php
<?php $this->load->view("includes/header"); ?>
<!-- header file end here -->
<h1>Main Layout</h1> <!-- You can remove this line -->
<?php $this->load->view( $view )?>
<!-- footer start here -->
<?php $this->load->view("includes/footer"); ?>
<!-- footer end here -->

10. That’s it, now you can use _view and _layout function in controller to load layouts and views as you want.

e.g.      $this->_view( 'index' ); 
      This will load index.php view with default layout, and
      $this->_layout( 'main' );
      $this->_view( 'about' );
      This will load about.php view with main layout

 

For more information contact me at solaxes@gmail.com and you can download code Here