Download the PHP package khantloonthu/php-router without Composer
On this page you can find all versions of the php package khantloonthu/php-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download khantloonthu/php-router
More information about khantloonthu/php-router
Files in khantloonthu/php-router
Package php-router
Short Description A simple and lightweight PHP router package.
License MIT
Informations about the package php-router
khantloonthu/php-router
Table of Contents
- Overview
- Installation
- Usage
- Routing
- Routing with params
- Global Middleware
- Route-Specific middlewares
- Aborting
- Redirecting
- Handle custom error pages
Overview
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
Installation is possible using Composer
Usage
Create an instance of \KhantLoonThu\PhpRouter\Router
, define some routes onto it, and run it.
Routing
Routing shorthands for single request methods are provided:
Note:
- [Routes must be hooked before $router->start(); is being called].
- [Unregistered routes will trigger 404 - not found].
Routing with params
Note: If route doesn't passed param, it will trigger 400 - Bad Request.
Global Middleware
middlewares accept only true
, false
, and ['status' => $statusCode, 'message' => 'Unauthorized']
;
Route-Specific middlewares
Route specific middlewares accept only true
, false
, and ['status' => $statusCode, 'message' => 'Unauthorized']
;
Aborting
If we want to abort before rendering we can use $router->abort($statusCode);
Redirecting
If we want to redirect after finishing the logic, we can use $router->redirect($path);
Handle custom error pages
If we want to show Custom Error Pages like 403
, 404
, 500
, we can use $router->handle($statusCode, callable);