Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load view file in core functions in codeigniter

Am trying to load view file in application/core/MY_Controller.php but its giving as follows.

Message: Undefined property: Edustaticlanding::$load

Filename: core/MY_Controller.php

Function is as follows in MY_Controller.php

function menu(){
    $arrr = array();
    return $arrdata['menu'] = $this->load->view('menu',$arrr,true);
}

And am calling this function in controller(Edustaticlanding.php) as follows.

function __construct(){
    $this->menucontent = $this->menu();
    print_r($this->menucontent); die;
}

Pls correct me.. where its going wrong.. thanks

like image 851
Ramesh_Konda Avatar asked Jan 21 '26 02:01

Ramesh_Konda


2 Answers

On application/core/MY_Controller.php

Add public $data = array() like below

<?php

class MY_Controller extends CI_Controller 
{
    public $data = array();

    public function __construct()
    {
        parent::__construct();

        $this->data['menu'] = $this->menu();
    }

    public function menu(){
        return $this->load->view('menu', NULL, TRUE);
    }
}

On the controller then once add public $data = array(); you can access the menu on view

You have to use $this->data now on controller

<?php

class Example extends MY_Controller {

    public $data = array();

    public function index() {

        $this->data['title'] = 'Welcome to Codeigniter';

        $this->load->view('example', $this->data);

    }
}   

On the example view now you can echo

<?php echo $menu;?>

Add extends CI_Controller to your core controller like following codes:

class MY_Controller extends CI_Controller {
like image 25
Mohammad Hamedani Avatar answered Jan 22 '26 16:01

Mohammad Hamedani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!