CodeIgniter Forums
Setting and Getting Session and Flashdata - 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: Setting and Getting Session and Flashdata (/showthread.php?tid=74458)



Setting and Getting Session and Flashdata - msjagan - 09-26-2019

Can anybody guide me how to set and retrieve session and flashdata?

I initialized session base controller and when I try to set and get on User controller, I am getting error saying "ErrorExceptoin": undefined variable $session when using this snippet below

PHP Code:
$session->set('user''someone'); 



RE: Setting and Getting Session and Flashdata - titounnes - 09-26-2019

$session = session();
$session->set('user', 'someone');


RE: Setting and Getting Session and Flashdata - msjagan - 09-26-2019

(09-26-2019, 01:35 AM)titounnes Wrote: $session = session();
$session->set('user', 'someone');

Thank you, it works

Should "$session = session();" be called on each controller?

Is there any way to autoload like CI3 version?



RE: Setting and Getting Session and Flashdata - Digital_Wolf - 09-26-2019

full simple example for CI4:

PHP Code:
<?php
namespace App\Models;

use \
Config\Services\session;

class 
My_Model
{
     private session null;
     
     
public function __construct() {
           $this->session = new session();
     }
     public function setDataSession($data) {
           // ... you code
           $this->session->set$data );
           // ... you another code
     }


Official guide to using the session library: User_Guide


RE: Setting and Getting Session and Flashdata - msjagan - 09-26-2019

(09-26-2019, 02:14 AM)Digital_Wolf Wrote: full simple example for CI4:

PHP Code:
<?php
namespace App\Models;

use \
Config\Services\session;

class 
My_Model
{
     private session null;
     
     
public function __construct() {
           $this->session = new session();
     }
     public function setDataSession($data) {
           // ... you code
           $this->session->set$data );
           // ... you another code
     }


It throws error Sad




RE: Setting and Getting Session and Flashdata - sktiwari19 - 09-26-2019

(09-26-2019, 12:16 AM)msjagan Wrote: Can anybody guide me how to set and retrieve session and flashdata?

I initialized session base controller and when I try to set and get on User controller, I am getting error saying "ErrorExceptoin": undefined variable $session when using this snippet below

PHP Code:
$session->set('user''someone'); 
I was also facing same problem. I got my answer, thaks a lot


RE: Setting and Getting Session and Flashdata - titounnes - 09-26-2019

CI4, different with CI3.
learn about filter.
https://github.com/codeigniter4/CodeIgniter4/blob/develop/user_guide_src/source/incoming/filters.rst

use BaseConttoller for autoload libraries, helper etc.


RE: Setting and Getting Session and Flashdata - msjagan - 09-26-2019

Yes CI4 is way different than CI3.

learning the changes was made on CI4 by applying in sample project.

Also found simple way to set and get session and flashdata. I will share my code with sample project which I am working on once its done.


RE: Setting and Getting Session and Flashdata - sktiwari19 - 09-26-2019

(09-26-2019, 02:53 AM)sktiwari19 Wrote:
(09-26-2019, 12:16 AM)msjagan Wrote: Can anybody guide me how to set and retrieve session and flashdata?

I initialized session base controller and when I try to set and get on User controller, I am getting error saying "ErrorExceptoin": undefined variable $session when using this snippet below

PHP Code:
$session->set('user''someone'); 
I was also facing same problem. I got my answer, thaks a lot
Hello Bro, i have joined this community to improve my skills not for spam


RE: Setting and Getting Session and Flashdata - Digital_Wolf - 09-26-2019

The error appears because you are using CI3, I gave an example for CI4. CI4 has been rewritten and therefore they are incompatible.
If you still have errors, then describe them here, but rather best to provide logs from the folder /writable/logs/ or /writable/debug/ if they are available.