Arunendra
Arunendra

Reputation: 570

How to create global attribute in Yii2 application

I am using Yii2 and want to create a attribute and access this attribute in every where in my project for exp:

Yii::$app->name

Above is an example of yii2 default name. Is there any way to declare my custom attribute as following :

Yii::$app->supportname

Upvotes: 1

Views: 307

Answers (1)

rob006
rob006

Reputation: 22174

You can extend Application class, add required properties, and use it on bootstrap.

Create class for web:

class MyApplication extends \yii\web\Application {

    public $supportname;
}

And use it in index.php:

(new MyApplication($config))->run();

You need to do the same for \yii\console\Application and yii script.


But probably the best way is to use Application::$params and access value by Yii::$app->params['supportname'].

Upvotes: 3

Related Questions