allmarkedup/super-sharp-router

一款专为PHP 5.4+设计的锐利路由库

v0.2 2014-10-30 10:28 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:49:16 UTC


README

这是一个简单、优雅的PHP 5.4+路由库。

灵感来源于(并大量提取自)Silex 路由,并基于 Symfony 组件 构建。

安装

使用 Composer

$ composer require allmarkedup/super-sharp-router

示例

“Hello World” 示例

<?php
$router = new Amu\SuperSharp\Router();

$router->get('/hello', function(){
    return 'Hello world!';
});

echo $router->match('/hello'); // Prints: Hello World!

匹配当前请求并返回响应对象

<?php
use Amu\SuperSharp\Http\Response;
use Symfony\Component\Routing\Exception\RouteNotFoundException;

$router = new Amu\SuperSharp\Router();
$router->get('/', function(){
    return new Response('This is the homepage');
});

try {
    $response = $router->match(); // matches against the current request
} catch (ResourceNotFoundException $e) {
    $response = new Response('No matching route found', 404);
}

$response->send();

基于HTTP动词的方法、动态路由参数和流畅的路由配置

<?php

$router->post('/articles', function(){
    return 'Article added!';
});

$router->get('/articles/{slug}', function($slug){
    return Example::find($slug);
});

$router->get('/users/{id}', function($id){
    return Example::find($id);
})
->assert('id', '\d') // $id route parameter must be a digit
->requireHttps();    // Must be HTTPS

运行测试

可以使用命令行中的PHP Unit运行测试

$ vendor/bin/phpunit

项目还包括一个 Grunt 监视任务,当文件更新时运行PHP Unit测试,这可以方便您使用。