0

I am writing my own MVC implementation and I am facing a problem including local CSS files. Basically all requests in my applications are going to a method which is decomposing them in order to get a controller and action. For example:

localhost:8888/items/list this request will be redirected to my index.php where I have:

Application::start($_SERVER['REQUEST_URI']);

Then the request is decomposed to get the controller 'items' and action 'list' So far all good and I didn't realise the problem as I was using bootstrap from CDN. However now if I include in my html something like:

   <link rel="stylesheet" type="text/css" href="/css/style.css">

This will go to my method again and it will not work. What I tried is to check if the URI contains 'css' and if it does just include the file with:

if(preg_match('/css/',$uri)){
include "../webroot/$uri";}

This worked in terms of loading the css but the css is ignored. Also I get

"Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8888/css/style.css"."

The problem is even bigger if I try to include images although with images at least I am able to display them but again through 'include'. Any ideas how to fix this ?

P.S I know that the problem is most likely because the css is loaded as text file, however I don't know how to get this working with my current setup

5
  • Did you try localhost:8080/project_name/css/style.css ? Commented Jul 15, 2017 at 15:28
  • There is no problem with loading the css itself, the problem is it is ignored completely for some reason. Commented Jul 15, 2017 at 15:30
  • Sure, most likely your script sends the wrong header. To php this is only text. You need to send specific headers indicating that the response actually is css rules. Commented Jul 15, 2017 at 15:36
  • Instead of include()ing the stylesheet you might want to edit the .htaccess file (apache) to allow direct access to the style? Or images? Or configure the server in general Commented Jul 15, 2017 at 15:43
  • Including the header fixed the problem Commented Jul 15, 2017 at 15:44

1 Answer 1

1

Try header("Content-type: text/css; charset: UTF-8"); with file_get_contents instead of include like this

if(preg_match('/css/',$uri)){
  header("Content-type: text/css; charset: UTF-8");
  if(!$file = file_get_contents("http://localhost:8080/project_name/webroot/$uri")) return 0;
  echo $file;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.