Welcome Guest, Not a member yet? Register   Sign In
Virtual path handling
#1

[eluser]GrahamDj28[/eluser]
When working on big projects it can be a pain to remember where all your assets are located.
You need to remember the path to each file. A lot of my projects are also placed on shared hostings that do not always provide the modules needed to set headers on various file types.
To solve this the headers are set with PHP, but doing so in a CodeIgniter class would mean that each call to a file would need to be routed again through Codeigniter. Thus loading files that are not needed at that point.

I have made a small library that is placed in the webroot and all calls to assets are routed to it via the .htaccess file
Code:
RewriteCond %{REQUEST_URI} ^/files.*$ [NC]
RewriteRule ^(.*)$ /webfiles/handle.php/$1?handle=1 [QSA,L]

The library handles the incoming call, locates the file, sets the headers by mime type and outputs it to the browser.
My assets folder is divided up into 4 subfolders.
- cache // Used for cached images
- cms // Folder for all CMS related asset files, contains subfolders and subsub ect...
- shared // Folder for all shared asset files, contains subfolders and subsub ect...
- website // Folder for all website related asset files, contains subfolders and subsub ect...

My URLs are virtual in the generated HTML and look like this:
Code:
http://www.my-domain.com/files/website/requested-css.css
http://www.my-domain.com/files/shared/requested-js.js
http://www.my-domain.com/files/cache/an-image.jpg

No more than 3 URI segments needed!
The first is used for the routing
The second is used for location
The third is the filename

Need a download link?
Just add download as the third segment and place the filename as the fourth segment
Code:
http://www.my-domain.com/files/website/download/an-image.jpg

Does your project require a lot of CSS and JS files?
Why not load all your CSS and JS with 1 single call?
Code:
http://www.my-domain.com/files/css=website/default.css,shared/bootstrap.css
http://www.my-domain.com/files/js=website/default.js,shared/bootstrap.js
Use /css= or /js= + a comma seperated list of required files as location/filename


This setup has been really helpful for me. I now only need to know what the location is and the name of the file. The rest is done for me.

Any tips or improvements are most welcome. The library is attached as a zip file.
#2

[eluser]ivantcholakov[/eluser]
Quote:Any tips or improvements are most welcome. The library is attached as a zip file.

Better publish your work at GutHub and add license information.
#3

[eluser]GrahamDj28[/eluser]
[quote author="ivantcholakov" date="1397664095"]
Quote:Any tips or improvements are most welcome. The library is attached as a zip file.

Better publish your work at GutHub and add license information.[/quote]

So the code is good?
#4

[eluser]ivantcholakov[/eluser]
The idea is important. Code always may be improved and somebody may send to you a pull request, or open an issue. You have already documented your idea. I took a look at your code, it looks normal.




Theme © iAndrew 2016 - Forum software by © MyBB