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/JRCSalter May 12 '25

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

When I view the source, and I click on the link to the css file, it takes me to the 404 page.

I don't know if it's relevant, but I had a bit of trouble initially getting the router to even work. In the video series I mentioned, he could type 'localhost/' followed by anything, and it would still get back to the 'index.php' file. I resolved that by putting the following into my Apache VirtualHost file:

Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

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.

1

u/colshrapnel May 13 '25

It's highly unlikely. What do you see in the address when just opening your application? Is it http://localhost/ or something else?

is there indeed styles folder along with controllers, views and index.php?

are you still using that PHP routing for CSS?

1

u/JRCSalter May 14 '25

Problem solved. I moved the above lines from the VirtualHost definition to my .htaccess file.

Have no idea how that worked, as I thought they were effectively the same thing.