Welcome Guest, Not a member yet? Register   Sign In
Where to declare global variables?
#8

[eluser]pickupman[/eluser]
If there are variables that you would like to set for 1 controller you can use
Code:
//Controller
class Something extends Controller{

  var $somevar; //Create a variable for the entire controller

  function Something(){
     parent::Controller();
    
     $this->somevar['message'] = 'Something to store for every method';
     $this->load->vars($this->somevar); //Load into every view
  }

  function mymethods(){
      
     $data['another_message'] = $this->somevar['message']; //Or access variable in methods
     $this->load->view('myview',$data); //Load $data keys into view
  }

}

I use both. config files for what would be static variables to replace define(). Doing something dynamic like a breadcrumb, page title, or user id, I might use the method shown above by putting the code into the constructor of the class. Once you get your feet wet with CI you find you can take this to the next level, by extending the Controller class. You can create custom controller classes for public and private controllers (think frontend/backend). Here's a great explanation.


Messages In This Thread
Where to declare global variables? - by El Forum - 05-14-2010, 01:52 PM
Where to declare global variables? - by El Forum - 05-14-2010, 02:23 PM
Where to declare global variables? - by El Forum - 05-14-2010, 02:25 PM
Where to declare global variables? - by El Forum - 05-14-2010, 02:27 PM
Where to declare global variables? - by El Forum - 05-14-2010, 02:34 PM
Where to declare global variables? - by El Forum - 05-14-2010, 02:45 PM
Where to declare global variables? - by El Forum - 05-14-2010, 03:23 PM
Where to declare global variables? - by El Forum - 05-14-2010, 07:52 PM
Where to declare global variables? - by El Forum - 11-16-2010, 10:16 AM
Where to declare global variables? - by El Forum - 11-17-2010, 04:38 AM
Where to declare global variables? - by El Forum - 11-17-2010, 03:23 PM
Where to declare global variables? - by El Forum - 01-16-2011, 11:39 AM
Where to declare global variables? - by El Forum - 01-18-2011, 09:18 PM



Theme © iAndrew 2016 - Forum software by © MyBB