Variable dedclared in controller available in view |
10-15-2021, 03:50 AM
(This post was last modified: 10-15-2021, 10:03 PM by CodingInCodeigniter. Edit Reason: Typo )
In my project i have my website settings saved in a DB table. I need to use these settings throughout the website.
Previously in Ci3 i would do the following in the beginning of my controller Code: <?php $this->settings would then be available to me in the controller as well as the views. Im trying to replicate this is Ci4 but im struggling to get it to work. Ive tried this: Code: <?php When i use it in my view like this: Code: <title>Administration for <?php echo $this->site_settings['site_name_title']?></title> Undefined property: CodeIgniter\View\View::$site_settings Can you please tell me what im doing wrong/not understanding
You did it wrong in CI3 too. You should pass $this->settings to the view in a data array. Then access $settings in the view.
Something like: PHP Code: return view('your_view', ['settings' => $this->settings]); See this tutorial in the user guide for an example: https://codeigniter.com/user_guide/tutor...y-the-news
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Gosh, ive been using it like that for years in Ci3 across tons of projects and it worked like a charm.
I am familiar with passing data to the view and i would use that to pass values initiated by that function but because every function would need $this->settings, i set it in my constructor. Thank you for your reply. Ill have to figure this one out.
I guess it work in CI3 because everything is loaded in the main CI object. In CI4 the architecture is very different. There's no global CI object.
When I have data I need in every controllers, I load them in a data array in the BaseController, then each controller add whatever they need to that array and pass it to the view. See my tutorial on multilingual website for an example of what I mean. Search the viewData variable: https://includebeer.com/en/blog/creating...r-4-part-1
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
10-17-2021, 12:49 AM
(This post was last modified: 10-17-2021, 12:54 AM by InsiteFX. Edit Reason: fixed spacing )
This is a merge array method I created for the BaseController.
PHP Code: <?php What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |