PHP-View
PHP-View copied to clipboard
$_SESSION global variable is not available when rendering $content
How to reproduce:
$_SESSION['user_id'] = 'some_value';
$phpView = new PhpRenderer("path/to/templates", ["title" => "My App"]);
$phpView->setLayout("layout.php");
//...
$phpview->render($response, "hello.php", ["title" => "Hello - My App", "name" => "John"]);
In hello.php
:
<?php var_dump($_SESSION); // Will not be set ?>
Hello <?=$name?>! Your id is: <?= $_SESSION['user_id'] ?>
In layout.php
:
<?php var_dump($_SESSION); // Will be filled as intended ?>
<html><head><title><?=$title?></title></head><body><?=$content?></body></html>
So, in both hello.php
and layout.php
you will be able to get $title
var. But, in hello.php
global $_SESSION var will be empty.
Expected behavior would be leaving global variables intact.
If you need the session in whole or in part you should send it as a parameter to the render method.