CodeIgniter: global variables in a controller

2019-03-10 23:17发布

问题:

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 controller

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

As you can see, some array items repeat over and over:

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

Isn't there a way to make them "global" in the controller, so that I have not to type them for each function? Something like (but this gives me error):

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 in advance!

回答1:

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

}


回答2:

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


回答3:

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


回答4:

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


回答5:

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 :-)!