PHP致命错误 - yii \ base \ ErrorException在YII2中调用未定义的方法Yii :: app()

时间:2018-01-18 11:43:23

标签: php yii yii2

public function actionIndex()
    {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        if (Yii::app ()->user->isGuest) {
            $this->redirect ( Yii::app ()->createUrl ( 'site/login' ) );
        } else {

        // Export Scenario
        $exportScenario = 'incoming';
        $exportScenario2 = 'outgoing';
        $exportScenario3 = 'missed';

        // Default date
        $date = null;
        $fromDate = null;
        $toDate = null;

        //Model definitions
        $model = new Logs (); 

        //get incoming calls
        $incalls = $model->getInCalls();

        // For filtering grid
        $filtersForm = new GridForm ();
        // Get filtered data
        $filteredData = $filtersForm->filter ( $incalls );

        $inDataProvider = new yii\data\ArrayDataProvider ( $filteredData, [ // Define pagination
                    'pagination' => [
                            'pageSize' => 10000 
                    ] 
            ] );






        //get outgoing calls
        $outcalls = $model->getOutCalls();
        // For filtering grid
        $filtersForm = new GridForm ();
        // Get filtered data
        $filteredData = $filtersForm->filter ( $outcalls );

        $outDataProvider = new CArrayDataProvider ( $filteredData, [ // Define pagination
                    'pagination' => [
                            'pageSize' => 10000 
                    ]
            ] );


        //get missed calls
        $missedcalls = $model->getMissedCalls();
        // For filtering grid
        $filtersForm = new GridForm ();
        // Get filtered data
        $filteredData = $filtersForm->filter ( $missedcalls );

        $missedDataProvider = new CArrayDataProvider ( $filteredData, [ // Define pagination
                    'pagination' => [
                            'pageSize' => 10000 
                    ] 
            ] );

        // For Excel exporting and getting printable versions

        if (isset ( $_GET ['export'] )) {
            $exportHash = $_GET ['id'];
            if ($_GET ['export'] == 'excel') {
                // Exporting excel
                Common::export ( null, 'get', $exportHash );
            } else {
                // Getting printable html
                Common::export ( null, 'get', $exportHash, 'html' );
            }
        }

        $this->render('index', [
            'exportScenario' => $exportScenario,
            'exportScenario2' => $exportScenario2,
            'exportScenario3' => $exportScenario3,
            'inDataProvider'=>$inDataProvider,
            'outDataProvider'=>$outDataProvider,
            'missedDataProvider'=>$missedDataProvider,
            'filtersForm' => $filtersForm,
            'date'=>$date,
            'model'=>$model,
            'incalls'=>$incalls

            ]);
    }
}

我运行此网站http://localhost/master/web/index.php?r=site/index,但错误调用未定义的方法Yii :: app()会显示。我正在使用yii2基本应用程序。任何帮助将不胜感激。

上面的代码是sitecontroller.php的摘录,错误来自这一行

  

if(Yii :: app() - > user-> isGuest){                   $ this-> redirect(Yii :: app() - > createUrl('site / login'));               }

1 个答案:

答案 0 :(得分:0)

在Yii2中

没有Yii:app()Yii::$app 所以你应该使用

if (Yii::$app->User->isGuest) {
        $this->redirect (Yii::$app->createUrl ( 'site/login' ) );
    } else {
    .....