Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable PHP to read .css and .js files while keeping their original Content-Type

I would like to configure apache php5 in a way that

  1. .css and .js files will be inspected by php
  2. their content type will remain as default (that is respectively "text/css" and "application/x-javascript")

I need this because both CSS and JS files are using global PHP vars. This allows me from a single PHP file to control both CSS and JS code dynamically (then the project is exported so that css and js files contain the actual values and not the PHP echos).

The way I'm doing right now is that I added .css and .js extension to apache php5.conf:

AddType application/x-httpd-php .php .phtml .php3 .css .js

I also use header('Content-Type: [...]') on top of each css/js file to change the Content-Type header value back to original. This isn't ideal as I have to manually add this line to every css/js file in order for the Content-type header to be appropriate, even if I do not use the PHP global vars.

So anyway I can make PHP inspect css/js files while keeping their original Content-Type without having to modify the actual css/js files themselves?

like image 229
Max Avatar asked Jun 23 '10 10:06

Max


2 Answers

I didn't try that, but from the docs it looks possible:

<Files *.css>
    php_value  default_mimetype "text/css"
</Files>
<Files *.js>
    php_value  default_mimetype "application/x-javascript"
</Files>
# ... and so on for other types
like image 104
unbeli Avatar answered Nov 13 '22 13:11

unbeli


If I absolutely had to do that, I would have made a special view for CSS and JavaScript files. For example, I would tell my application to handle all requests to, say, /pseudostatic/css/somefilename.js by a certain controller, which would take a template based on the name of a requested file, prepend all the needed global variables to it and serve it with a needed header.

And I would use caching based on the file's timestamp.

But I think this is realy an unnecessary pain. I bet you can find a way around, like keeping most of your CSS and Javascript in static files and loading only a tiny bit of server-side generated scripts and styles directly inside the requested page. This way you will save your server a lot of processing time.

like image 2
Igor Zinov'yev Avatar answered Nov 13 '22 13:11

Igor Zinov'yev