apex/router

轻量级,直观的路由器

安装: 20

依赖者: 1

建议者: 0

安全: 0

星标: 2

关注者: 1

分支: 0

开放问题: 0

类型:

1.0.1 2024-01-11 16:34 UTC

This package is auto-updated.

Last update: 2024-09-11 18:17:48 UTC


README

这是一个轻量级、易于理解和使用的HTTP路由器,它还完全可选地集成了Syrus模板引擎,使其本质上成为一个小巧的微框架。它不适合做重负载,但如果您只需要一个结构良好且快速运行的解决方案,它将是极好的。支持以下功能:

  • 使用YAML文件定义路由,支持部分和完整匹配路径、动态路径参数和多个主机名。
  • 完全支持PSR15和PSR7,并使用中间件类来转发所有HTTP请求。
  • 所有中间件类都完全支持通过Apex容器进行属性和构造函数依赖注入。
  • 可选集成Syrus模板引擎,允许自动路由/映射模板,每个模板一个单独的PHP文件,每个PHP类中包含特定于HTTP方法的PHP函数。

安装

使用Composer安装:

composer require apex/router

目录

  1. 基本配置
  2. 路由YAML文件
  3. 中间件类
  4. 路由函数
  5. Syrus模板引擎集成

基本用法

对于快速示例,请查看此包附带example.php脚本。

首先,将一些路由添加到您的YAML文件中

routes:
    default: Syrus
    members: MembersArea
    contact$: ContactPage
    "product/:category/:product_id": PathParamsExample

use Apex\Router\Router;
use Nyholm\Psr7Server\ServerRequestCreator;
use Nyholm\Psr7\Factory\Psr17Factory;
use League\Uri\Http;

require_once("./vendor/autoload.php");

// Define a uri to test with
$uri = Http::createFromString("http://example.com/category/cars");

// Generate PSR7 compliant server request object
$factory = new Psr17Factory();
$creator = new ServerRequestCreator($factory, $factory, $factory, $factory);
$request = $creator->fromGlobals()->withUri($uri);

// Handle http request via router
$router = new Router();
$response = $router->handle($request);

// If this is handled via Syrus auto-routing, the 
// HTML would have already been output to the user. 
// Otherwise, $response is a PSR7 ResponseInterface object.

// Alternatively, we can use the lookup method
$response = $router->lookup($request);

// This simply determines the route, obtains any dynamic path parameters, 
// and instantiates the middleware, but does not execute the process() method within it.
// Instead, $response is a Apex\Router\RouterResponse instance.

// Now, to execute the middle wear and output:
$router->execute($response, true);

支持

如果您有任何问题、问题或反馈,请随时在ApexPl Reddit子版块上留言,以获得快速而有帮助的回应。