Git Product home page Git Product logo

phprouter's Introduction

PHPRouter is a simple routing extension for PHP inspired by express framework in Node.JS.

Installation

Download the zip folder and extract to the directory where your project is located in.

Usage

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.


Example

<?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();
?>

API


Route Methods

$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.


Request Array

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 Object

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


Error

$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.


Start Routing

$app->start();

Start the routing process.


License

MIT

phprouter's People

Contributors

mohdrashid avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.