r/PHPhelp • • May 11 '25

Solved Can't load CSS using PHP router

So, I've followed this guide to build a PHP router: https://laracasts.com/series/php-for-beginners-2023-edition/episodes/15

My directory structure is the same as shown in that video, with a few minor tweaks. Source code for that video is here: https://github.com/laracasts/PHP-For-Beginners-Series/tree/4d46b0040bf03eb8198cf8c76be3d3704384e14d

However, when I insert a <link> in my HTML head, to get some CSS, it doesn't want to load it.

I have made a directory called 'styles' and added a file called 'main.css' in it with the following text:

body {
  border: 1px solid red;
}

I have made a test file in the same directory as the 'index.php' file called 'test.html', and placed the following line in the head:

<link rel="stylesheet" href="styles/main.css" />

That file works, and there is a definite red border around the body.

However, if I put that same line in the 'views/partials/head.php' file, the style is gone. I view source, and click on the link, and it can't find it.

I then decided to try to build the style into the router. I add '/styles/main.css' => controllers/styles.main.css', to the $routes array, and then add a controller file called 'controllers/styles.main.css' that does nothing more than require the css file. I load this up and the style isn't there. However, if I view source, and click on the link, I am taken to the css file, so the link is working, but it's just not loading the styles.

1 Upvotes

11 comments sorted by

View all comments

Show parent comment

1

u/MateusAzevedo May 12 '25

the dev tools said that the path was 'http://localhost/styles/main.css'

If styles folder is at the same level of index.php, and it indeed has main.css, then that should be working.

If you're absolute sure your files are where they need to be, then the only reason it can't work in this case is a webserver miss configuration, likely the redirect rules you added. Unfortunately, I'm not an Apache expert to help further with that. Maybe look for Apache logs, maybe you'll find something there.