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

#1
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 ?
Reply

#2
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
Reply

#3
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'
); 


AppAutoLoadObjects.php:

PHP Code:
class AppAutoLoadObjects {

    private 
$ci;

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

    public function 
initialize()
    {
        
$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
Reply

#4
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.
Reply

#5
A better way would be to extend the controller class with a new MY_Controller.
http://www.codeigniter.com/user_guide/ge...core-class

/application/core/MY_Controller.php
PHP Code:
class MY_Controller extends CI_Controller {

  protected 
$foo;

  function 
__construct()
  {
    
parent::__construct();

    
//load libraries, helpers, models, etc.
    
$this->load->model('some_model');

    
//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 {
  function 
__construct()
  {
    
parent::__construct();

    die(
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
Reply

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

#7
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

Code:
CI_Controller

 - 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
Reply

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

https://github.com/ivantcholakov/codeigniter-registry
Reply

#9
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
Reply

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

Edit: To be more precise:
Code:
$some_result = ClassName::factory()->method();
http://en.wikipedia.org/wiki/Factory_(ob...ogramming)

Or you can make a static facade:
Code:
$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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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