Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

php - Override Yii2 assetManager config in controller

I use yii-jui to add some UI elements in the views such as datePicker. In the frontendconfigmain-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' => [
                'yiijuiJuiAsset' => [
                    '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' => [
                'yiijuiJuiAsset' => [
                    '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);?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

Or if you want to keep other bundles config :

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...