1

I am relatively new to this whole MVC structure for web apps.

Here is the structure of my project.

enter image description here

The file you see on the right of the picture is inside the 'Views' folder.

When I want to link a css file, why does the path have to be href="/css/stylePortfolio.css"

Why is it not href="../../public/css/stylePortfolio.css"?

6
  • How did you bootstrap this? Commented Jul 1, 2016 at 9:52
  • @Chay22 what do you mean exactly? Commented Jul 1, 2016 at 9:53
  • Because your site root path is index.php folder, your public folder when you have CSS folder. Commented Jul 1, 2016 at 9:54
  • because the href attribute is a url, not a file path. Commented Jul 1, 2016 at 9:55
  • @miceli Can you explain in more detail? What is a site root path? How do you set it? And how does this affect the whole structure? Commented Jul 1, 2016 at 9:56

4 Answers 4

2

What you are looking at, is the HTML that is sent to the users' browser. The browser does not know anything about the structure of your application. It simply reads that href link, and downloads the file from http://example.com<link>, where <link> is /css/main.css for example.

When configured correctly, the web root of your website is in your /public folder. That means, anything that a browser requests, is relative to your web root. Thus, if you want to link to a css file, you need to think of that link relatively to your projects web root, not relatively to your project root.

An example: Say, you create a new project in /home/user/AwesomePhpProject. Now, /home/user/AwesomePhpProject is called your project root.

In your project root, you create a directory, public. You configure that directory to be your web root, using VirtualHost (when using Apache) or the root directive (when using Nginx). /home/user/AwesomePhpProject/public is now your web root. When a browser requests /css/main.css, it will be directed to /css/main.css relative to your web root. In our case, that will be /home/user/AwesomePhpProject/public/css/main.css.

Most modern applications separate the project and web root, for security reasons.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. I'm starting to understand. Could you elaborate a bit? ex: What is a web root? What is a project root? How are they separated?
2

As your index.php is inside the public folder, so all the views are loading in the public folder. That is why you should declare the CSS path from the public root. You can modify the path if necessary.

In this case, you can declare a global variable or constant your main controller with the path of your CSS folder

define('CSS_PATH', 'http://localhost/fab/public/css/');

Now use this like

<link rel="stylesheet" href="<?=CSS_PATH?>bootstrap.css">

8 Comments

when I do this I get the error Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://fab.app/fab.app/public/css/bootstrap.min.css".
Add this to your .htaccess and let me know AddType text/css .css
I did it. No difference. The problem is that the path is wrong. It's http://fab.app/fab.app/public/css/bootstrap.min.css when it should be "http://fab.app/css/bootstrap.min.css
Is it solved? If not then could you show me the line you defined in PHP?
You are right. I had not included the http:// part of the line in php. Now the error is what you tried to solve before: Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://fab.app/public/css/bootstrap.min.css" So the path is correct but it doesn't interpret it as css.
|
0

It's because front-end links (like CSS and JS) are relative from current file you are in. And since your root is in public you have to specify URL from this directory (you can't link to any file in top level of root).

Comments

0

Why is it not href="../../public/css/stylePortfolio.css"?

Because entry point of your MVC is index.php from public folder. So all your css and js links should be relative to public folder

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.