定制 archict/router 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

archict/router

最新稳定版本:v1.1.0

Composer 安装命令:

composer require archict/router

包简介

Simple HTTP router

README 文档

README

Tests

Usage

This Brick allows you to setup route with a request handler and add some middleware. Let's see how to do that!

A simple route ...

There is 2 way for creating a route: Having a whole controller class, or a simple closure.

First you need to listen to the Event RouteCollectorEvent:

<?php

use Archict\Brick\Service;
use Archict\Brick\ListeningEvent;
use Archict\Router\RouteCollectorEvent;

#[Service]
class MyService {
    #[ListeningEvent]
    public function routeCollector(RouteCollectorEvent $event) 
    {
    }
}

Then just create the route with the Event object:

With a closure:

$event->addRoute(Method::GET, '/hello', static fn() => 'Hello World!');

The closure can take a Request as argument, and return a string or a Response.

With a controller class

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

$event->addRoute(Method::GET, '/hello', new MyController());

class MyController implements RequestHandler {
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        return ResponseFactory::build()
            ->withStatus(200)
            ->withBody('Hello World!')
            ->get();
    }
}

Your controller must implement interface RequestHandler. The method handle can return either a string or a Response.

Please note that you can define only one handler per route.

You can also pass directly the classname, the Router will then instantiate it and inject Services.

The first argument of method RouteCollectorEvent::addRoute must be a string of an allowed HTTP method. Enum Method contains list of allowed methods. If your route can match multiple method you can pass an array of method, or Method::ALL.

The second argument is your route, it can be a simple static route, or a dynamic one. In this last case, each dynamic part must be written between {}. Inside there is 2 part separated by a :, name of the part, and pattern. The name of the dynamic part allow you to easily retrieve it in Request object.

The pattern can be empty, then it will match all characters until next / (or the end), or it can be a regex with some shortcuts:

  • \d+ match digits [0-9]+
  • \l+ match letters [a-zA-Z]+
  • \a+ match digits and letters [a-zA-Z0-9]+
  • \s+ match digits, letters and underscore [a-zA-Z0-9_]+

You can also have an optional suffix to your route with [/suffix].

Here is an example: /article/{id:\d+}[/{title}].

If something went wrong along your process, you can throw an exception built with HTTPExceptionFactory. The exception will be caught by the router and used to build a response.

... with a middleware

Sometimes you have some treatment to do before handling your request. For that there is middlewares. You can define as many middlewares as you want. To define one, the procedure is pretty the same as for a simple route:

<?php

use Archict\Brick\Service;
use Archict\Brick\ListeningEvent;
use Archict\Router\RouteCollectorEvent;
use Psr\Http\Message\ServerRequestInterface;

#[Service]
class MyService {
    #[ListeningEvent]
    public function routeCollector(RouteCollectorEvent $event) 
    {
        $event->addMiddleware(Method::GET, '/hello', static function(ServerRequestInterface $request): ServerRequestInterface {
            // Do something
            return $request
        });
        // Or
        $event->addMiddleware(Method::GET, '/hello', new MyMiddleware());
    }
}

If you define your middleware with a closure, then it must return a Request. If it's an object, then your class must implement interface Middleware:

use Psr\Http\Message\ServerRequestInterface;

class MyMiddleware implements Middleware
{
    public function process(ServerRequestInterface $request): ServerRequestInterface
    {
        return $request;
    }
}

You can do whatever you want in your middleware. If something went wrong, the procedure is the same as for RequestHandler.

Special response handling

By special, we mean 404, 500, ... In short HTTP code different from 2XX. By default, Archict will use ResponseHandler which just set the corresponding headers. Via the config file of this Brick you change this behavior:

error_handling:
  404: \MyHandler
  501: 'Oops! Something went wrong'

You have 2 choices:

  1. Pass a string, Archict will use it as response body
  2. Pass a class string of a class implementing interface ResponseHandler, Archict will call it. For example:
<?php

use Archict\Router\ResponseHandler;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class MyHandler implements ResponseHandler
{
    public function handleResponse(ResponseInterface $response, ServerRequestInterface $request): ResponseInterface
    {
        $factory = new \GuzzleHttp\Psr7\HttpFactory();
        return $response->withBody($factory->createStream("Page '{$request->getUri()->getPath()}' not found!"));
    }
}

Your class can also have dependencies, as for a Service just add them to your constructor. These dependencies must be available Service.

archict/router 适用场景与选型建议

archict/router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 624 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 archict/router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 archict/router 我们能提供哪些服务?
定制开发 / 二次开发

基于 archict/router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 624
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-21