Welcome Guest, Not a member yet? Register   Sign In
How does one to extend Controller class?
#6

[eluser]JoostV[/eluser]
If you have a lot of controllers that must use a certain method you can create that method in a model or a library.

Call that method in the __construct() of every controller that needs it. Or you can autolad the library if EVERY controller needs it.

EXAMPLE LIBRARY
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Somelib
{
    public function __construct()
    {
        // Instantiate the CI libraries so we can work with them
        $CI =& get_instance();
        // Place some array in the controller's $data array
        $CI->data['somelib'] = array(0 => 'zero', 1 => 'one');
    }
}
EXAMPLE CONTROLLER
Code:
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
class Somecontroller extends Controller
{
    /**
     * Contains all data to be passed to view
     * @var array $data
     */
    public $data = array();

    function __construct ()
    {
        parent::Controller();
        // Load library
        $this->load->library('somelib');
    }
}


Messages In This Thread
How does one to extend Controller class? - by El Forum - 08-30-2009, 09:05 AM
How does one to extend Controller class? - by El Forum - 08-30-2009, 09:09 AM
How does one to extend Controller class? - by El Forum - 08-30-2009, 09:38 AM
How does one to extend Controller class? - by El Forum - 08-30-2009, 09:49 AM
How does one to extend Controller class? - by El Forum - 08-30-2009, 11:49 AM
How does one to extend Controller class? - by El Forum - 08-30-2009, 12:07 PM



Theme © iAndrew 2016 - Forum software by © MyBB