<p>This is a Yii-ism, and part of its <a href="https://www.yiiframework.com/doc/guide/2.0/en/concept-di-container" rel="nofollow noreferrer">dependency injection</a> or <a href="https://www.yiiframework.com/doc/guide/2.0/en/concept-configurations" rel="nofollow noreferrer">configuration</a> system, which allows you to dynamically define <a href="https://www.yiiframework.com/doc/guide/2.0/en/concept-components" rel="nofollow noreferrer">components</a> (like <code>session</code> or <code>db</code>). Part of that system involves <a href="https://www.yiiframework.com/doc/guide/2.0/en/concept-behaviors#attaching-behaviors" rel="nofollow noreferrer">attaching Behaviors</a>—in this case, it's declaring the Craft-provided <code>SessionBehavior</code> be attached as <code>session</code>.</p>
<blockquote>
<p> 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 <code>as name</code> configuration shorthand!</p>
</blockquote>
<p>Behaviors are sort of like <a href="https://www.php.net/manual/en/language.oop5.traits.php" rel="nofollow noreferrer">PHP's <code>trait</code> feature</a>, except that they can be attached at runtime—in this case, when a property or method is unavailable on the configured <code>Session</code> component, Yii will comb through any attached Behaviors and look for that same property/method.</p>
<p>This lets Craft inject the <a href="https://github.com/craftcms/cms/blob/develop/src/behaviors/SessionBehavior.php" rel="nofollow noreferrer">extra Session features it depends on</a>, without having to &quot;own&quot; a version of every Yii-compatible Session driver class. Instead, it just keeps those extra features in the <code>SessionBehavior</code> and bolts it on when the app is bootstrapped. You can see the default configuration <a href="https://github.com/craftcms/cms/blob/9a96dc7628ff7a8e77f86d731b6cc021a47aa3a3/src/helpers/App.php#L861-L879" rel="nofollow noreferrer">here</a>, which includes the same <code>as session</code> key and <code>SessionBehavior</code> class.</p>
<blockquote>
<p> Craft actually shims this feature for a few classes that aren't configured or mounted as &quot;components&quot; by providing <code>EVENT_DEFINE_BEHAVIORS</code> to let developers attach extra features.</p>
</blockquote>

CC BY-SA 4.0
2022-02-24T00:12:21.327Z
2022-02-24T00:12:21.327Z
August Miller
<p>When you configure the session component, I see <code>as session</code> used, pointing to the <code>SessionBehaviour</code> class but what does this do or what does it mean?</p>
<pre><code>'session' =&gt; [
     'class' =&gt; yii\redis\Session::class,
     'as session' =&gt; craft\behaviors\SessionBehavior::class,
     'redis' =&gt; 'redis',
],
</code></pre>

<session>
Session Component - Redis
# Session Component - Redis

(Asked by: username_0 on 2022-02-23)

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',
],

**Tags:** session

---

## 1 answers

### Answer by username_1 on 2022-02-24. Score: 1 | Accepted answer

This is a Yii-ism, and part of its [dependency injection](https://www.yiiframework.com/doc/guide/2.0/en/concept-di-container) or [configuration](https://www.yiiframework.com/doc/guide/2.0/en/concept-configurations) system, which allows you to dynamically define [components](https://www.yiiframework.com/doc/guide/2.0/en/concept-components) (like `session` or `db`). Part of that system involves [attaching Behaviors](https://www.yiiframework.com/doc/guide/2.0/en/concept-behaviors#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](https://www.php.net/manual/en/language.oop5.traits.php), 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](https://github.com/craftcms/cms/blob/develop/src/behaviors/SessionBehavior.php), 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](https://github.com/craftcms/cms/blob/9a96dc7628ff7a8e77f86d731b6cc021a47aa3a3/src/helpers/App.php#L861-L879), 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.


---

