I have got a basic slim application with a session_start()
in the index.php.
I gave $_SESSION a value in a class called Validator.php :
class Validator
{
protected $errors;
public function validate($request, array $rules)
{
// filled $errors with data
$_SESSION['errors'] = $this->errors;
return $this;
}
public function failed() {
return !empty($this->errors);
}
}
In another class, I set that variable globally to work inside twig template :
class ValidationErrorsMiddleware extends Middleware
{
protected $container;
public function __invoke($request, $response, $next) {
$twig = $this->container->view->getEnvironment();
$twig->addGlobal('errors', $_SESSION['errors']);
$response = $next($request, $response);
return $response;
}
}
And finally, calling the session variable in my Controller :
class AuthController extends Controller {
public function getSignUp($request, $response)
{
// Render index view
}
public function postSignUp($request, $response)
{
$validation = $this->container->validator->validate($request, [
'email' => v::email()->notEmpty(),
'name' => v::notEmpty()->alpha(),
'password' => v::NoWhitespace()->notEmpty(),
]);
var_dump($_SESSION); // no result
var_dump($validation); //no result
if($validation->failed()) {
return $response->withRedirect($this->container->router->pathFor('auth.signup'));
}
$user = User::create([
'email' => $request->getParam('email'),
'firstName' => $request->getParam('fname'),
'lastName' => $request->getParam('lname'),
'password' => password_hash($request->getParam('password'), PASSWORD_DEFAULT),
'firstLogin' => '1',
]);
return $response->withRedirect($this->container->router->pathFor('home'));
}
}
Inside my Controller, the $_SESSION variable is always null, as if it didn’t have any values inside of it. A var_dump of anything doesn’t work there. But there apparently IS values insinde the session variable as when I var dumb it in my index.php there are results showing up… I’m confused, what am I missing?