1

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

Sajid anwar
  • 1,194
  • 14
  • 41

2 Answers2

1

Simply add this in view file

<?php 
   $this->registerJsFile('PATH_TO_FILE');
?>
Yasar Arafath
  • 605
  • 1
  • 9
  • 30
  • Thanks of your time. But its wired that why my-custom.js not working while other working. Yeah i have know a little bit registerJsFile function. I think this is cache issue? – Sajid anwar Jan 12 '17 at 14:48
1

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

vijay nathji
  • 1,608
  • 13
  • 23
  • Thanks I so much appreciate your work. but I have a lot of try but no luck. I have know about the above help. Your help working fine but want to know why not working code. Does YII have cache issue. Thanks @vijay – Sajid anwar Jan 12 '17 at 14:35
  • yes it may be check this both links might be help you : http://stackoverflow.com/questions/24819220/yii2-assets-clear-cache http://stackoverflow.com/questions/37723515/why-by-every-refreshing-page-cache-reload-anew/37729758#37729758 – vijay nathji Jan 13 '17 at 09:30