CodeIgniter:控制器中的全局变量

时间:2012-05-14 17:26:41

标签: arrays codeigniter controller global-variables

我是CodeIgniter的新手,在我继续操作的过程中,我遇到的问题是,在程序编码中很容易修复

目前的问题是:我有这个控制器

class Basic extends Controller {

    function index(){
        $data['title'] = 'Page Title';
        $data['robots'] = 'noindex,nofollow';
        $data['css'] = $this->config->item('css');
        $data['my_data'] = 'Some chunk of text';
        $this->load->view('basic_view', $data);
    }

    function form(){
        $data['title'] = 'Page Title';
        $data['robots'] = 'noindex,nofollow';
        $data['css'] = $this->config->item('css');
        $data['my_other_data'] = 'Another chunk of text';
        $this->load->view('form_view', $data);
    }
}

如您所见,一些数组项重复:

$data['title'] = 'Page Title';
$data['robots'] = 'noindex,nofollow';
$data['css'] = $this->config->item('css');

是不是有办法让它们成为全球性的"在控制器中,所以我不必为每个函数键入它们? 像(但这给我错误):

class Basic extends Controller {

    // "global" items in the $data array
    $data['title'] = 'Page Title';
    $data['robots'] = 'noindex,nofollow';
    $data['css'] = $this->config->item('css');

    function index(){
        $data['my_data'] = 'Some chunk of text';
        $this->load->view('basic_view', $data);
    }

    function form(){
        $data['my_other_data'] = 'Another chunk of text';
        $this->load->view('form_view', $data);
    }

}

提前提醒!

5 个答案:

答案 0 :(得分:32)

你可以做的是制作可以从控制器中的任何方法访问的“类变量”。在构造函数中,您可以设置这些值。

class Basic extends Controller {
    // "global" items
    var $data;

    function __construct(){
        parent::__construct(); // needed when adding a constructor to a controller
        $this->data = array(
            'title' => 'Page Title',
            'robots' => 'noindex,nofollow',
            'css' => $this->config->item('css')
        );
        // $this->data can be accessed from anywhere in the controller.
    }    

    function index(){
        $data = $this->data;
        $data['my_data'] = 'Some chunk of text';
        $this->load->view('basic_view', $data);
    }

    function form(){
        $data = $this->data;
        $data['my_other_data'] = 'Another chunk of text';
        $this->load->view('form_view', $data);
    }

}

答案 1 :(得分:16)

您可以设置名为data的类属性,然后将其默认值设置为构造函数,这是在创建Basic上的新实例时运行的第一件事。 然后,您可以使用$this关键字

来引用它
class Basic extends Controller
{
   var $data = array();

   public function __construct()
   {
       parent::__construct();
       // load config file if not autoloaded
       $this->data['title'] = 'Page Title';
       $this->data['robots'] = 'noindex,nofollow';
       $this->data['css'] = $this->config->item('css');
   }

   function index()
   {
       $this->data['my_data'] = 'Some chunk of text';
       $this->load->view('basic_view', $this->data);
   }

   function form()
   {
       $this->data['my_data'] = 'Another chunk of text';
       $this->load->view('form_view', $this->data);
   }
}

答案 2 :(得分:3)

嘿,谢谢,这是我的狙击手 它是一个持有视图的全局变量

/* Location: ./application/core/MY_Controller  */

class MY_Controller extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->data = array(
            'sidebar' => $this->load->view('sidebar', '' , TRUE),
        );
    }

}

/* Location: ./application/controllers/start.php */
class Start extends MY_Controller {

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

    public function index()
    {
        $data = $this->data;

        $this->load->view('header');
        $this->load->view('start', $data);
        $this->load->view('footer');
    }
}

答案 3 :(得分:1)

尽管它已经很久了。对其他人可以使用$ this-> load-> vars($ data);在核心MY_controller中,使$ data数组在所有视图中都可用。

/* Location: ./application/core/MY_Controller  */

class MY_Controller extends CI_Controller {

function __construct()
{
    parent::__construct();
    $data['title'] = 'Page Title';
    $data['robots'] = 'noindex,nofollow';
    $data['css'] = $this->config->item('css');
    $this->load->vars($data);
}

}

 /* Location: ./application/controllers/start.php */
class Start extends MY_Controller {

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

public function index()
{
    $data['myvar'] = "mystring";

    $this->load->view('header');
    $this->load->view('start', $data);
    $this->load->view('footer');
}
 }

答案 4 :(得分:0)

为什么不使用帮手呢?

文件:

/application/helpers/meta_helper.php

内容:

<?php 
function meta_data() {
return array("title" => null, "robots" => "noindex, nofollow" );
}

在您的控制器中:

class Basic extends Controller {

    function __construct(){
        parent::__construct();
        $this->load->helper('meta');
    }    

    function index(){
        $data['meta'] = meta_data(); //associate the array on it's own key;

        //if you want to assign specific value
        $data['meta']['title'] = 'My Specific Page Title';

        //all other values will be assigned from the helper automatically

        $this->load->view('basic_view', $data);
    }

在您的视图模板中:

 <title><?php $meta['title']; ?></title>

将输出:

<title>My Specific Page Title</title>

希望有道理: - )!