Search code examples
sessionfat-free-framework

Why session message is not working Fat-free-framework?


I wrote a sample crud application and want to show some messages if successful or if error occurs. But when I tried to set a session message it does not displayed to the page after redirect. It also doesn't exists in $_SESSION.

Here is my code in UsersController

public function index(){
    $user = new User($this->db);
    $this->f3->set('users',$user->all());
    $this->f3->set('page_head','User List');
    //get session 
    new Session();
    $sess= $this->f3->get('SESSION.test');
    //set it to use in view file
    $this->f3->set('message', $sess);
    $this->f3->set('view','users/list.htm');
}

public function create(){
    if($this->f3->exists('POST.create')){
        $user = new User($this->db);
        $user->add();
        //set session 
        new Session();
        $this->f3->set('SESSION.test',"Success");
        $this->f3->reroute('/users');
    } else{
        $this->f3->set('page_head','Create User');
        $this->f3->set('view','users/create.htm');
    }
}

and here is my view file.

      <check if="{{ @message }}">
        <div class="alert alert-success">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <strong>{{ @message }}</strong>
        </div>

Its not working. What's wrong with this? Codes are taken from fat free framework

Edit: As xfra35 suggested

Ok, when i used

$f3->set('SESSION.test','Success');

and removed call to

new Session();

this works fine but how to use it directly in layout file instead of setting a message variable in controller to pass it to layout file? I mean should I remove these lines

$sess= $this->f3->get('SESSION.test'); $this->f3->set('message', $sess);

and can use this session message directly in view file. I need to set session message like this

$_SESSION['msg']['success']="Updated";

or $_SESSION['msg']['error']="Error";

I need to loop through session message and view if its error message or success message. Is there any way to make session variable blank once they displayed?


Solution

  • The Session class you're using is a cache-based session handler.

    If no in-memory cache backend (APC, Memcache, Redis, XCache etc..) is found on your server, the framework will use its own filesystem-based cache system. In that case, the files are stored in a subfolder named cache/ inside the TEMP folder. (see here for more details on F3's cache engine)

    So to answer your question: there are chances that the TEMP folder is not writable.

    Anyway if you don't explicitly need to store sessions in cache, just remove calls to the Session class and keep using $f3->set('SESSION.test'): session will be stored on filesystem by PHP (classic behavior).

    Here are the various ways of storing sessions in F3:

    Default PHP session (filesystem):

    $f3->set('SESSION.test','Success');
    

    Cache storage:

    new Session();
    $f3->set('SESSION.test','Success');
    

    SQL storage:

    $db=new DB\SQL($dsn,$user,$pw);
    new DB\SQL\Session($db);
    $f3->set('SESSION.test','Success');
    

    MongoDB storage:

    $db=new DB\Mongo($dsn,$dbname);
    new DB\Mongo\Session($db);
    $f3->set('SESSION.test','Success');
    

    JigDB storage:

    $db=new DB\Jig($dir);
    new DB\Jig\Session($db);
    $f3->set('SESSION.test','Success');