25

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

}

提前感谢!

4

5 回答 5

41

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

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

}
于 2012-05-14T17:32:11.910 回答
18

您可以设置一个名为 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);
   }
}
于 2012-05-14T17:31:16.007 回答
6

嘿,谢谢,这是我的片段,它是一个全局变量,包含一个视图

/* 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');
    }
}
于 2013-02-11T13:16:34.273 回答
2

虽然过了这么久。它可以帮助其他你可以使用 $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');
}
 }
于 2016-11-15T10:29:23.243 回答
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>

希望这是有道理的:-)!

于 2016-05-16T17:42:12.453 回答