Reputation: 57
I am using the yii2 and i want to load the js file from controller as in yii 1:
public function actionIndex()
{
Yii::app()->clientScript->registerScriptFile('scripts/myfile.js',CClientScript::POS_END);
$this->render('index');
}
But in yii2 i can't load the js file from controller. I can only load the js file directly in view file but not from the controller my code is to load the js file:
$this->registerJsFile(Yii::$app->request->BaseUrl . '/js/custom.js', ['depends' => [yii\web\JqueryAsset::className()]]);
But this is not working in the controller it works in the view file.
Is there any way to load the js file from the controller in Yii2?
Upvotes: 0
Views: 1349
Reputation: 621
As commented by Farbrizio, we not sure why you would use the controller to attach the script files but this should do what you are trying to achieve
\Yii::$app->getView()->registerJsFile(\Yii::$app->request->BaseUrl . '/js/custom.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
Upvotes: 1