I'm trying to learn something about MVC, but I have problems including stylesheets correctly.
In my index.php
I do require_once('views/layout.php');
. Now I want to include my main.css
which is placed at css/main.css
, but I can't figure out how I could do that correctly, since the urls look like this: example.com/controller/action
and the browser tries to load my stylesheets from example.com/controller/css/main.css
Now I could include the stylesheets like this: <link href="../css/main.css" rel="stylesheet">
, but I don't think that this would be a good solution, since that wouldn't work with urls like example.com/controller/action/1
.
I'd like to know if there's a good way to solve this problem. Thanks in advance!
I'd have a global variable name:
$rootUrl = 'https://yourdomain.com';
So wherever I need include style/script file, I use absolute path:
<script src="<?php echo $rootUrl . '/css/main.css' ?>" /><script>