How to get values ​​from params.php in yii2

I am using yii2 for my application. In the params.php file, I defined an array like

return ['setValue'=>100]; 

and I included params.php in web.php

 <?php $params = require(__DIR__ . '/params.php'); $config = [ 'params' => $params, 

]; return $ config;

and I use another header.php file in the views folder. So how to get params array in header.php? I used like \ Yii :: $ app-> params; this does not work. can anyone help me

+7
php yii2
source share
2 answers

Make sure you have the correct config / main.php (this fi sample for the backend application that I used)

  <?php $params = array_merge( require(__DIR__ . '/../../common/config/params.php'), require(__DIR__ . '/../../common/config/params-local.php'), require(__DIR__ . '/params.php'), require(__DIR__ . '/params-local.php') ); return [ 'id' => 'your-app-backend', 'name' => 'Your APP Backend', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'controllerNamespace' => 'backend\controllers', 'modules' => [], 'components' => [ 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ], ], ], 'errorHandler' => [ 'errorAction' => 'site/error', ], ], 'params' => $params, ]; 

Assuming you have param.php with

 <?php return [ 'adminEmail' => ' my_mail@example.com ', ]; 

you can get the parameter using yii :: $ app-> params

  Yii::$app->params['adminEmail'])) 

for printing use

  echo Yii::$app->params['adminEmail'])) 
+12
source share

You can access a single value with:

 $value = Yii::$app->params['nameParameter']; 

But, if you want to get an array

  $ values ​​= Yii :: $ app-> params; 

You must have access to all the properties defined in your configuration file that are integrated for use as attributes of "Yii :: $ app". In this case, in this case, the params attribute .:

According to the documentation http://www.yiiframework.com/doc-2.0/guide-structure-applications.html

+2
source share

All Articles