I’m working on a php/codeigniter project and I’m thinking about creating a controller specifically to handle returning customized css & javascript files.
In previous projects I’ve included external CSS & JS files in the headers of my view files but they’ve essentially had to be static since they were just sent as regular assets by the server. For my next project I’m thinking about using a controller and essentially making my CSS/Javascript files ‘views’ that are loaded by the controller.
What do you guys think? Is this a good approach? Is there a generally accepted better way of doing this?
By using a controller for your js/css, you have the overhead of loading a ton of unnecessary stuff for every request for the file. The security class, the input class, the router, exceptions, etc.
You don’t need any of this for what should be static content. You don’t want the files to be dynamic either (changing per request) because this will affect the browser cache. Either the file will get cached in the state it was loaded, or you will lose the benefit of the browser cache.
Your best bet is to combine the files, minimize them, and write the content to a static file, but then they aren’t dynamic. However, they probably shouldn’t be.
Another thing you can do is to use a php file to combine the static files and reference it in your
<link>tag (screen.php). Simple CSS example:Either way, you should update the file name, perhaps with a query string, in order to avoid the old cached file being served. Example:
/screen.css?version=2I do use
$this->load->view()sometimes for dynamic js, but only for inline script tags (dynamic WYSIWYG configuration for example). This allows you to pass variables to the “view” file as well as avoid cache issues.My point of view is that it’s not worth firing up CI and all the related dependencies just to serve what should be static content.