CodeIgniter:控制器中的全局变量

Iva*_*van 22 arrays controller codeigniter 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

难道没有办法让它们在控制器中"全局",所以我不必为每个函数键入它们吗?像(但这给我错误):

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

事先提醒!

Roc*_*mat 34

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

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);
    }

}
Run Code Online (Sandbox Code Playgroud)


Dal*_*len 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);
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 您需要将 `parent::__construct();` 添加到构造函数中才能使其工作。 (2认同)
  • 是的,如果尚未自动加载,可能还会加载配置文件 (2认同)

小智 6

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

/* 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');
    }
}
Run Code Online (Sandbox Code Playgroud)