SaK
SaK

Reputation: 420

Codeigniter: Passing data from parent controller constructor to view( Undefined variable error )

MyController:

class MY_Controller extends CI_Controller {

        public $data = array();

        function __construct() {
            parent::__construct();
            $this->data['errors'] = array();
            $this->data['site_name'] = config_item('site_name');
        }

}

AdminController:

class Admin_Controller extends MY_Controller {

    function __construct() {
        parent::__construct ();
        $this->data ['meta_title'] = 'Admin Panel';
        $this->load->helper ( 'form' );
        $this->load->library ( 'form_validation' );
    }
}

UserController:

class User extends Admin_Controller {

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

        public function login() {
            $this->data['subview'] = 'admin/user/login';
            $this->load->view('admin/_layout_modal', $this->data);
        }
         }

View: _layout_modal.php

$this->load->view($subview);
echo $meta_title;

But both $subview and $meta_title are throwing "Undefined variable" error.

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: subview

Filename: admin/_layout_modal.php

Upvotes: 1

Views: 2154

Answers (2)

Agha Umair Ahmed
Agha Umair Ahmed

Reputation: 1020

try to use $data['subview'] not $this->data['subview'] and when loading the view you use $this->load->view('admin/layout',$data); and in layout file call <?php echo $subview; ?>

Upvotes: 0

尤川豪
尤川豪

Reputation: 479

In default, $this->load->view send the data to browser directly.

It seems you want to load view as string format, not sending to browser directly.

You need to add one parameter to the 'view' function

I think you should change your codes like this:

UserController:

    public function login() {
        $this->data['subview'] = $this->load->view('admin/user/login', true);
        $this->load->view('admin/_layout_modal', $this->data);
    }

View: _layout_modal.php

echo $subview;
echo $meta_title;

Reference:

http://ellislab.com/codeigniter/user-guide/general/views.html

Upvotes: 1

Related Questions