Coder Social home page Coder Social logo

router's Introduction

router

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License
A barebones router for PHP.
It matches urls and executes PHP functions.
Automatic get variable based on handler function parameter list.
Suport to compile router callback handlers into plain array source code.

中文版.

Installation

composer require bephp/router

API Reference

group/prefix($prefix, $hook)

add group routers with same prefix. if not pass param $prefix just reset attribute prefix of router instance to empty string. will merge the $hook to this group.

match($method, $path, $callback, $hook)

create the router tree based on given $method and $path, the $callback and $hook will stored in the leaf node.

get/post/put/delete/head/options($path, $callback, $hook)

wrap the match method without $method parameter. also defined "post", "put", "delete", "head" and so on.

execute()

the enter point of the application. have 3 optional parameters, $params will be merged with request variable and passed into callback handler. can pass $method and $path when not deploy as web server, can using the 2 parameters to test this library.

error()

  1. if call this API with $error_code and $callback, just define the callback handler for the error code.
  2. if call this API with $error_code and other parameters, will trigger the error callback handler, the parameters will passed to the error handler.

hook()

  1. if call this API with $hook_name and $callback, just define the callback handler for the hook name.
  2. if call this API with $hook_name and other parameters, will trigger the hook handler, the parameters will passed to the hook handler.
  3. there's 2 spec hook: "before" and "after", this library will auto call "before" hook before execute the handler, and call "after" hook after execute the handler.
  4. the "after" hook will auto trigger with the return value of callback handler.
  5. the "before" hook, and other user define hooks will auto trigger with the current router object $router, and the merged params stored in $router->params. if these hook return false, will trigger 406 error handler.

Validate

using ctype functions to validate params in pathinfo
example:

if defined router: "/hello/:name:a.json", and using URL: "/hello/lloyd.json" to resolve url.
will call function "ctype_alpha" to validate "lloyd".
validate command to map the ctype functions:

A => ctype_alnum
a => ctype_alpha
d => ctype_digit
x => ctype_xdigit
l => ctype_lower
u => ctype_upper

Compile

the PHP request always match the callback handlers every time. but the request just match one callback. so we can compile the routed tree node into plain array, to save time.

DEV model

using CRouter instead of Router, will always compile the source code into target file.

$crouter = new CRouter("router.inc.php", true);

PRODUCTION model

just include the target source code, and execute it with parameters.

$router = include("router.inc.php");
$router->execute();

Performance

  1. using tree struct to stored callback handler on leaf node. Ensure that the time complexity of find callback function is O(log n). Tree Node
  2. using CRouter class, suport to compile router callback handlers into plain array source code. so can save time to create tree node to store callback by split pathinfo.

using "php-router-benchmark" to test router performance.

Worst-case matching

This benchmark matches the last route and unknown route. It generates a randomly prefixed and suffixed route in an attempt to thwart any optimization. 1,000 routes each with 9 arguments.

This benchmark consists of 10 tests. Each test is executed 1,000 times, the results pruned, and then averaged. Values that fall outside of 3 standard deviations of the mean are discarded.

Test Name Results Time + Interval Change
Router - unknown route (1000 routes) 993 0.0000232719 +0.0000000000 baseline
Router - last route (1000 routes) 981 0.0000955424 +0.0000722705 311% slower
FastRoute - unknown route (1000 routes) 990 0.0005051955 +0.0004819236 2071% slower
FastRoute - last route (1000 routes) 998 0.0005567203 +0.0005334484 2292% slower
Symfony2 Dumped - unknown route (1000 routes) 998 0.0006116139 +0.0005883420 2528% slower
Symfony2 Dumped - last route (1000 routes) 998 0.0007765370 +0.0007532651 3237% slower
Symfony2 - unknown route (1000 routes) 996 0.0028456177 +0.0028223458 12128% slower
Symfony2 - last route (1000 routes) 993 0.0030129542 +0.0029896823 12847% slower
Aura v2 - last route (1000 routes) 989 0.1707107230 +0.1706874511 733450% slower
Aura v2 - unknown route (1000 routes) 988 0.1798588730 +0.1798356011 772760% slower

Example

belong is one simple example, see the full examples in example.php.

(new Router())
->error(405, function($message){
    header('Location: /hello/world', true, 302);
})
->get('/hello/:name', function($name){
    echo "Hello $name !!!";
})

Start server

php -S 0.0.0.0:8888 example.php

Test

Url not match, trigger 405 error handler.

curl -vvv 127.0.0.1:8888
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:8888
> Accept: */*
> 
< HTTP/1.1 302 Found
< Host: 127.0.0.1:8888
< Connection: close
< X-Powered-By: PHP/5.5.9-1ubuntu4.12
< Location: /hello/world
< Content-type: text/html
< 
* Closing connection 0

Url match get current result.

curl -vvv 127.0.0.1:8888/hello/lloyd
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> GET /hello/lloyd HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:8888
> Accept: */*
> 
< HTTP/1.1 200 OK
< Host: 127.0.0.1:8888
< Connection: close
< X-Powered-By: PHP/5.5.9-1ubuntu4.12
< Content-type: text/html
< 
* Closing connection 0
Hello lloyd !!!

Demo

there's one blog demo, work with ActiveRecord and MicroTpl.

router's People

Contributors

liushaohui123 avatar lloydzhou avatar wjiec avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

router's Issues

请教能匹配上,但是无法获值是哪里不对?

    ->get('/Message/photo/:id:d-:index:d-:width:d-:height:d.jpg',function($id, $index,$width,$height){

        $arr = get_defined_vars();

        print_r($arr);

    })

浏览器访问这个URL能正常,但结果是下面这样,是我哪里处理的不对?

Array ( [id] => [index] => [width] => [height] => )

compile feature

compile the matched callback handlers into plain array. do not create array "$_tree" every time.

hook no need to return $params

i did't want return $params in every hooks.
so only hook return false, will trigger 406 error.
but call_user_func_array and call_user_func, can not transfer reference manual, so transfer $this to hook callback. fe25fc5

perfomance

using explode to split the path info, instead of preg_match.

文件层级怪怪的

感觉源码文件都放到root下面有点儿不太清晰,不过也就一个文件...
ToT

add hook to group

when define one group, can define hooks for this group.

$router->group('/admin', array('auth'));

this example define group url prefixed by '/admin', and all these urls has 'auth' hook.

feature to add hook

add hook like auth and so on.
may be can format parameters before call handler function.

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.