PHPRouter is a simple routing extension for PHP inspired by express framework in Node.JS.
Download the zip folder and extract to the directory where your project is located in.
Include the router in the project like
include_once('PHPRouter/router.php');
PHP router supports GET, POST, PUT, DELETE, PATCH and ANYrequests. Function any can be used to capture all events. The callback will return request parameters and response object in order.
A typical route looks initialization looks like:
//where method is get,post,put,delete, patch or any
$app->method('/', function($request,$response){
$response->send("GET request");
});
Support for complex routes using regex
//where method is get,post,put,delete or patch
$app->method('/:id', function($request,$response){
$response->send("GET request");
});
Here the id will be returned as name-value pair in request array's "params" field.
<?php
require_once('../PHPRouter/router.php');
//Initalizing the PHPRouter class
$app = new PHPRouter\Router();
/*****
Routes
******/
//All HTTP request for /employee will come here
$app->any('/employee', function($request,$response){
$response->send("ANY request");
});
//All GET request for /user will come here
$app->get('/user', function($request,$response){
$response->json(["id"=>"1","name"=>"DroidHat","url"=>"http://www.droidhat.com"],200);
});
//All POST request for /:id will come here; where id is any alphanumeral
$app->post('/:id', function($request,$response){
$response->json(["id"=>($request["params"]["id"])],200);
});
//All POST request for /:id/:name will come here; where id and name are any alphanumeral
$app->post('/:id/:name', function($request,$response){
$response->json(["id"=>($request["params"]["id"]),"name"=>($request["params"]["name"])],200);
});
$app->put('/', function($request,$response){
$response->send("PUT request");
});
$app->patch('/', function($request,$response){
$response->send("PATCH request");
});
$app->delete('/', function($request,$response){
$response->send("DELETE request");
});
//Error Handler
$app->error(function(Exception $e,$response){
$response->send('path not found',404);
});
//Starting the router
$app->start();
?>
$app->get()
$app->post()
$app->put()
$app->delete()
$app->patch()
$app->any()
$app->error()
$app->start()
$app->method($path, function($request,$response){});
where method is get, post, put, delete, patch or any. any supports all methods.
The first parameter is the route path like '/' or '/user'. Second parameter is the callback function, i.e., the function to be called when processing is done. The callback function takes request array and response object as parameters.
Contains headers(HTTP information, Request information and PHP_AUTH) information, body parameter, url parameters, files and cookies informations in array format.
Array indexes
"raw": Body in raw format
"body": Body in associative array format like $_POST
"header": HTTP,REQUEST and PHP_AUTH information
"method": The type of HTTP REQUEST
"params": URL parameters like $_GET
"files": FIles if any available like $_FILES
"cookies": Cookies if any available like $_COOKIE
Usage
$request["body"] to access body parameters
response is an object of class Response. It has methods such as send, json and status.
Fucntions
1.
send($message,$status)
: $message is the message that you want to output to the requester and $status is an optional field in case you want to send status also.2.
json($message,$status)
: $message is the message that you want to output to the requester and $status is an optional field in case you want to send status also. The difference is that here $message should be a PHP array that will be converted the function to JSON.3.
status($status)
: send HTTP status only.
Usage
$response->send("Hello World",200); to output to the requester "hello" world with a status of 200
$app->error(function(Exception $e){})
Error function takes a callback function as a parameter. The callback function will be passed exception information if any occurs.
$app->start();
Start the routing process.
MIT