session in view file |
(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 I would change this world, but God doesn't give me the source.
(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,
You can use the session help like so.
PHP Code: <?= session('item');?> I load my sessions in a BaseController constructor so its always available. 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 |