Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简化CodeIgniter控制器_Codeigniter - Fatal编程技术网

简化CodeIgniter控制器

简化CodeIgniter控制器,codeigniter,Codeigniter,我想把一段代码放在我的CI 2.x core文件夹中,并通过一个基本控制器重用,该控制器将由我的所有其他控制器扩展 以下是出现在每个控制器中的代码,我想转到更中心的位置: $data['navigation'] = generate_navigation(); // helper function $data['country'] = code2country(); // helper function $data['langs'] = $this->select_country_mo

我想把一段代码放在我的CI 2.x core文件夹中,并通过一个基本控制器重用,该控制器将由我的所有其他控制器扩展

以下是出现在每个控制器中的代码,我想转到更中心的位置:

$data['navigation'] = generate_navigation();  // helper function
$data['country'] = code2country();  // helper function
$data['langs'] = $this->select_country_model->get_langs();

// Get copy and images for page
$query = $this->common_model->get_content('markets', 'architectural');

// Load title, description and keywords tags with data
foreach ($query as $row) {
    $data['title'] = $row->page_title;
    $data['description'] = $row->description;
    $data['keywords'] = $row->keywords;
}

如何将其放入基本控制器(my_controller.php)中,然后将数据从扩展控制器发送到视图中。我是否仍然使用
$data[]=
$this->load->view('whatever',$data)

是的,您仍然可以在
$data
变量中传递它,但您需要分配它,以便可以从其他控制器访问它,如下所示:

class MY_Controller extends CI_Controller {

    var $data = array();

    function __construct()
    {
        $this->load->model('select_country_model');
        $this->load->model('common_model');

        $this->data['navigation'] = generate_navigation();  // helper function
        $this->data['country'] = code2country();  // helper function
        $this->data['langs'] = $this->select_country_model->get_langs();

        $query = $this->common_model->get_content('markets', 'architectural');

        foreach ($query as $row) {
            $this->data['title'] = $row->page_title;
            $this->data['description'] = $row->description;
            $this->data['keywords'] = $row->keywords;
        }
    }
}

然后只要用
MY_controller
扩展你的控制器,你就可以用
$this->data
访问
$data
,这样我就可以做$this->load->view('whatever',$this->data')。我刚刚更新了一些代码。每当你想使用
$data
时,你必须用
$this->data
引用它。这适用于主控制器及其扩展的所有控制器。