CodeIgniter: global variables in a controller

前端 未结 5 1210
一整个雨季
一整个雨季 2020-12-24 13:56

I\'m a very newbie on CodeIgniter, and while I go on I run into problems that, in the procedural coding, were easy to fix

The current issue is: I have this controlle

相关标签:
5条回答
  • 2020-12-24 14:34

    Why not user a helper?

    File:

    /application/helpers/meta_helper.php
    

    Content:

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

    In your controller:

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

    And in your view template:

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

    Will output:

    <title>My Specific Page Title</title>
    

    Hope that makes sense :-)!

    0 讨论(0)
  • 2020-12-24 14:36

    What you can do is make "class variables" that can be accessed from any method in the controller. In the constructor, you set these values.

    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);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-24 14:41

    You can setup a class property named data and then set it's default values into the contructor which is the first thing which is run when a new instance on Basic is created. Then you can reference to it with the $this keyword

    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);
       }
    }
    
    0 讨论(0)
  • 2020-12-24 14:44

    Even though its been so long. It can be helpful to other you can use $this->load->vars($data); in core MY_controller to make $data array available in all views.

    /* 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');
    }
     }
    
    0 讨论(0)
  • 2020-12-24 14:52

    hey thanks here's my snipet it's a global variable holding a view

    /* 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');
        }
    }
    
    0 讨论(0)
提交回复
热议问题