我很难将继承的数据从扩展控制器传递到我在codeigniter应用程序中的视图。
我有我的backend_controller和我的控制面板代码如下。我还在下面的视图中包含了我的所作所为。
当我加载页面时,我收到未定义的变量cms name错误。我的印象是我正确地正确传递了数据。
<?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; ?>
答案 0 :(得分:1)
您可能希望将$data
数组设为实例属性:
class Backend_Controller extends MY_Controller
{
protected $data = array();
...
public function my_backend()
{
$this->data['cms_name'] = $this->config->item('cms_name');
}
}
答案 1 :(得分:0)
您需要创建一个类属性,以使其可供子类使用:
class Backend_Controller extends MY_Controller
{
public $data;
public function __construct()
{
parent::__construct();
$this->data['cms_name'] = $this->config->item('cms_name');
}
}