Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Konstantinos Pedarakis
Konstantinos Pedarakis
21,301 Points

sessions in php with Slim framework via twig.

Hi, im trying to find a solution for the session variables in php through my twig templates. how can i have echo the session variables inside my twig templates. i search everywhere, but i cannot find any good solution.

1 Answer

You'll probably want to pull out the values in a controller class or method, and pass it to your view.

Slim Docs - Pass data to a view

I don't think slim has a 'session wrapper' by default, so you'll be calling the session super global to get out your values -

<?php

$_SESSION['some-key'] // some value.

So you could do something like

<?php

$app->view->setData('first_name', $_SESSION['user']['first_name']);
Konstantinos Pedarakis
Konstantinos Pedarakis
21,301 Points

yes! you are right thanks!. now probably i can get the variable first_name as you described there and pass it through my controller , probably with the render function, and then just by doing {{ first_name }} in my twig template i will have my Session variable. Thanks, i will look at it later.