Sajid anwar
Sajid anwar

Reputation: 1194

js file path issue YII2

I want to include my custom js file which is in C:\xampp\htdocs\yii2\vendor\bower\backend\assets\js but console gives me error

Failed to load resource: the server responded with a status of 404 (Not Found)

While other same file have same above directory which is working.
In my appAsset file is under C:\xampp\htdocs\yii2\backend\assets

<?php

namespace backend\assets;

use yii\web\AssetBundle;

/**
 * Main backend application asset bundle.
 */
class AppAsset extends AssetBundle
{
    //public $basePath = '@webroot';
    //public $baseUrl = '@web';
    public $sourcePath = '@bower/backend/';
    public $css = [
            'assets/css/chosen.css',
            'assets/css/style.css',
            'assets/css/font-awesome.min.css',
            'assets/css/bootstrap.min.css',
            //'assets/css/bootstrap.css',
            'assets/css/jquery.dataTables.min.css',
            'assets/css/w3.css',
            'assets/css/jquery-ui.css', 
    ];
    public $js = [
        //'assets/js/jquery.min.js',
        'assets/js/jquery-ui.js',
        'assets/js/jquery.dataTables.min.js',
        'assets/js/jquery-ui.multidatespicker.js',
        'assets/js/chosen.jquery.js',
        'assets/js/chosen.jquery.js',
        'assets/js/my-custom.js',
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

and my-custom.js

<script>
    $(document).ready(function(){
       $('li.active .treeview').on('click', function(e) {

          $('li.active .treeview-menu').toggleClass("hide");
          e.preventDefault();
        });
    });
</script>

The url of the file which is not finding is this

http://localhost/yii2/backend/web/assets/c4875c89/assets/js/my-custom.js

enter image description here

Upvotes: 1

Views: 2127

Answers (2)

vijay nathji
vijay nathji

Reputation: 1638

Use RegisterJsFile concept:

You should simply register this js file in your view, e.g. :

$this->registerJsFile('@web/js/specific.js');

Or your custompath

 $this->registerJsFile('PATH_TO_FILELOCATION');

Read more :http://www.yiiframework.com/doc-2.0/guide-output-client-scripts.html#registering-scripts

and

http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJsFile()-detail

Upvotes: 1

Yasar Arafath
Yasar Arafath

Reputation: 625

Simply add this in view file

<?php 
   $this->registerJsFile('PATH_TO_FILE');
?>

Upvotes: 1

Related Questions