Global Var inside Constructor to pass to view |
[eluser]pyrokinesis[/eluser]
Hi everyone, Is it possible to automatically declare/set a variable in the controller __construct and automatically pass it to the view when every controller method is called??? E.G. -> Code: <? Thanks
[eluser]Dam1an[/eluser]
You would need to use $this to reference the data array in all your functions, like so Code: <?php Also, passing $data into the function is a bad idea, unless you plan on having serialized arrays in the URL?
[eluser]pyrokinesis[/eluser]
Thanks for the reply Dam1an, How about if I do this then? Code: <?php It doesn't seem to work either, I'm trying to set up a default value that is set in the constructor and passed passed to every view unless overwritten from within the specific controller. Thanks for the help
[eluser]n0xie[/eluser]
[quote author="pyrokinesis" date="1251984496"] I'm trying to set up a default value that is set in the constructor and passed passed to every view unless overwritten from within the specific controller. [/quote] You would be better of just extending the Controller Class. In MY_Controller you set all your default values, and unless overwritten from within your controller, it will use those values. It's easier and cleaner. http://ellislab.com/codeigniter/user-gui...asses.html
[eluser]pyrokinesis[/eluser]
Thanks for the help guys... Ended up opting for the simple solution: Code: <? Thanks
[eluser]maicobreako[/eluser]
I was having trouble getting MY_Controller to work and ran across this thread. My question is why does parent::__construct(); work? The only referrence to this I've seen, is this thread. I couldn't get parent::MY_Controller(); to work in the Contact controller, I'd get this message: "Fatal error: Call to undefined method MY_Controller::MY_Controller() in C:\wamp\winf\contact\controllers\contact.php on line 8" BTW, using ci svn version from a few weeks ago. /winf/contact/libraries/MY_Contoller.php Code: class MY_Controller extends Controller Code: class Contact extends MY_Controller
[eluser]maicobreako[/eluser]
To answer my own question, apparently I too quickly skimmed over this line in the user guide. Quote:Note: If you need to use a constructor in your class make sure you extend the parent constructor:
[eluser]Chad Fulton[/eluser]
It looks like this thread has moved away from the original question, but here's an alternate method: Quote:Is it possible to automatically declare/set a variable in the controller __construct and automatically pass it to the view when every controller method is called??? You can do this via the $this->load->vars(); function. I'll modify your original example to demonstrate: Code: <?php
[eluser]Colin Williams[/eluser]
+1 for $this->load->vars() Best function for the job.
[eluser]sineld[/eluser]
Code: <?php this lines saved my life :-) thanks |
Welcome Guest, Not a member yet? Register Sign In |