Stefan Hansch
Stefan Hansch

Reputation: 1590

Yii2 view layout doesn't see any js, css files with AppAsset bundle

Yii2 advanced template, PHP 5.6, Ubuntu 16.04, nginx.

I have following myproject/backend/assets/AppAsset.php:

class AppAsset extends AssetBundle {
    public $basePath = '@webroot';

    public $css = [
        '/css/style.css', 
        '/plugins/iCheck/square/blue.css', 
        '/plugins/datatables/dataTables.bootstrap.css', 
        '/css/AdminLTE.min.css'
    ];

    public $js = [
        'admin/js/common.js',
        'plugins/datatables/jquery.dataTables.js',
        'plugins/datatables/dataTables.bootstrap.js',
        'plugins/iCheck/icheck.min.js' 
    ];

    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset'
    ];
}

Layout has:

use backend\assets\AppAsset;
AppAsset::register($this);

It is my layout main.php, when I have added AppAsset bundle.

<?php
use backend\assets\AppAsset;
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;

AppAsset::register($this);

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

?>
<?php //$this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= Html::csrfMetaTags() ?>
    <?php
    //$assets_base = Yii::$app->controller->getAssetsBase();
    ?>

    <title>MyProject</title>
    <?php $this->head() ?>
</head>
<body class="login-page">
<?php $this->beginBody() ?>
<h1>Test!!!</h1>
<?= $content ?>

<?php $this->endBody() ?>
</body>
</html>
<?php

All css and js files are located in public folder web (myproject/backend/web).

When I reload page tag <head> has only:

<meta charset="UTF-8">
    <meta name="viewport" 
  <meta name="csrf-param" 
<meta name="csrf-token"

Upvotes: 0

Views: 710

Answers (1)

rob006
rob006

Reputation: 22174

You should remove comment from this line:

<?php //$this->beginPage() ?>

$this->beginPage() is required to enable output buffering, which is required to inject assets links in the place of $this->head() call.

In general you should not remove any method calls from default layout of app templates, unless you're really know what you're doing - they are there for a reason.

Upvotes: 3

Related Questions