• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variables in all areas

Hey guys,

i could need some advice

In my application i've a handful objects, which should accessible in all Areas of CI (views, controllers and models)
The conditions are: 
  • All objects need to be instantiated
  • I need access to the database via get_instance
  • I need access to the session variables
Whats the best practice to accomplish that ?

CI idea is not to preload all libs and functions except if you need them.
How ever you have config/autoload.php autoload configuration where you can list libs and other things which you may autoload all the time and get_instance is the thing that will give you access to all loaded elements with CI.
Its your choice .
Best VPS Hosting : Digital Ocean

yeah i know that

but i was looking for some more flexible method because i've some objects in my session and i don't want to access them via $this->session->userdata(blabla) (because of the fact that it is too long)

so i was looking for a handsome method 

what i did so far is the following:

Creating a hook - for example:
Hooks.php :

PHP Code:
$hook['pre_controller'][] = array(
'class' => 'AppAutoLoadObjects',
'function' => 'initialize',
'filename' => 'AppAutoLoadObjects.php',
'filepath' => 'hooks'


PHP Code:
class AppAutoLoadObjects {


    public function 
$this->ci = &get_instance();

    public function 
$ur = new stdClass();
$ur->exampleVar 1;
$this->ci->ur $ur;

In this example - i have access to $this->ur in all models, controllers and views (basically in my entire CI environment)
But i don't really know if this is a good way to accomplish what i want

Basically the main reason for this question is, i need quick access to some objects (approx. 5) which should be globally available

another way to do this is in the constructor of your controller. if you do something like

PHP Code:
if( ! $this->user $this->appusers->returnUserFromSession() ){

    $this->showNoUserError() ; } 

$this->user is now available to all models and views. The methods for returnUserFromSession() are nice and clean in a model. And if the user object does not come back there is clear path for dealing with it.

A better way would be to extend the controller class with a new MY_Controller.

PHP Code:
class MY_Controller extends CI_Controller {



//load libraries, helpers, models, etc.

//execute code to create variables to be globally available
$this->foo $this->some_model->some_method();

Then your other controllers would extend MY_Controller instead of CI_Controller.
PHP Code:
class Test extends MY_Controller {

var_dump($this->foo)); //$this->foo is available since it was defined in MY_Controller which this class extends

Now going to http://yoursite.com/test will show the output of $this->foo, that was created in MY_Controller

what if $this->foo is not returned from some_model?

thx guys for your answers (i really appreciate this)

i wouldn't ask this question if i've just one controller in my application
or if my data are coming from a model only

my project contains currently about 200 Controllers and i dont want to instantiate this variables in a single controller
nor would i do this in a Base_Controller

my normal current project controller structure looks like


 - MX_Controller
 -- Base_Controller
 --- Session_Controller
 ---- Application_Controller
 ----- Modules_Controller
 ------ Concrete_Controller

So i could load this in the Base_Controller
But the major problem is, that there are some Controllers (4 or 5) which are just extending the CI_Controller

I use HMVC and accessing certain data globally is also an important aspect to be solved. I use a Registry library for this.


you are right, the registry object is by far the cleanest solution
it collides however with my intention to get access to certain objects as fast as possible Wink

$some_result = ClassName::singleton()->method();
Something like this would be nice, but you have to implement it. :-)

Edit: To be more precise:
$some_result = ClassName::factory()->method();

Or you can make a static facade:
$some_result = ClassName::staticMethod();

In addition, you would need to have autoloading organized, based on spl_*() functions. I don't know whether going in this direction is worth-while for you, because CodeIgniter 3 seemingly would not help here, you are on your own.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.