0

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; ?>
4

2 回答 2

1

您可能希望使$data数组成为实例属性:

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

    public function my_backend()
    {
        $this->data['cms_name'] = $this->config->item('cms_name');
    }
}
于 2013-09-12T15:10:21.700 回答
0

您需要创建一个类属性以使其可用于子类:

class Backend_Controller extends MY_Controller 
{
    public $data;

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

    }
}
于 2013-09-12T15:10:13.930 回答