How to load class at every page load in advanced app

≯℡__Kan透↙ 提交于 2019-12-12 05:48:01

问题


I want to have class in which I would put all my methods that should run everytime someone loads a page, lets call it InitRoutines.

In yii2 basic app I would do something like this. I would add class to compontents config file, and add it to bootstrap, simple as that.

But I can not figure out how to do it in advanced app, preferable in common/config

config.php

$config = [
    // ..
    'components' => [
        'InitRoutines' => [
            'class' => 'app\commands\InitRoutines',
        ],
    ],
];

$config['bootstrap'][] = 'InitRoutines';

And in InitRoutines I have init class which run everything I need at page load.

InitRoutines.php

namespace app\commands;

use Yii;
use yii\base\Component;
use app\commands\AppHelper;
use app\commands\Access;

class InitRoutines extends Component
{
    public function init()
    {
        parent::init();
        Access::checkForMaintenance();
        Yii::$app->language = AppHelper::getUserLanguageCode();
    }
}

How can I accomplish same in advanced app ?


回答1:


I think I found the solution, I dont know if it is correct way but it seem to be working.

config.php

return [
    // ...
    'components' => [
        'InitRoutines' => [
            'class' => 'common\commands\InitRoutines',
        ],
    ],
    'bootstrap' => [
        'log',
        'common\commands\InitRoutines'
    ],
];


来源:https://stackoverflow.com/questions/42498431/how-to-load-class-at-every-page-load-in-advanced-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!