0
votes

I am using yii2 advanced template.

I can't access Yii::$app->params from my user model. I get a null value in return .

Here is my main.php in frontend/config ( i use advanced template).

<?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'),
    require(__DIR__ . '/functions.php')
);

return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontend\controllers',
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        /*
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],
        */
    ],
    'params' => $params,
];
1
Have you tried to print out the params object? var_dump(Yii::$app->params);h2ooooooo

1 Answers

1
votes

Params are set in config/params.php/config/params-local.php, and accessible through Yii::$app->params['key'].

I not sure how your 'functions.php' looks, but this works out of the box with 'adminEmail'. Params are values stored in an array, and your functions.php should not haves functions or any other logic. Try removing your functions.php from the array_merge, and try with this:

Eksample (already defined in Yii2 Advanced app)

<?= Yii::$app->params['adminEmail'] ?>