if you haven't used this before the link is: http://altorouter.com/
I am making a small application but do not require a framework, only the routing part. So I've decided to try altorouter out as it seemed quite simple.
I want to map certain routes to do certain things. for example:
www.example.com/products/
this should show my products.php template and pull data from the database to populate the fields. I have got this working with the following:
$router->map( 'GET', '/products', function() {
require('partials/connectdb.php'); //Require Database Connection
$pageContent = getProductsContent($conn); //prepare all the page content from database
require __DIR__ . '/products.php'; //require the template to use
});
The same for all of the other standard pages, my problem is when the route can change. For example:
www.example.com/shoes/
www.example.com/shorts/
www.example.com/shirts/
www.example.com/ties/
When a user goes to these routes I want to get the param 'shoes' and then while still using the products.php template, do the logic for only shoes.
So looking at the docs it says you can do:
www.example.com/[*] //which means anything.
However after adding this into my listed routes, it voids anything else the user trys to visit. So if they visit:
www.example.com/products // Like it worked before
It actually does the logic inside:
www.example.com/[*]
Does anyone know altorouter and can help me out please? I will paste my full page code below:
// Site Router
$router->map( 'GET', '/', function() {
require('partials/connectdb.php'); //Require Database Connection
$pageContent = getHomeContent($conn);
require __DIR__ . '/home.php';
});
$router->map( 'GET', '/products', function() {
require('partials/connectdb.php'); //Require Database Connection
$pageContent = getProductsContent($conn);
require __DIR__ . '/products.php';
});
$router->map( 'GET', '/[*]', function($id) {
require('partials/connectdb.php'); //Require Database Connection
$test = 'this was a test';
$pageContent = getProductsContent($conn);
require __DIR__ . '/products.php';
});
// match current request url
$match = $router->match();
// call closure or throw 404 status
if( $match && is_callable( $match['target'] ) ) {
call_user_func_array( $match['target'], $match['params'] );
} else {
// no route was matched
header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}