kenjis / codeigniter-ss-twig

A Simple and Secure Twig integration for CodeIgniter 3.x and 4.x
MIT License
168 stars 46 forks source link

Related with Session Data in twig Templates #6

Closed salihkulangara closed 8 years ago

salihkulangara commented 8 years ago

Actually not an issue, but It will be a great help if you guide on this question. How can we use $this->session->set_flashdata(), $this->session->set_userdata() and other session data in our twig templates?

I have tried something like this one:

$datasession = array(
    'nick' => $sess_nick,
    'login_ok' => true
);
$this->session->set_userdata($datasession);
$this->twig->addGlobal("session", $this->session);

and in twig template {{ session.userdata.nick }} but not working!!!

kenjis commented 8 years ago

The below code works on my Mac.

class Welcome extends CI_Controller
{
    public function index()
    {
        $this->load->library('session');
        $this->load->library('twig');

        $datasession = array(
            'nick' => 'Mike',
            'login_ok' => true
        );
        $this->session->set_userdata($datasession);
        $this->twig->addGlobal('session', $this->session);

        $this->twig->display('welcome', []);
    }
}

views/welcome.twig

{{ session.userdata.nick }}
salihkulangara commented 8 years ago

Thank you for your response, actually I was able to fix that issue. But I what I stuck now is with flash data, that is not working as intended. for example I use following code in my controller

public function flash(){
        $this->session->set_flashdata('test_sess', 'Hello Session');  
        $this->twig->addGlobal('session', $this->session);    
        $this->twig->display('test');
    }

in view

  <h1>{{ 'flashdata: ' }}{{ session.flashdata.test_sess }}</h1>

I am able to get the data, its showing up, but when we refresh the page again then also the message is there, normally session data will only be available for the next request, and is then automatically cleared, right? can you just point out what I am doing wrong???

kenjis commented 8 years ago

when we refresh the page again then also the message is there

You set the flash data on the flash page. So everytime you get the page, you see the flash data.

See https://github.com/kenjis/codeigniter-twig-samples/blob/master/application/controllers/Session_sample.php. When you access flash, the flash data is set. So if you go to index (this is the next request), you can see the flash data, but if you refresh the page, the flash message is gone.

kenjis commented 8 years ago

@salihkulangara any feedback?

salihkulangara commented 8 years ago

Sorry about that, here is an example, if i take your sample codes:

http://citest.dev/index.php/session_sample/flash by going to the above link I am expecting the following: 1 - set a session called 'test_sess' 2 - It will display the "test_sess" on "session_sample/flash" view 3- If I refresh the same page (ie: http://citest.dev/index.php/session_sample/flash), I expect the 'test_sess' will be destroyed, but that is not happening

and for the above you have gave me this advice: "You set the flash data on the flash page. So everytime you get the page, you see the flash data."

Ok So What about this

I have slightly changed the flash() in session_sample controller as:

public function flash() { $this->session->set_flashdata('test_sess', 'Hello Session'); $this->twig->addGlobal('session', $this->session); redirect('welcome/flash_test'); }

and in the Welcome controller, I added:

public function flash_test() { $this->twig->display('session_sample/flash'); }

now: if I go to - http://citest.dev/index.php/session_sample/flash

redirects to - http://citest.dev/index.php/welcome/flash_test

but no flash data

[I may be doing somthing utter foolishness]

AND IF I TRY THIS WITHOUT TWIG:::::

ession_sample controller:

public function flash() { $this->session->set_flashdata('test_sess', 'Hello Session'); $this->twig->addGlobal('session', $this->session); redirect('welcome/flash_test'); }

welcome controller: public function flash_test() { $this->load->view('session_sample/flash'); }

in flash.php view:

echo $this->session->flashdata('test_sess');

Everything works fine; Go to url set the flash data, redirect to view, if i refresh session cleared;

I hope you understand what I want to say.

kenjis commented 8 years ago

@salihkulangara I changed the sample as you really want: https://github.com/kenjis/codeigniter-twig-samples/blob/master/application/controllers/Session_sample.php#L27

If I go to http://localhost:8000/session_sample/flash, it redirects to http://localhost:8000/session_sample/flash_test and I see Flashdata: Hello Session. And if I reload the page (session_sample/flash_test), the flash session data is gone.

salihkulangara commented 8 years ago

Thanks man, that's it.