I'm from custom php world but It looks like I can't just set a session variable in plain php. I'm playing around

use craft\web\Session;
$session = new Session();
$session->set("a", 'b');

or other choice, Craft::$app->getSession()->set("a", "b");

But i don't see it working, I don't see my storage session added in any way.

What am I doing wrong ?

1 Answer 1


The Yii2 components behave a bit like singletons, you shouldn't create new ones manually. Instead, accessing them over the Craft::$app property is preferred, as you indicated. This should work:

$session = Craft::$app->getSession();
$session->set("a", "b");

How are you checking if the session value got set? It's likely you're just looking in the wrong place, the default session component is file-based now if I recall correctly. You can check if a session variable exists and/or what value it holds like this:

  • 1
    That's exactly what I needed ! Thank you very much, it works ! Commented Nov 24, 2021 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.