yii2 registering JS files to a View

David picture David · Dec 26, 2014 · Viewed 77.9k times · Source

I have A.php view file in /views/A/ folder. And I have A.js js file in /views/A/ folder Please help me register js file in view file.

As I understand I must write $this->registerJsFile('path/to/file.js'); 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 /views/A/ ?

Answer

xav picture xav · Feb 26, 2015

This is not elegant, but working if you need to have your js file registered after jquery (as seen in the Yii2 doc)

<?php $this->registerJsFile(Yii::$app->request->baseUrl.'/js/youFile.js',['depends' => [\yii\web\JqueryAsset::className()]]); ?>