i have a problem when i want to get $_SESSION['session'];
in twig template using slim micro Framework.
this is my code :
<!DOCTYPE html>
<html>
<head>
<title>{{ title }} </title>
</head>
<body>
<p> welcome <?php echo $_SESSION['username']; ?>
<p> {{ body }} </p>
<a href="http://localhost/slim/public_html/logout">logout</a>
</body>
</html>
i can't get session username with that code.
any suggestion how to passing session to twig template?
in php file:
in twig file:
You should pass the value from your PHP file into Twig via associative array.
You should register session as a twig global, so it becomes accessible in your templates.
In your template:
I'm using Slim and Twig as well. My class:
As you can see I've added
addGlobals
. Now it works as it should and I can use{{session.user_id}}
and so.A part of my index.php:
I hope it will help you.
But is it safe to use 'global' in Twig ?
This how I was able to achieve it with Slim Framework ver3
And then access the session in the Twig template like