Search code examples
phpoopyii2yii-componentsyii2-advanced-app

Override Yii2 assetManager config in controller


I use yii-jui to add some UI elements in the views such as datePicker. In the frontend\config\main-local.php I set the following to change the theme used by the JqueryUI:

$config = [
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'gjhgjhghjg87hjh8878878',
        ],
        'assetManager' => [
            'bundles' => [
                'yii\jui\JuiAsset' => [
                    'css' =>                    
                      ['themes/flick/jquery-ui.css'],
                ],
            ],
        ],
    ],
];

I tried the following to override this configuration item in the controller actions method:

public function actions() {  

      Yii::$app->components['assetManager'] = [
            'bundles' => [
                'yii\jui\JuiAsset' => [
                    'css' =>                    
                      ['themes/dot-luv/jquery-ui.css'],
                ],
            ],
        ];
      return parent::actions();
    }

Also I tried to set the value of Yii::$app->components['assetManager'] shown above to the view itself (it is partial view of form _form.php) and to the action that calls this view (updateAction). However, all this trying doesn't be succeeded to change the theme. Is there in Yii2 a method like that found in CakePHP such as Configure::write($key, $value);?


Solution

  • You should modify Yii::$app->assetManager->bundles (Yii::$app->assetManager is an object, not an array), e.g.

    Yii::$app->assetManager->bundles = [
        'yii\jui\JuiAsset' => [
            'css' => ['themes/dot-luv/jquery-ui.css'],
        ],
    ];
    

    Or if you want to keep other bundles config :

    Yii::$app->assetManager->bundles['yii\jui\JuiAsset'] = [
        'css' => ['themes/dot-luv/jquery-ui.css'],
    ];