em4nl/urouter

简单的基于trie的路由器

v0.0.4 2019-03-07 11:12 UTC

This package is auto-updated.

Last update: 2024-09-08 07:28:54 UTC


README

一个简单的基于trie的PHP路由器

安装

通过composer

composer require em4nl/urouter

使用

假设您正在使用自动加载,并且您的composer vendor目录位于./vendor

<?php

require_once __DIR__ . '/vendor/autoload.php';

$router = new Em4nl\U\Router();

// set a base path if this app doesn't live at the root path
// (right behind the domain)
$router->base('/my-app');

$router->get('/', function() {
    echo 'the index route';
});

$router->get('/test', function() {
    echo "the /test route";
});

$router->get('/:thing', function($thing) {
    echo "I like $thing!";
});

$router->get('/test/*', function($wildcard_match) {
    // will match paths of arbitrary length behind /test/ ...
});

$router->post('/form', function($context) {
    // ...
});

$router->catchall(function() {
    header('HTTP/1.1 404 Not Found');
    // ...
});

$router->run();

路由不需要按特定顺序定义,它们将自动按特定性匹配。例如,如果您访问/test/,则/test路由将匹配,而不是/:thing路由,即使后者在源代码中定义得较早。

开发

安装依赖项

composer install

运行测试

./vendor/bin/phpunit tests

许可协议

MIT许可协议