Welcome Guest, Not a member yet? Register   Sign In
New to CI - Having global variables available to every controller?
#1

[eluser]Bleeding Edge Productions[/eluser]
Hi guys,

I just typed a very long message only to have is disappear into the 'ether', so please forgive the brevity of this replacement one.

I am new to CI (and quite new to PHP). I'm following Blanco and Upton's "CodeIgnter 1.7" book, which quite early on has the controller:


Code:
class Start extends Controller {

  var $base;

  function Start() {
    parent::Controller();
    $this->base = $this->config->item('base_url');
  }

  function hello($name = 'Guest') {
    $data['base'] = $this->base;
    $data['mytitle'] = 'Welcome to this site';
    $data['mytext'] = "Hello, $name, now we're getting dynamic!";
    $this->load->view('testview', $data);
  }

}

..which is great.

BUT, I want $base and to be available to EVERY controller, and ideally don't want to have to instantiate the variable every time - or certainly not have to have a separate function in every controller to populate it. Is this possible (ie have the variable instantiated and populated by default, or at least combine the functions), and if so, is it against best practice or something?

I'm sure there's a simple solution which through inexperience I'm just not seeing Smile Any help would be appreciated!

Many thanks,

Martin
#2

[eluser]Bleeding Edge Productions[/eluser]
Sorry - as an aside to this, if the controller is called Start and there is a function of the same name (Start - with a capital) in it, is that function automatically run?

If I had a controller called Start with functions Start and index in, and then go to:

domain.com/index.php/start/

will both Start and index functions be called?

Thanks!
#3

[eluser]theprodigy[/eluser]
The easiest way (in my opinion) to have base available to all controllers, is to create a class in your application/libraries folder called MY_Controller, and have it extend Controller. Then, have all your controllers extend MY_Controller instead of Controller (does that make sense?)

Example:
application/libraries/MY_Controller.php
Code:
class MY_Controller extends Controller  // <--- extends Controller
{
   protected $base;

   public function __construct()
   {
      parent::__construct();   // <---- make sure to call the parent constructor
      $this->base = $this->config->item('base_url');
   }
}

application/controllers/start.php
Code:
class Start extends MY_Controller  // <--- extends MY_Controller
{
   public function __construct()
   {
      parent::__construct();
      // Already contains $this->base through inheritance
   }
}

To answer your question about Class Start and Function Start, yes at least for right now.
In PHP4, the constructors were named the same as the class. In PHP5, they changed the constructors to be __construct() to follow suit with the rest of the PHP Magic Methods that they implemented. As far as I know, PHP only deprecated the old constructor style and not deleted it entirely. I believe it will still look for it in the case of a missing __construct() function. I wouldn't bet on it lasting for too much longer though, so I wouldn't create new code with that style.
#4

[eluser]Rolly1971[/eluser]
to answer the last question, when you call the Start controller the function: Start() automagically gets called.

As for the index function, CI automagically looks for and loads this function if no other function is being looked for in the uri. eg:

example.com/index.php/start

loads: the start controller and calls the index() function.

if you go: example.com/index.php/start/hello

the index() function does not get called automatically. However the Start() still gets automatically called. And because you have: hello as your second uri segment, CI will look for a function called: hello() and run it if it exists.
#5

[eluser]Bleeding Edge Productions[/eluser]
Thank you very much guys! Much appreciated.

So __construct() works if using PHP5, despite CI being 'built for' PHP4?

Thanks.
#6

[eluser]theprodigy[/eluser]
Quote:So __construct() works if using PHP5, despite CI being ‘built for’ PHP4?
CI wasn't built for PHP4. CI was built for PHP5 and backwards compatible for PHP4. You can use PHP5 in your code or you can use PHP4 in your code. Either way, as long as your server supports it, it will work. Now, that being said, because CI was built to support PHP4, it lacks some of the object handling that is in PHP5, only because there was no way to make it compatible with PHP4 if they used it. Other than that, it's pretty much your choice (as well as your hosting providers choice).




Theme © iAndrew 2016 - Forum software by © MyBB