1

I have created MY_Controller.php in the core folder. I autoloaded all the libraries and model that i want in my website. And also put some data in the MY_Controller.php. but when I extend this controller by another controller the data cannot be extended in the second controller. This is MY_Controller.php code.

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
   public $data;
   public function __contstruct()
    {
        parent::__construct();
        date_default_timezone_set('Asia/Karachi');
        $this->load->library(array('ion_auth', 'form_validation','form'));
        /*-Website Developer Information-*/

        $this->data['FullName']="Waqas Dev lover";
        $this->data['ShortName']="Dev Lover";
        $this->data['Mobile']="03049211134";
        $this->data['Version']="1.0.1";
        $this->data['Copyright']="Waqas Dev Lover";
        $this->data['Year']=date("Y");
        $this->data['DevelopedBy']="Waqas Dev Lover";
        $this->data['DevelopedByUrl']="http://www.facebook.com/xndltwaqas1";
        /* -End of Website Developer Information- */
        /*System Information */
        $this->data["SectionH1"]="Admin panel";
        /*End of System Information */

        /*User Information */
       $user_id =$this->session->userdata('user_id');
       //echo  =$this->ion_auth->logged_in();
       //exit;
        $this->data['user'] = $this->ion_auth->user($user_id)->row();
        //$this->get_user_groups($user_id);
        $this->data['group']=$this->ion_auth_model->get_users_groups($user_id)->result();
        //var_dump($this->data['group']);
        //exit();
        /*End User Information */
    }

     public function show($path,$data=NULL){
        if($data === NULL){
            $data = $this->data;
            var_dump($data); exit;
        }
        $this->load->view("template/header",$data);
        $this->load->view("template/sidebar",$data);
        $this->load->view($path,$data);
        $this->load->view("template/footer",$data);
    }  
}
?>

And this is the Dashboard.php in which I want to extends the MY_Controller.php. But it Won't extends the Data in the Dashboard.php.

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

class Dashboard extends MY_Controller{
    public function index(){
        $this->show("admin/home");
    }
}
1
  • take a look at your line __contstruct() in your MY_Controller class - if this isn't a typo you should alter it to __construct(). Commented Dec 17, 2018 at 8:34

1 Answer 1

1

You can already add, append or reset your $this->data from MY_Controller without the need to pass extra data array to show.

public function show($path)
{
    $this->load->view("template/header", $this->data);
    $this->load->view("template/sidebar", $this->data);
    $this->load->view($path, $this->data);
    $this->load->view("template/footer", $this->data);
}

and in your dashboard you can use it directly:

class Dashboard extends MY_Controller
{
    public function index()
    {
        $this->data['foo'] = 'bar';
        $this->show("admin/home");
    }
}

you can now access $foo in your dashboard view

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.