I recently started using Yii 2 , and I am having problems with the layout file to get the following error:
Call to undefined method Yii::app()
This is my layout file:
<?php use yii\helpers\Html; ?> <?php $this->beginPage() ?> <!DOCTYPE html> <html lang="<?=Yii::$app->language?>"> <head> <title><?=Html::encode($this->title)?></title> <meta charset="<?=Yii::$app->charset?>"/> <meta name="viewport" content="width=device-width, initial-scale=1"> <?=Html::csrfMetaTags()?> <link href="<?=Yii::app()->request->baseUrl;?>/css/bootstrap.min.css" rel="stylesheet" media="screen"> <link href="<?=Yii::app()->request->baseUrl;?>/css/custom.css" rel="stylesheet" media="screen"> <script type="text/javascript" src="<?=Yii::app()->request->baseUrl;?>/js/bootstrap.min.js"></script> <script type="text/javascript" src="<?=Yii::app()->request->baseUrl;?>/js/scripts.js"></script> <?php $this->head() ?> </head> <body> <?php $this->beginBody() ?> <?=$content?> <?php $this->endBody() ?> </body> </html> <?php $this->endPage() ?>
When I use Yii::$app , I get no problems, but if I use Yii::app() , I get this error.
I started using Yii::app() in some places when I read, and I was told that you should use the following to include absolute path names in the views:
Yii::app()->request->baseUrl
... and enable jQuery usage:
Yii::app()->clientScript->registerCoreScript("jquery");
However, when I do something with app() , I get the above error.
I tried replacing app() with $app ; the page was loaded fine, but in Yii::$app->request->baseUrl there was an empty value.
What am I doing wrong here?
php yii yii2
Bret
source share