Welcome Guest, Not a member yet? Register   Sign In
Newbie Q: Setting Global vars in a controller
#1

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

}
#2

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

}
#3

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

}
#4

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




Theme © iAndrew 2016 - Forum software by © MyBB