CodeIgniter Forums
How to autoload sitewide variables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to autoload sitewide variables (/showthread.php?tid=32019)

Pages: 1 2


How to autoload sitewide variables - El Forum - 07-10-2010

[eluser]Christophe28[/eluser]
Hi,

I wonder how to autoload variables to use sitewide like current online users, etc ...

I have read the userguide->autoloader but it seems this is only to autoload helpers, models, etc

Do I have to autoload a library with the online_users() function and include $data['online_users'] = $this->autoload_library->online_users(); in every conroller? There must be a better way?

Can somebody tell me how this is commonly done in codeigniter?

Thank you :-)

Christophe


How to autoload sitewide variables - El Forum - 07-10-2010

[eluser]treeface[/eluser]
You could always make your own library and load it when you need it (or simply load it site-wide in your autoload config file).

http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

You can keep variables, functions, or whatever else you need in there, because it is, effectively, just a class.


How to autoload sitewide variables - El Forum - 07-11-2010

[eluser]LuckyFella73[/eluser]
Hi Christophe,

search the forum for my_controller - you will get lots of helpfull
information and hints how to solve your problem.

Cheers


How to autoload sitewide variables - El Forum - 07-11-2010

[eluser]Christophe28[/eluser]
Hi,

I have searched the forum and userguide for 'my_controller' but can't find any useful information. There must be a simply way to autoload a file or class giving some variables for all pages in one time? Does somebody know how to do this?

I'm new in codeigniter and even oop ... I only have created some controllers, models, views and library, but saw myself creating the same variables over and over again for each page (controllerfunction) like 'online users' etc ...

Christophe


How to autoload sitewide variables - El Forum - 07-11-2010

[eluser]WanWizard[/eluser]
MY_Controller is discussed in the user guide section "Creating core classes".

Alternative is to create a custom config file, define your variables there, and load it using $this->load->config() or via autoload. You can then use $this->config->item() in your controllers to fetch the variables.


How to autoload sitewide variables - El Forum - 07-11-2010

[eluser]Christophe28[/eluser]
Hi,

Thank you for your answer.

But creating a config file, autoloading it would be the same as creating a library and autoloading it, not? I could use the variables in the all different controllers, but my goal is to autoload a file or class, and use the variables directly in the view like for example using {memory_usage} I'd like to create somthing like this {online_users} ...


How to autoload sitewide variables - El Forum - 07-11-2010

[eluser]davidbehler[/eluser]
What you could do is this:

Extend the controller class and have your own _output method. Something like this:
Code:
class MY_Controller extends Controller
{
  function MY_Controller()
  {
    parent::Controller();
  }

  function _output($output)
  {
    $this->load->model('Some_model');
    
    echo str_replace('{online_users}', $this->Some_model->get_online_users(), $output);
  }
}

All your controllers then have to extend MY_Controller instead of Controller. Check the User Guide for more into on this.


How to autoload sitewide variables - El Forum - 07-11-2010

[eluser]Christophe28[/eluser]
Hi,

I have tried some things but got errors all the way Sad

When I used the output function like you mentioned I just got an emtpy webpage ...

Thx anyway.

Best,
Christophe


How to autoload sitewide variables - El Forum - 07-11-2010

[eluser]davidbehler[/eluser]
Mh..should work as my example more or less directly taken from the user guide.

I'll give it a try myself..maybe I can get it to work.


How to autoload sitewide variables - El Forum - 07-11-2010

[eluser]Christophe28[/eluser]
That would be great! I was kind of giving up here ...