Skip to content
Advertisement

Add 404 rule into PHP class router

I have a simple PHP router but I missed a rule if de file not exists. What I think is I must declare all files to check if the file exist?

But this raises another question how to check if the endpoint is empty or not exist and if this is empty show the 404 file also. Below the code what I have so far.

Router.php

JavaScript

index.php

JavaScript

Advertisement

Answer

This is a bit broad for a stackoverflow question but I think what you need to do is have your router register all the possible routes, and only actually try to run them at the end:

JavaScript

Note that in get() the route is only defined if the given file exists.

then when declaring your routes:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement