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:
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( AppAutoLoadObjects.php: PHP Code: class AppAutoLoadObjects { 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->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.
http://www.codeigniter.com/user_guide/ge...core-class /application/core/MY_Controller.php PHP Code: class MY_Controller extends CI_Controller { Then your other controllers would extend MY_Controller instead of CI_Controller. PHP Code: class Test extends MY_Controller { Now going to http://yoursite.com/test will show the output of $this->foo, that was created in MY_Controller
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 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.
https://github.com/ivantcholakov/codeigniter-registry
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
04-10-2015, 12:12 PM
(This post was last modified: 04-10-2015, 12:38 PM by ivantcholakov. Edit Reason: A smile strike ) Code: $some_result = ClassName::singleton()->method(); Edit: To be more precise: Code: $some_result = ClassName::factory()->method(); 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. |
Welcome Guest, Not a member yet? Register Sign In |