- Overview
- Installation
- Usage
- Routing
- Routing with params
- Global Middleware
- Route-Specific middlewares
- Aborting
- Redirecting
- Handle custom error pages
This is a lightweight, simple, and extensible HTTP router implemented in PHP designed for basic to medium complexity web applications. It supports route registration, middleware, dynamic route parameters, error handling, and HTTP method overrides. Built by Khant Loon Thu (https://github.com/KhantLoonThu).
Installation is possible using Composer
composer require khantloonthu/php-router
Create an instance of \KhantLoonThu\PhpRouter\Router
, define some routes onto it, and run it.
// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';
use \KhantLoonThu\PhpRouter\Router;
// Create Router instance
$router = new Router();
// Define routes
// ...
// Run it!
$router->run();
Routing shorthands for single request methods are provided:
$router->get('/', function() {
return "Hello World";
});
$router->get('/about-us', fn() => require __DIR__ . '/about-us.php');
$router->get('/contact-us', function () {
return require __DIR__ . '/contact-us.php';
});
$router->post('/pattern', function() {
// ...
});
$router->put('/pattern', function() {
// ...
});
$router->delete('/pattern', function() {
// ...
});
Note:
- [Routes must be hooked before $router->run(); is being called].
- [Unregistered routes will trigger 404 - not found].
// Required parameter
$router->get('/user/{id}', function($id) {
return "User ID: " . $id;
});
// Multiple required parameters
$router->get('/post/{postId}/comment/{commentId}', function($postId, $commentId) {
return "Post ID: $postId, Comment ID: $commentId";
});
Note: If route doesn't passed param, it will trigger 400 - Bad Request.
middlewares accept only true
, false
, and ['status' => $statusCode, 'message' => 'Unauthorized']
;
function checkLoggedIn($request) {
global $isLoggedIn;
if (!$isLoggedIn) {
return ['status' => 401, 'message' => 'Unauthorized: Please log in.'];
}
return true; // continue processing
}
$router->middleware('checkLoggedIn');
// or anonymous function style
$router->middleware(function ($request) {
global $isLoggedIn;
if (!$isLoggedIn) {
return false; // triggers 403 Forbidden by default
}
return true;
});
Route specific middlewares accept only true
, false
, and ['status' => $statusCode, 'message' => 'Unauthorized']
;
$router->get('/dashboard', function () {
return require __DIR__ . '/dashboard.php';
}, [
function checkAdmin() {
$isAdmin = false;
if (!$isAdmin) return false;
return true;
}
]);
If we want to abort before rendering we can use $router->abort($statusCode);
$router->post('/create-user', function () use ($router) {
$isAdmin = false;
if (!$isAdmin) {
$router->abort(403);
}
// continue with user creation...
});
If we want to redirect after finishing the logic, we can use $router->redirect($path);
$router->post('/send-mail', function () use ($router) {
// send mail logic
$mail->send();
$router->redirect('/');
});
If we want to show Custom Error Pages like 403
, 404
, 500
, we can use $router->handle($statusCode, callable);
$router->handle(404, function() {
return require __DIR__ . '/404.php';
});