How to dynamically control paths in static CSS files with MVC code igniter project
I'm creating a website/codeigniter-project that uses views which link to external CSS files.
Everywhere throughout my project/web-page's views I can control the URL pa开发者_JAVA技巧ths of images, links, etc by constructing them from the 'base_url' setting variable. I don't have any control over static, linked external CSS files. This means that whenever my base URL path changes for my site I have to go through my CSS files and do global search/replaces to update all my paths.
To solve this I thought about creating a controller just to load CSS/JavaScript files and treating the actual files like views with hooks but I was talked out of this by @WesleyMurch in this question:
Using a controller to handle returning customized css & javascript files with codeigniter
How can I dynamically assign base-paths to my css assets so I don't have to do global search and replaces every time I update the base path of my site?
For this I use a view file containing css code with all the variables. The only change is that you should set appropriate headers for CSS
Create function style
in your controller and set it to render appropriate view file (style.php
). Code all your css with php code in style.php
.
Set following headers at the start of the function:
header("ContentType: text/css");
header("Expires: <some far future expiration time or use mod_expires with apache>");
精彩评论