How can I pass $_SESSION variable to a view

I’m trying the pass a session variable to a view. I’m using twig-view.
On my route.php file-


$app->get('/adminhome', function($request, $response, $args) {
	session_start();
	//echo $_SESSION['user'];
	return $this->view->render($response, 'test.php');

});

and on test.php -

<?php
session_start()
echo $_SESSION['user'];
?>

but I get nothing in the browser and my php code is commented out.

<!--<?php
session_start()
echo $_SESSION['user'];
?>-->
<html>
   <head></head>
    <body></body>
</html>

What am I doing wrong? Should I use php-view. And is there a better way to do this?

There is a nice short introduction to twig on their homepage: http://twig.sensiolabs.org/. Basically you do not use php code in your templates, but the twig syntax (which is fast & easy to learn, no worries). Better rename your test.php into test.html.twig so it is clear it is intended to use as a twig template.

To pass variables to the view put them in an array and specify said array as the 3rd argument in your render method.

Example code:

$app->get('/adminhome', function($request, $response, $args) {
	session_start();
        $arr["user"] = $_SESSION["user"];
	return $this->view->render($response, 'test.html.twig', $arr);
});

In your test.html.twig simply put:

<h1>Hi {{ user }}!</h1>

I would also recommend you take advantage of the {% extends "layout.html" %} syntax and similar very powerfull features of twig. You’ll find some examples on the webpage from twig too.