David David - 4 months ago 200
Javascript Question

yii2 registering JS files to a View

I have A.php view file in

And I have A.js js file in
Please help me register js file in view file.

As I understand I must write
in view file.

But (Question A) I get
method registerJsFile is not found in a class
message from PHPStorm.

Also (Question B) what should I write in path considering both files are in the same folder


is there any specific reason to include the file manually rather than creating an asset bundle?

In any case if you've read the documentation regarding assets, you would have noticed that there's a clear distinction about source, published and external assets.

The most important part of it being that source and published assets use different options to determine whether and how a file should be published.

In your case you've got a source asset which needs to be copied over to the assets directory.

The invocation of registerJsFile as hinted in the documentation, will expect a published asset.

Here you have specifically two options, which probably the first is more quick and coherent:

  1. move the asset in the web/ folder, as in web/js/ or whatever you prefer and keep using registerJsFile()
  2. add a new asset bundle for source assets, specifying the various options as detailed in the above linked page.

Hope this clears things out.