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:
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?
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.
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.