Welcome Guest, Not a member yet? Register   Sign In
$this->load->vars()
#1

[eluser]kidego32[/eluser]
Hello,

I have the following code:

Code:
Class MY_Controller extends Controller {
    
    function MY_Controller() {

        parent::Controller();
        
        // setup default navigation data
        $data['uri_segment_one'] = $this->uri->segment(1);
        $data['uri_segment_two'] = $this->uri->segment(2);
        
        $data['nav1'] = $this->Navigation_model->getNavItems(0);
        $data['nav2'] = $this->Navigation_model->getNavItems($data['uri_segment_one']);
        $data['nav3'] = false;
        $data['nav_active'] = $data['uri_segment_two'];
        $data['title'] = "Door Application";
        
        $this->load->vars($data);
    }
}

I'm extending my application controllers from this MY_Controller. The problem is that I can't access this data in the index() function. Shouldn't this data be available, or am I going about this the wrong way?

Julio
#2

[eluser]sophistry[/eluser]
from the manual:
Quote:$this->load->vars($array)

This function takes an associative array as input and generates variables using the PHP extract function. This function produces the same result as using the second parameter of the $this->load->view() function above. The reason you might want to use this function independently is if you would like to set some global variables in the constructor of your controller and have them become available in any view file loaded from any function. You can have multiple calls to this function. The data get cached and merged into one array for conversion to variables.

i believe that you only get the variables when you are in the view context (view has been "loaded").

if you want to share data through extended classes you should probably use class variables (var $title = 'title here'Wink
#3

[eluser]kidego32[/eluser]
Ok, thanks.
#4

[eluser]mihailt[/eluser]
hmm.. maybe store'em in a static class variable. so you could access it via parent::$YourData ???
#5

[eluser]xwero[/eluser]
I think a class variable is a bad idea because then it's a part of the super object. Because you don't know how all the classes are named that can be added to the super object it's possible you get same name errors.

A way to get the stored vars is to extend the loader class with following method:
Code:
function var($key)
{
   return (isset($this->_ci_cached_vars[$key])) ?  $this->_ci_cached_vars[$key] : NULL ;
}
The usage is
Code:
$this->load->var('some_var');
#6

[eluser]mihailt[/eluser]
[quote author="xwero" date="1227205020"]I think a class variable is a bad idea because then it's a part of the super object. Because you don't know how all the classes are named that can be added to the super object it's possible you get same name errors.
[/quote]

not sure that i got you right , but i think it's not the issue, since static variable is a class variable not object variable and object instance doesn't have acces to it.

Anyway, setting the implementation aside, what needs to be done can be achived by following registry pattern.
#7

[eluser]xwero[/eluser]
Php doesn't make care what type the object is, that is a logical difference. So if you add the variable with the name vars but if you load a library with the same name it's likely somewhere down the line you get bugs.

The loader already keeps an array of the variables for the view files so i don't see why you need another way to store them.
#8

[eluser]mihailt[/eluser]
[quote author="xwero" date="1227207794"]Php doesn't make care what type the object is, that is a logical difference. So if you add the variable with the name vars but if you load a library with the same name it's likely somewhere down the line you get bugs.

The loader already keeps an array of the variables for the view files so i don't see why you need another way to store them.[/quote]

Agree with about CI_Loader, but i still don't think there can be any conflicts since
$someobject->vars, $someobject::$vars and $someobject->vars() are pretty obvious different things, and what i suggested was to use static variable, static variables belong to the class and not to the instance of the class, you cannot access it within an object scope. About the logic part it's actaully not a big difference - both are libraries so it doesn't matter much, the only difference is that in one case you would store variables in parent class and in other case you would store it in superobject.




Theme © iAndrew 2016 - Forum software by © MyBB