Getting Call to undefined method Yii::app() within layout view

I’ve recently started using Yii 2 and I’m having some issues with the layout file with getting the below error:




Call to undefined method Yii::app()



This is my layout file:


<?php


use yii\helpers\Html;


/* @var $this yii\web\View */

/* @var $content string */


?>


<?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>

	

	<!--[if lte IE 8]>

		<script src="<?=Yii::app()->request->baseUrl;?>/js/html5shiv.min.js"></script>

		<script src="<?=Yii::app()->request->baseUrl;?>/js/respond.min.js"></script>

	<![endif]-->

	

	<?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 issues, but if I use Yii::app() then I get that error.

I started using Yii::app() in some places as I was reading around and was told you should use the below to make sure to include absolute path names within views:


Yii::app()->request->baseUrl

…and to include jQuery use:


Yii::app()->clientScript->registerCoreScript("jquery");

However when I do anything with app() I get the above error.

I tried replacing app() with $app and the page loaded fine but there was a blank value in Yii::$app->request->baseUrl.

What am I doing wrong here!?

Thanks!

In Yii 2, $app is a property of Yii, not a method, so you should use Yii::$app->blah.

Alright thanks. Any idea why:


Yii::$app->request->baseUrl

would be blank then?

Also, I just added the below to my layout file:


<?php Yii::$app->clientScript->registerCoreScript("jquery"); ?>

…and now am getting this error:


Getting unknown property: yii\web\Application::clientScript

Yii2 is not backwards compatible with Yii 1.x.

You need something like $this->registerJsFile(‘http://example.com/js/main.js’, [‘depends’ => [JqueryAsset::className()]]);

See http://www.yiiframework.com/doc-2.0/guide-output-client-scripts.html

I thought it was different for core scripts? … which seems to still be the case as I am still getting two copies of jQuery being included after I did:


<?php $this->registerJsFile($base_url . '/js/jquery.min.js', array('position' => $this::POS_HEAD), 'jquery'); ?>

Additionally, how do you use the “depends on” when it doesn’t depend on an “asset bundle”… like I ant to do this:


	<?php $this->registerJsFile($base_url . '/js/jquery.min.js', array('position' => $this::POS_HEAD), 'jquery'); ?>

	<?php $this->registerJsFile($base_url . '/js/bootstrap.min.js', array('depends' => array('jquery'), 'position' => $this::POS_HEAD), 'bootstrap'); ?>

	<?php $this->registerJsFile($base_url . '/js/scripts.js', array('depends' => array('jquery', 'bootstrap'), 'position' => $this::POS_HEAD), 'scripts'); ?>

But it doesn’t work as “jquery/bootstrap” aren’t classes. Lastly, how do you include JS/CSS files within conditional statements with this?

If you have made the advanced or basic app, jquery & bootstrap are already included, just go ahead and use them

Not quite sure what you mean. But jquery is only included by default when needed and placed in the "assets" folder and because of this is causing conflict issues on the pages where it is auto-loaded.

On top of that, I would prefer to maintain my own version of these frameworks.

Well I ended up using the below to disable Yii from including any jQuery or Bootstrap files.

Inside config/web.php within the "components" section add:




		'assetManager' => [

			'bundles' => [

				'yii\web\JqueryAsset' => [

					'js'=>[]

				],

				'yii\bootstrap\BootstrapPluginAsset' => [

					'js'=>[]

				],

				'yii\bootstrap\BootstrapAsset' => [

					'css' => [],

				],

			],

		]