Passing Variables from one controller to its extended controller - 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: Passing Variables from one controller to its extended controller (/showthread.php?tid=56624) |
Passing Variables from one controller to its extended controller - El Forum - 01-07-2013 [eluser]xtremer360[/eluser] I'm trying to figure out how to handle a situation where I have a MY_Controller, Backend_Controller and of course the Backend extends the MY and I have other controllers like Login and other smaller controllers that extend the Backend. My question is if you notice on the login controller it needs to be able to access a variable inside of the backend controller? How can I get that variable to be used in this case? In the backend controller I have this: Code: <?php Here's the login controller: Code: <?php Passing Variables from one controller to its extended controller - El Forum - 01-08-2013 [eluser]Rowan Wilson[/eluser] There are ways to do this either by passing data via the URI, in session data or even by storing the data in the db. However, rather than repeating stuff in both controllers, why not use your core MY_Controller to set the variables and then have both Controllers extend your MY_Controller. That way it keeps things DRY. Your already using it, why not make good use of it. Just a thought. Passing Variables from one controller to its extended controller - El Forum - 01-08-2013 [eluser]xtremer360[/eluser] So I can define the variable in the backed controller and use that variable in my login controller. I don't have to redefine it? Passing Variables from one controller to its extended controller - El Forum - 01-08-2013 [eluser]LuckyFella73[/eluser] Yes you can do that. Code: <?php Passing Variables from one controller to its extended controller - El Forum - 01-08-2013 [eluser]xtremer360[/eluser] I have the following and its not giving me an error anywhere. I am working locally. I have a bet that its not getting tht cms template variable in the login controller but I don't know. I had to remove the <> characters so it'll show that line of code. Code: <?php Code: <?php Passing Variables from one controller to its extended controller - El Forum - 01-08-2013 [eluser]xtremer360[/eluser] I figured out why it wasn't working. It was because I was putting the frontend and backend controllers in the libraries folder instead of the core folder. I do have one more issue at hand. I have a configuration file called defaults.php located in the config folder and I have this config file autoloaded properly. Inside of my backend controller I define a variable called $cms_template and make the value of the variable a config file item value. If I then load the login controller it gives me a 500 error but if I make the value of the cms template variable in the backend controller have the value of 'supr' then it loads the login controller fine? Why is that? Why can't I define the variable as the config item. Backend Controller Code: <?php Login Controller Code: <?php Passing Variables from one controller to its extended controller - El Forum - 01-09-2013 [eluser]LuckyFella73[/eluser] The use of $this-> is only possible from within an object context. Try this way: Code: <?php Passing Variables from one controller to its extended controller - El Forum - 01-09-2013 [eluser]xtremer360[/eluser] Works great thank you. Passing Variables from one controller to its extended controller - El Forum - 01-09-2013 [eluser]xtremer360[/eluser] How would I access the variable in a view because $this->cms_template does not work? |