【问题标题】:CodeIgniter: global variables in a controllerCodeIgniter:控制器中的全局变量
【发布时间】:2012-05-14 17:26:40
【问题描述】:

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

}

提前致谢!

【问题讨论】:

    标签: arrays codeigniter controller global-variables


    【解决方案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);
        }
    
    }
    

    【讨论】:

    • @Dalen:感谢您纠正这个错字:-)
    • 谢谢你们!同时我忘记了这个问题,因为我发现“$this->load->vars($array)”非常适合我的示例......无论如何,如果我必须在类方法
    【解决方案2】:

    您可以设置一个名为 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);
       }
    }
    

    【讨论】:

    • 您需要将parent::__construct(); 添加到构造函数才能使其工作。
    • 对,如果还没有自动加载,可能还会加载配置文件
    • 比选择的答案更好的例子,虽然它们看起来相同,概念上看起来相似,这里避免了很多重复,所有函数使用成员变量的想法在这个例子中更明显.
    • 这是一个很好的解决方案,非常好。有时事情很容易,但我们不了解它们并面临很多问题。谢谢你的回答。
    【解决方案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');
        }
    }
    

    【讨论】:

      【解决方案4】:

      虽然已经很久了。它可以帮助其他你可以使用 $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');
      }
       }
      

      【讨论】:

        【解决方案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>
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-08
          • 2019-08-02
          相关资源
          最近更新 更多