![]() |
session in view file - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: session in view file (/showthread.php?tid=74589) |
session in view file - jbloch - 10-12-2019 How to get session value in view file In CI4? RE: session in view file - Digital_Wolf - 10-12-2019 (10-12-2019, 01:32 AM)jbloch Wrote: How to get session value in view file In CI4?Something like this: PHP Code: // IN CONTROLLER: Session documentation: Link RE: session in view file - Ekley - 10-13-2019 (10-12-2019, 08:03 AM)Digital_Wolf Wrote:(10-12-2019, 01:32 AM)jbloch Wrote: How to get session value in view file In CI4?Something like this: Hello, You have 2 possibilities, it depends upon the number of variables of your session you want to transfer. 1. In the view code directly The context ($this) is an instance of the View class --> you don't have access to the Session instance directly. You have to write a bit of code in the View: PHP Code: $session = \Config\Services::session(); 2. Controller code + view code You can also pass the data during the call of the view() method in the Controller. PHP Code: <?php Then, in the view: PHP Code: <?= $item ?> Should work ![]() -- I prefer method 2, it is more about separating Controller and View from a purpose point of view. Regards, RE: session in view file - InsiteFX - 10-13-2019 You can use the session help like so. PHP Code: <?= session('item');?> I load my sessions in a BaseController constructor so its always available. |