robert430404/rc-router

这是一个使用正则表达式解析的简单路由器

2.1.0 2017-03-07 20:08 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:30:32 UTC


README

Latest Stable Version Build Status codecov

这是什么?

这是RC Router。这是一个简单的基于正则表达式的路由器,允许您通过在路由中使用占位符传递变量。

为什么要写这个?

我这么做是为了锻炼我的大脑,并全面理解PHP空间中的路由工作原理。与其只是阅读它并假设我知道它做什么,不如写这个来巩固我的知识。

安装包

只需使用composer

composer require robert430404/rc-router

它是如何工作的?

这是一个composer包,因此它依赖于composer来自动加载类。然后您创建一个新的Router()对象实例,并将您的路由分配给该实例。一旦定义了您的路由,然后将Router()传递给Resolver(),它将处理您的路由。

<?php

use RcRouter\Router;
use RcRouter\Utilities\Resolver;
use RcRouter\Exceptions\RouteNotFoundException;

include 'vendor/autoload.php';

$router = new Router();

$router->request(['GET'], '/', function () {
    echo 'Closure Handler Test';
});

$uri    = $_SERVER['REQUEST_URI'];    // You do not have to use globals here if you have access to a different source.
$method = $_SERVER['REQUEST_METHOD']; // You simply need to pass these (uri and method) as strings to the Resolver.

try {
    new Resolver($uri, $method, $router);
} catch (RouteNotFoundException $e) {
    echo '404 not found';
}

有哪些功能?

该路由器支持字符串和整数URL变量,并以数组形式将它们传递回您。您可以以任何方式进一步扩展它。路由器在使用方面非常灵活。

您可以为每个路由传递闭包或命名处理函数来控制路由匹配时发生的事情。

当找不到路由时,解析器会抛出RouteNotFoundException,允许您捕获并创建自己的404处理程序。

您现在可以编写自己的自定义解析器并将其传递给解析器,以便使Controller@Action调用更容易、更干净地实现。

<?php

use RcRouter\Router;
use RcRouter\Utilities\Resolver;
use RcRouter\Exceptions\RouteNotFoundException;
use YourProject\Routing\CustomParser;

include 'vendor/autoload.php';

$router = new Router();

$router->request(['GET'], '/', function () {
    echo 'Closure Handler Test';
});

$uri    = $_SERVER['REQUEST_URI'];    // You do not have to use globals here if you have access to a different source.
$method = $_SERVER['REQUEST_METHOD']; // You simply need to pass these (uri and method) as strings to the Resolver.

try {
    new Resolver($uri, $method, $router, new CustomParser());
} catch (RouteNotFoundException $e) {
    echo '404 not found';
}

如果您没有传递自定义解析器,它将默认为内置解析器。

占位符是如何工作的?

当您的路由中有占位符时,它们将以数组形式返回,您可以在处理程序中访问它。

您有权访问字符串占位符。当您定义路由时,您使用{}来定义字符串占位符,如下所示

<?php

$router->request(['GET'], '/{placeholder}', function ($mapped) {
    // Deal with returned data and route response here.
});

您还有权访问整数占位符。当您定义路由时,您使用{:i}来定义整数占位符,如下所示

<?php

$router->request(['GET'], '/{placeholder:i}', function ($mapped) {
    // Deal with returned data and route response here.
});

一些示例

基于闭包的简单路由

<?php

use RcRouter\Router;
use RcRouter\Utilities\Resolver;
use RcRouter\Exceptions\RouteNotFoundException;

include 'vendor/autoload.php';

$router = new Router();

$router->request(['GET'], '/', function () {
    echo 'Closure Handler Test';
});

$uri    = $_SERVER['REQUEST_URI'];    // You do not have to use globals here if you have access to a different source.
$method = $_SERVER['REQUEST_METHOD']; // You simply need to pass these (uri and method) as strings to the Resolver.

try {
    new Resolver($uri, $method, $router);
} catch (RouteNotFoundException $e) {
    echo '404 not found';
}

带有命名处理函数的简单路由

<?php

use RcRouter\Router;
use RcRouter\Utilities\Resolver;
use RcRouter\Exceptions\RouteNotFoundException;

include 'vendor/autoload.php';

$router = new Router();

$router->request(['GET'], '/', 'handler');

$uri    = $_SERVER['REQUEST_URI'];    // You do not have to use globals here if you have access to a different source.
$method = $_SERVER['REQUEST_METHOD']; // You simply need to pass these (uri and method) as strings to the Resolver.

try {
    new Resolver($uri, $method, $router);
} catch (RouteNotFoundException $e) {
    echo '404 not found';
}

function handler()
{
    echo 'External Handler Test';
}

带有变量的正则闭包路由

<?php

use RcRouter\Router;
use RcRouter\Utilities\Resolver;
use RcRouter\Exceptions\RouteNotFoundException;

include 'vendor/autoload.php';

$router = new Router();

$router->request(['GET'], '/{id:i}/{post:i}', function ($mapped) {
    echo '<pre>';
    var_dump($mapped);
    echo '</pre>';

    echo 'Route Found';
});

$uri    = $_SERVER['REQUEST_URI'];    // You do not have to use globals here if you have access to a different source.
$method = $_SERVER['REQUEST_METHOD']; // You simply need to pass these (uri and method) as strings to the Resolver.

try {
    new Resolver($uri, $method, $router);
} catch (RouteNotFoundException $e) {
    echo '404 not found';
}

带有变量和外部处理函数的正则路由

<?php

use RcRouter\Router;
use RcRouter\Utilities\Resolver;
use RcRouter\Exceptions\RouteNotFoundException;

include 'vendor/autoload.php';

$router = new Router();

$router->request(['GET'], '/{id:i}/{post:i}', 'handler');

$uri    = $_SERVER['REQUEST_URI'];    // You do not have to use globals here if you have access to a different source.
$method = $_SERVER['REQUEST_METHOD']; // You simply need to pass these (uri and method) as strings to the Resolver.

try {
    new Resolver($uri, $method, $router);
} catch (RouteNotFoundException $e) {
    echo '404 not found';
}

function handler($mapped)
{
    echo '<pre>';
    var_dump($mapped);
    echo '</pre>';
    
    echo 'Route Found';
}

返回值中包含什么?

“$mapped”变量返回如下结构

<?php

$mapped = [
    'all' => [
        'id'   => 0,
        'name' => 'Robert'
    ],
    'int' => [
        'id' => 0
    ],
    'string' => [
        'name' => 'Robert'
    ],
];