user2576961
user2576961

Reputation: 415

Passing data from a parent to child controller to a view

I'm having a difficult time passing inherited data from an extended controller to my view in codeigniter application.

I have my backend_controller and my control panel code as the following. I also include what I do in the view below.

When I load the load the page I receive the undefined variable cms name error. I was under the impression I correctly passed the data correctly.

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

class Backend_Controller extends MY_Controller 
{
    public function __construct()
    {
        parent::__construct();
        $this->my_backend();
    }

    public function my_backend()
    { 
        $data['cms_name'] = $this->config->item('cms_name');
    }
}

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

class Control_panel extends Backend_Controller 
{
    /**
     * Loads models, configs, helpers.
     */
    public function __construct()
    {
         parent::__construct();
    }

    /**
     * Loads the control panel.
     */
    public function index()
    {
        $this->template
            ->title('Dashboard')
            ->build('dashboard_view', $this->data);
    }
}

<?php echo $cms_name; ?>

Upvotes: 0

Views: 724

Answers (2)

rid
rid

Reputation: 63442

You likely want to make the $data array an instance property:

class Backend_Controller extends MY_Controller
{
    protected $data = array();
    ...

    public function my_backend()
    {
        $this->data['cms_name'] = $this->config->item('cms_name');
    }
}

Upvotes: 1

Damien Pirsy
Damien Pirsy

Reputation: 25435

You need to create a class property to have it available to the child class:

class Backend_Controller extends MY_Controller 
{
    public $data;

    public function __construct()
    {
        parent::__construct();
        $this->data['cms_name'] = $this->config->item('cms_name');

    }
}

Upvotes: 0

Related Questions