Ali Ali - 3 years ago 271
PHP Question

Slim framework session not recognized globally

I have got a basic slim application with a

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;
var_dump($_SESSION); // doesn't work or show anything
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']);
var_dump($_SESSION); // works
$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 I put a die() here, the var_dump works... if I don't put it, it doesn't. I don't know what that means but maybe you guys got an idea

// If I remove this if statement a user is created even if he
// the validation fails. Not even the HTML form prevents it
// even if all fields are blank
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 either. But there apparently IS values inside 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?

Answer Source

I think that your problem comes from within the validate function or how you're using it to be exact. But without further tests it's not possible to understand from where is your problem coming from.

I would refer you to go to the Respect Validator page and check their custom rules.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download