yii2yii2-advanced-appyii-components

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
    }
}

Solution

  • 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