CodeIgniter Forums
Newbie Q: Setting Global vars in a controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Newbie Q: Setting Global vars in a controller (/showthread.php?tid=30100)



Newbie Q: Setting Global vars in a controller - El Forum - 05-03-2010

[eluser]Unknown[/eluser]
I have a number of vars I'm going to be plugging into $data in each section of this controller. Does anyone know of an easy way to set a global variable just for this controller? Thanks in advance.

Here's the goods:

Code:
<?php

class Gigiem extends Controller {
  
  function index()  {
    $this->load->model('blog_model');
    $this->load->model('work_model');
    $this->load->helper('text');
    
    $data = array(
      'view' => 'html5',
      'css' => array('html5'),
      'js' => array('site'),
      'static' => 'http://www.mysite.com/static/',
      'title' => 'Welcome!',
      'posts' => $this->blog_model->getRecent(2),
      'projects' => $this->work_model->getRecent(2)
    );
  
    $this->load->view('slate', $data);
  }
  
  function work()  {
    $this->load->model('work_model');
  
    $data = array(
      'view' => 'work',
      'css' => array('html5'),
      'js' => array('site'),
      'static' => 'http://www.mysite.com/static/',
      'title' => 'Previous Work',
      'projects' => $this->work_model->getRecent(2)
    );
    
    $this->load->view('slate', $data);
  }

}



Newbie Q: Setting Global vars in a controller - El Forum - 05-03-2010

[eluser]mddd[/eluser]
Define the variable as a class property and set the values in the constructor of your controller.
Code:
<?php

class Gigiem extends Controller {
  
var $data;

function Gigiem()
{

  parent::Controller();

  // here you set the information that is the same for all the methods in this controller
  $this->data = array(
      'view' => 'html5',
      'css' => array('html5'),
      'js' => array('site'),
      'static' => 'http://www.mysite.com/static/',
      'title' => 'Welcome!',
      'projects' => $this->work_model->getRecent(2)
  );

}

function index()  {

  $this->load->model('blog_model');
  $this->load->model('work_model');
  $this->load->helper('text');
    
  // here you add the information that is unique to this method (index)
  $this->data['title'] = 'welcome';
  $this->data['posts'] = $this->blog_model->getRecent(2);

  $this->load->view('slate', $this->data);
}
  
function work()  {

  $this->load->model('work_model');
  
  // here you add the information that is unique to this method (index)
  $this->data['title'] = 'previous work';

  $this->load->view('slate', $this->data);
}

}



Newbie Q: Setting Global vars in a controller - El Forum - 05-03-2010

[eluser]erik.brannstrom[/eluser]
I'm not sure exactly what it is you wish to achieve? Do you mean that you don't want to repeat setting for example the css and js values in multiple methods? If so, this should do the trick.

Code:
<?php

class Gigiem extends Controller {
  private $data = array(
      'css' => array('html5'),
      'js' => array('site'),
      'static' => 'http://www.mysite.com/static/',
      'title' => 'Welcome!'
  );

  // index() ...

  function work()  {
    $this->load->model('work_model');
  
    $this->data['view'] = 'work';
    $this->data['title'] = 'Previous Work';
    $this->data['projects'] = $this->work_model->getRecent(2);
    
    $this->load->view('slate', $this->data);
  }

}



Newbie Q: Setting Global vars in a controller - El Forum - 05-03-2010

[eluser]Unknown[/eluser]
Ah excellent! Thanks Smile