如何在高级应用程序中的每个页面加载时加载类 [英] How to load class at every page load in advanced app

查看:24
本文介绍了如何在高级应用程序中的每个页面加载时加载类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个类,我将在每次有人加载页面时都会运行我的所有方法,我们称之为 InitRoutines.

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.

在 yii2 基本应用程序中,我会做这样的事情.我会将类添加到组件配置文件中,然后将其添加到引导程序中,就这么简单.

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.

但我不知道如何在高级应用程序中做到这一点,最好在 common/config 中

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';

在 InitRoutines 中,我有一个 init 类,它在页面加载时运行我需要的一切.

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 ?

推荐答案

我想我找到了解决方案,我不知道它是否正确,但它似乎有效.

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'
    ],
];

这篇关于如何在高级应用程序中的每个页面加载时加载类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆