When you configure the session component, I see as session used, pointing to the SessionBehaviour class but what does this do or what does it mean?

'session' => [
     'class' => yii\redis\Session::class,
     'as session' => craft\behaviors\SessionBehavior::class,
     'redis' => 'redis',

1 Answer 1


This is a Yii-ism, and part of its dependency injection or configuration system, which allows you to dynamically define components (like session or db). Part of that system involves attaching Behaviors—in this case, it's declaring the Craft-provided SessionBehavior be attached as session.

🚨 The name is a little confusing, and isn't strictly necessary within the Behaviors system—named behaviors are just easier to detach or access directly, later on. It also appears as though they're required for the as name configuration shorthand!

Behaviors are sort of like PHP's trait feature, except that they can be attached at runtime—in this case, when a property or method is unavailable on the configured Session component, Yii will comb through any attached Behaviors and look for that same property/method.

This lets Craft inject the extra Session features it depends on, without having to "own" a version of every Yii-compatible Session driver class. Instead, it just keeps those extra features in the SessionBehavior and bolts it on when the app is bootstrapped. You can see the default configuration here, which includes the same as session key and SessionBehavior class.

💡 Craft actually shims this feature for a few classes that aren't configured or mounted as "components" by providing EVENT_DEFINE_BEHAVIORS to let developers attach extra features.

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.