笨:在控制器中的全局变量(CodeIgniter: global variables in a co

2019-07-29 09:01发布

我在笨非常新手,而我去我碰上的是,在程序编码,很容易解决的问题

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

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

}

Thnaks提前!

Answer 1:

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

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

}


Answer 2:

你可以设置一个类属性命名的数据,然后将它的默认值到这是一个新的实例时,其上运行的第一件事就是将构造器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);
   }
}


Answer 3:

嘿感谢这里是我的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');
    }
}


Answer 4:

尽管它已经这么久。 它可以帮助其他的你可以用$这个 - >负载>瓦尔($的数据); 核心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');
}
 }


Answer 5:

为什么不用户一个帮手?

文件:

/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>

希望是有道理:-)!



文章来源: CodeIgniter: global variables in a controller