问题
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