Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 每个控制器之前的Yii框架操作_Php_Yii_Controller - Fatal编程技术网

Php 每个控制器之前的Yii框架操作

Php 每个控制器之前的Yii框架操作,php,yii,controller,Php,Yii,Controller,是否有任何“主”控制器在“控制器”文件夹中的其他控制器之前触发?我有一个项目,每个用户都有不同的站点语言,因此我想先检查设置,然后使用以下方法设置语言: Yii::$app->language='en-EN'; 现在我在我所有的控制器上都这样做,但我想这应该是一个更容易的选择。我不久前也遇到过同样的问题,并通过添加另一个组件找到了解决方案 将配置中的类添加到components部件,并通过添加到引导程序在开始时加载它 config.php $config = [ // ..

是否有任何“主”控制器在“控制器”文件夹中的其他控制器之前触发?我有一个项目,每个用户都有不同的站点语言,因此我想先检查设置,然后使用以下方法设置语言:

Yii::$app->language='en-EN';

现在我在我所有的控制器上都这样做,但我想这应该是一个更容易的选择。

我不久前也遇到过同样的问题,并通过添加另一个组件找到了解决方案

将配置中的类添加到components部件,并通过添加到引导程序在开始时加载它

config.php

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

$config['bootstrap'][] = 'InitRoutines';
namespace app\commands;

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

class InitRoutines extends Component
{
    // this method runs at start at every page load
    public function init()
    {
        parent::init();
        Access::checkForMaintenance(); // my custom method
        Yii::$app->language = AppHelper::getUserLanguageCode(); // my custom method
    }
}
然后让类使用
init()
方法扩展
Component

InitRoutines.php

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

$config['bootstrap'][] = 'InitRoutines';
namespace app\commands;

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

class InitRoutines extends Component
{
    // this method runs at start at every page load
    public function init()
    {
        parent::init();
        Access::checkForMaintenance(); // my custom method
        Yii::$app->language = AppHelper::getUserLanguageCode(); // my custom method
    }
}

不久前我也遇到了同样的问题,并通过添加另一个组件找到了解决方案

将配置中的类添加到components部件,并通过添加到引导程序在开始时加载它

config.php

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

$config['bootstrap'][] = 'InitRoutines';
namespace app\commands;

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

class InitRoutines extends Component
{
    // this method runs at start at every page load
    public function init()
    {
        parent::init();
        Access::checkForMaintenance(); // my custom method
        Yii::$app->language = AppHelper::getUserLanguageCode(); // my custom method
    }
}
然后让类使用
init()
方法扩展
Component

InitRoutines.php

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

$config['bootstrap'][] = 'InitRoutines';
namespace app\commands;

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

class InitRoutines extends Component
{
    // this method runs at start at every page load
    public function init()
    {
        parent::init();
        Access::checkForMaintenance(); // my custom method
        Yii::$app->language = AppHelper::getUserLanguageCode(); // my custom method
    }
}

您可以附加到应用程序事件或在此处执行您的操作。对我来说似乎更容易,例如:

$config = [
    // ..
    'on beforeRequest' => function($event) {
        Yii::$app->language='en-EN';           
    }
];

您可以附加到应用程序事件或在此处执行您的操作。对我来说似乎更容易,例如:

$config = [
    // ..
    'on beforeRequest' => function($event) {
        Yii::$app->language='en-EN';           
    }
];

你有没有检查配置文件
config/main.php
?是的,我恐怕找不到那里的任何模型。我在尝试使用Yii时出现“尝试获取非对象的属性”错误::$app->user->identity->user\u lang;你有没有检查配置文件
config/main.php
?是的,我恐怕找不到那里的任何模型。我在尝试使用Yii时出现“尝试获取非对象的属性”错误::$app->user->identity->user\u lang;太好了!谢谢,太好了!谢谢