Link css and js using AppAsset.php

I’ve got my css and js files in web/css web/js folders respectively. I want to include them using AppAsset.php, which goes something like this:

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        // "css/bootstrap.min.css",
        "css/font-awesome.min.css",
        "css/prettyPhoto.css",
        "css/price-range.css",
        "css/animate.css",
        "css/main.css",
        "css/responsive.css"
    ];
    public $js = [
        // "js/jquery.js",
        // "js/bootstrap.min.js",
        "js/jquery.scrollUp.min.js",
        "js/price-range.js",
        "js/jquery.prettyPhoto.js",
        "js/main.js"
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

and in my layouts/main.php I register it like this:

<?php

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

use app\widgets\Alert;
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use app\assets\AppAsset;

AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>

My styles doesn’t appear. Can you point out my error?

1 Like

Do you have a complete HTML document in layouts/main.php or just what you show above?

Do you have a complete HTML document in layouts/main.php or just what you show above?

It’s the very top of the php file and, yes it’s a complete html file, just no begin/endBody() function calls yet. I’ll make an online php sandbox to show what exactly is happening inside those files.

Seems like it was something related to beginBody() endBody() missing methods.

1 Like

These are the methods Yii use to inject JS in your page.
Make sure you also call head() inside your <head> section.