Arunendra
Arunendra

Reputation: 570

Dynamically Change Yii2 configurations

I am trying to dynamically change App name and some components config Using component event: Please refer to the attached image, I have highlighted with red marker which part i want to set in my component.

enter image description here

My component code is following:

<?php
namespace common\components;

use Yii;
use common\models\AppPartner;


class Arun Extends \yii\base\Behavior{

    public function events()
    {
        return [
            \yii\web\Application::EVENT_BEFORE_REQUEST => 'getAppData',
        ];
    }

    public function getAppData(){
      // want to change stuff here
    }
}

Upvotes: 3

Views: 2097

Answers (1)

Patrick
Patrick

Reputation: 1338

You can access the application properties like this:

\Yii::$app->name = 'new name';

The app also gives you access to the components:

\Yii::$app->pinPayment->settings['mode'] = 'new mode';

See these parts of the documentation:

https://www.yiiframework.com/doc/guide/2.0/en/structure-application-components

https://www.yiiframework.com/doc/api/2.0/yii-base-application#$name-detail

Upvotes: 4

Related Questions