Yii class for direct file open

Hi there,

Does anyone knows about Yii class that would be a wrapper for basic PHP file open routines, like fopen? I know CFileHelper, but it hasn’t got any function for opening a file directly stored on server.

Why would I need that, someone might ask? Well, I’m using: Yii::app()->clientScript->registerScriptFile() after Yii::app()->assetManager->publish() to include JavaScript file and be able to use it within a page.

But what about situation, when I have to actually change something inside JS file? For example: insert actual path to image files, it uses. If I would use it on static webpage, I would do one time edit of JS file and problem would be gone. But I’m using it in dynamic environment of Yii and I have no idea where file will be put by Yii after Yii::app()->assetManager->publish().

Well… I can publish files to assets folder from source but change permanently JS file by entering there paths pointing to source folder from where assetsManager publishes them. This might sound as some kind of idea. But what if files will be put on another server in another path/directory? Remember each time that I have to manually update paths in JS file?

I’m thinking about publishing all files that are needed via Yii::app()->assetManager->publish() except JS file I need to modify. Then open that file directly from source folder to a string, modify what I need (i.e. paths) and put to website with Yii::app()->clientScript->registerScript this time.

If anyone knows a better way to achieve it, I will apperciate any advise.

Here you go man, get this extension for your purposes:

CFile

Also, about your problem… why don’t you have inside that JS file the paths to be GLOBAL variables to the document. And then, you can:

  1. have the main layout to write your global variables (in this case paths)

  2. OR write a <script bla bla bla, that points to a controller action that actually renders /text-javascript mime-type, global variable contents, and this is the first script file in your website.

I think that is better than actually modifying javascript files.

What do you think?

best

Brilliant! :) Can’t figure out why I didn’t think about so obvious solution? :confused:

BTW: CFile extension looks very interesting.

Happy to help… congrats…

When we can’t find a solution to our problems we should do as the wise man does: back to basics.

We tend to assume and forget very obvious things in our overloaded head.