A simple and easy to use, page.js based + php integration AJAX page router.
Clone the repo into your brand new website.
git clone https://github.com/Coasters-Crafters/php-router.git
Then, depending on your webserver, follow this guide.
This router works like the Next.js router, only PHP.
In router.php on line 2, there's a require_once, update this with a php file you want included on all pages. This is useful for specifying classes or a database connection.
All routes look inside of the pages/files directory with the path of the url.
e.g: I want to navigate to https://example.com/help. To do this, I would make a file in pages/files called help.php.
Since this doesn't have any parameters in it, I don't need to edit the router settings.
Now, lets say I want to lookup a user. I can do this by naming my php file something special.
e.g: I want to view a user called Joe with the user ID of 1, the URL is https://example.com/users/1. To do this, I would make a file called users[id].php. Then, navigate to the router.js file and add to the customRoutes list. This route's name would be /users/:id. Then, in that PHP file, I can specify the following to get the params.
<?php echo $_PARAMS['id']?>This also works for multiple params by naming the file users[param1][param2].php. The route that goes in router.js would look like /:param1/:param2.php
Extending this past the params involves turning the php file into a folder (so the folder name would be users[id]) and putting an index.php inside the folder, then you can append to your customRoutes in router.js and add more routes under the users[id] folder.
There are examples of all ways of routing in the boilerplate.
Use the path specified in the router.js file to be able to post, here's a fetch example.
let resp = (await fetch("/pages/router.php?path=/your/path/in/router.js", {
method: "POST",
body: JSON.stringify({
your: "body"
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
}));Edit the 404.html page in /pages/ to customize what your 404 page should look like.
The easiest way to quickly start this up is with the PHP Development Server:
php -S localhost:8080