PHPackages                             garazol/yii2-add-body-class - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. garazol/yii2-add-body-class

ActiveYii2-component[Utility &amp; Helpers](/categories/utility)

garazol/yii2-add-body-class
===========================

YII2 component which allows us to assign CSS classes to HTML body element from controllers easily

v1.3(9y ago)2855MITPHP

Since Mar 7Pushed 9y ago1 watchersCompare

[ Source](https://github.com/garazol/yii2-add-body-class)[ Packagist](https://packagist.org/packages/garazol/yii2-add-body-class)[ RSS](/packages/garazol-yii2-add-body-class/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (2)Versions (6)Used By (0)

yii2-add-body-class
===================

[](#yii2-add-body-class)

This Yii2 behavior class allows us to assign CSS classes to the layout HTML body element from controllers.

### 1. Download

[](#1-download)

Yii2-add-body-class can be installed using composer. Run following command to download and install Yii2-add-body-class:

```
composer require garazol/yii2-add-body-class
```

### 2. Configure

[](#2-configure)

In the base controller place the behavior definition:

(For detailed examination of special parameters see comments in this block.)

```
// reference to the behavior class:
use garazol\yii2AddBodyClass\components\behaviors\BodyClassBehavior;

// add body class behavior configuration to the behaviors method:
/**
 * @inheritdoc
 */
public function behaviors()
{
    return [
        'BodyClassBehavior' => [
            'class' => BodyClassBehavior::className(),
            // you can define here what kind of classes should be rendered automatically
            // key: AUTO_GENERATE_USER_LOGGED_STATUS can provide user-logged-in or user-logged-out
            // key: AUTO_GENERATE_CONTROLLER_ACTION can provide the current controller and action name classess, e.g.: controller-site and action-index
            // key: AUTO_GENERATE_MODULE can provide the current module, e.g.: module-example
            'autoGeneratedClassTypes' => [
                BodyClassBehavior::AUTO_GENERATE_USER_LOGGED_STATUS,
                BodyClassBehavior::AUTO_GENERATE_CONTROLLER_ACTION,
                BodyClassBehavior::AUTO_GENERATE_MODULE,
            ],
            // you can control the class style
            // for hyphen separated classes use this (default) (results: user-logged-in)
            'classStyle'=>BodyClassBehavior::CLASS_STYLE_HYPHEN,//default
            // for camel case classes use this constant (results: userLoggedIn)
            //'classStyle'=>BodyClassBehavior::CLASS_STYLE_CAMEL_CASE,
        ],
    ];
}
```

In the layout template you can render the composed body classes:

```
