including Javascript files


I’m creating a form to dynamically create multiple models of the same type. To do this, I will need a sizable amount of javascript. So I plan on putting my javascript in an external file.

I did a test to make sure that my external javascript file was getting loaded correctly. It didn’t work. This is my test code in my form view:

$cs = Yii::app()->getClientScript();


$cs->registerScriptFile(Yii::getPathOfAlias('application.components.javascript').'/multiloadSchedulingInterface.js', CClientScript::POS_HEAD);

The code in multiloadSchedulingInterface.js (for testing purposes) is simply:

alert('hello world');

I tested this as well:

$cs = Yii::app()->getClientScript();


$js="alert('hello world');";

$cs->registerScript('miniTest', $js, CClientScript::POS_HEAD);

That works as it should. I echoed out the path of Yii::getPathOfAlias(‘application.components.javascript’) and it was fine. I verified it in terminal and all is well. I also echoed out Yii::app()->basePath.’/components/javascript’ since I saw a lot of posts using that instead and it resulted in the same path.

I just don’t understand why the registerScriptFile() is not working. Any Ideas?




$cs = Yii::app()->getClientScript();




$cs->registerScriptFile($myScript, CClientScript::POS_HEAD);


to register a script file it should be located in an accessible via http directory (I think that application.components.javascript is not)

  1. try to relocate your js file


  1. leave it in the protected application.components.javascript folder but first publish it using AssetsManager:

$jsFile = Yii::app()->getAssetManager()->publish($file);


Thanks. That worked but what does that do exactly? I read the documentation on it and it said that it copies the file to a web readable directory and then returns the path to it. I can’t find any changes to my apache website filesystem. Does it use temp space?

Why does this have to be done if my code is already in a web readable directory?

Will I have to do this every time I use external javascript?

How come I haven’t seen this in documentation if it needs to be used each time?

I didn’t have to do this with a css file that I tested.



Ah, duh. Apache doesn’t have access to the protected directory and since its being referenced by Apache and not compiled by php first it can’t see it.

Didn’t think of that! Thanks!


The /protected folder IS NOT available to be accessed via URL, you’ll get a deny request from apache if you try to do so.

When your scripts are not in a public place(ie, like in your case, they are in the /protected folder) you need to "publish" them so that these scripts are accessible.

You do that using the publish() method of the assetManager component. Basically, this method takes a file or a folder and copies them in the /assets folder that is web accessible, then it returns the url to that file so that you can use it with the clientScript component and register it as you wish.

Just remember, you need to publish your assets ONLY if these resides in the /protected folder of your app, otherwise, just register them as you usually do and it will work.

Makes sense ?

nice explanation…