evgsavosin/choco-router 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

evgsavosin/choco-router

最新稳定版本:2.1.1

Composer 安装命令:

composer require evgsavosin/choco-router

包简介

Modern router for PHP based on regex expression with cache system.

README 文档

README

PHPUnit Tests Stable PHP License

Modern router for PHP based on regex expression with cache system. Caching has driver support: file system, APCum, Memcached. Defining routes is possible both using methods and attributes from PHP 8.0.

Requirements

  • PHP 8.1 or newer;
  • APCu (optional);
  • Memcached (optional).

Install

Install via composer:

composer require evgsavosin/choco-router

Usage

Basic usage

To use it is necessary to define the call of classes using for example PSR-11 implementation.

<?php 

declare(strict_types=1);

require 'vendor/autoload.php';

use ChocoRouter\SimpleRouter;
use ChocoRouter\HttpMethod;

$router = new SimpleRouter();

$router->addRoute(HttpMethod::GET, '/foo', fn (): string => 'Foo!' );
$router->addRoute(
    HttpMethod::POST, 
    '/foo/{bar}', 
    fn (mixed $value): string => "Foo bar and {$value}!",
    ['bar' => '[0-9]+']
);

try {
    $router->resolve(
        $_SERVER['REQUEST_METHOD'], 
        $_SERVER['REQUEST_URI']
    )->callableResolve(function (mixed $handler, array $arguments): mixed {
        if (is_string($handler)) {
            [$controllerName, $methodName] = explode('@', $handler);

            // PSR-11 implementation for classes: controllers, actions and etc...
        } elseif (is_callable($handler)) {
            $handler(...$arguments);
        }
    }); 
} catch (HttpException $e) {
    if ($e->getCode() === HttpException::NOT_FOUND) {
        // Handle 404...
    } else if ($e->getCode() === HttpException::BAD_REQUEST) {
        // Handle 400...
    }
}

Route definition

The route can be defined with method: addRoute(HttpMethod $httpMethod, string $uri, mixed $handler, array $parameters = []): void. Parameters can be passed {bar} or {bar?} with regular expressions ['bar' => '[0-9]+']. Real example:

$router->addRoute(HttpMethod::GET, '/foo/{bar?}', 'foo-bar', ['bar' => '[0-9]+']);

A question mark means the parameter is optional.

The route group is defined using addGroup(string $prefix, callable $callback): void method. Real example:

$router->addGroup('/gorge', function (RouteCollection $r): void {
    $router->addRoute(HttpMethod::GET, '/foo/{bar?}', 'foo-bar', ['bar' => '[0-9]+']);
});

HTTP Methods

Full list of methods:

HttpMethod::CONNECT
HttpMethod::HEAD
HttpMethod::GET
HttpMethod::POST
HttpMethod::PUT
HttpMethod::DELETE
HttpMethod::OPTIONS

Configuration

You can set the configuration when initializing a simple router:

$router = new SimpleRouter([
    'cacheDisable' => false,
    'cacheDriver' => FileDriver::class,
    'cacheOptions' => [] 

    /*
        For memcached driver, there passed array of servers. 
        For file driver, there passed path to cache directory.
    */
]);

Attributes

The router supports attributes from PHP 8.0. Example:

use App\Action\FooAction;

$router = new SimpleRouter();
$router->load([FooAction::class]);
$router->resolve(/*...*/)->callableResolve(/*...*/);

Cache

Router has support cache system with defined drivers:

  • ChocoRouter\Cache\Drivers\FileDriver::class;
  • ChocoRouter\Cache\Drivers\ApcuDriver::class;
  • ChocoRouter\Cache\Drivers\MemcachedDriver::class.

For use cache move definition routes to cache callback:

$router = new SimpleRouter([
    'cacheDriver' => FileDriver::class
]);

$router->cache(static function (RouteCollection $r): void {
    $r->addRoute(HttpMethod::GET, '/foo/{bar}', App\Actions\FooAction::class, ['bar' => '[0-9]+']);
});

$router->resolve(/*...*/)->callableResolve(/*...*/);

Contributing

The author has not yet had time to write instructions, but any pull request or issue will be glad.

License

Choco Router has MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固