Easy PHP Routing with FlightPHP
Never worry about setting routes manually again
When you are learning PHP programming, your URL’s should look something like this:
http://yourdomain.com/showcustomerdetail.php?id=73
and it is entirely ok to keep it like that, but as you advance in your projects and you start wanting to have more control over what you are building, you might want to clean up your URL to something cleaner and readable to something like this:
http://yourdomain.com/customer/detail/73
What is Routing
Routing is a technique that adds separation between files and URLs. And because of that, you’ll have to implement some code to manage that separation. And since URL’s options can (and probably will) grow a lot, it is a good practice to have some logic that handles a specific URL pattern you decide, and then build all the pages on top of that.
Flight PHP
Flight PHP is a very lightweight microframework that can do a lot of cool stuff, including URL routing. It can be installed via Composer, or manually downloaded and extracted to your project directory.
The way it handles routing is by redirecting all URL requests to your index.php file, and from there you can say what to do with each URL:
Flight::route('/', function(){
echo 'This is my Home';
});
//
Flight::route('/customer/detail/@id', function($id){
echo 'Load details for customer with id: ' + $id;
});
You can look at the Documentation to see all the options they have to define patterns to your URL.
The 3 Levels
For this tutorial, let’s build a single routing logic that can handle up to 3 elements in a URL: controller, method, and parameter:
- Root Level: /
- Controller Level: /customer
- Method Level: /customer/new
- Parameter Level: /customer/detail/73
To do this, we can specify this specific pattern:
Flight::route('/(@controller(/@method(/@id)))', static function ($controller, $method, $id) {
//define default values for root level controller and method
$controller = $controller ? $controller : 'Home';
$method = $method ? $method : 'main';
$controller = '\YourNamespace\Controller\\' . ucfirst(strtolower($controller)) . 'Controller';
//check if they are valid
if( ! class_exists($conName) ||
(class_exists($conName) && ! method_exists($conName, $method)))
{
//skip to next route if not found
return true;
}
//instantiate controller and call the speficied method
$ct = new $controller();
$ct->$method($id);
});
Flight::route('*', static function(): void {
Flight::notFound();
});
Flight::start();
Handling 404
You might have noticed that there is a function called not_found. Create this function to handle all the cases where any controller or a method don’t exist. There you can do whatever you might want:
- Show a 404 error page.
- Redirect to another URL
- Log a message to a server
- Throw an exception
function not_found()
{
Flight:redirect('/not-found.php');
}
Using API and Webpage together
If you are building a project where you have API and webpages handled by the same domain, you can use this same logic to handle both of them just by having another similar routing function to API calls
/*
* Handles all calls that starts with /api
* search for controllers inside ApiController namespace
*/
Flight::route('/api/(@controller(/@method(/@id)))', function ($controller, $method, $id) {
$controller = $controller ? $controller : 'Home';
$method = $method ? $method : 'main';
$controller = '\YourNamespace\ApiController\\' . ucfirst(strtolower($controller)) . 'Controller';
...
});
/*
* Handles all webpage calls
* search for controllers inside Controller namespace
*/
Flight::route('/(@controller(/@method(/@id)))', function ($controller, $method, $id) {
$controller = $controller ? $controller : 'Home';
$method = $method ? $method : 'main';
$controller = '\YourNamespace\Controller\\' . ucfirst(strtolower($controller)) . 'Controller';
...
});
Since Flight resolves the first pattern that matches the requested URL, it is wise to put more specific ones first, and then the generic ones after that.
Link to FlightPHP
Hope this helps you guys out, Bye!