memcrab/router

基于memCrab Core API的PHP路由器

安装次数 36,696

依赖项: 0

建议者: 0

安全: 0

星标: 2

关注者: 3

分支: 2

开放性问题: 0

类型:软件包

1.4.3 2021-08-25 12:39 UTC

This package is auto-updated.

Last update: 2024-09-25 19:07:20 UTC


README

状态

Build Status Dependency Status Total Downloads Latest Stable Version Latest Unstable Version License composer.lock available

这是一个基于yaml配置文件的php路由器,支持每个路由条件中的正则表达式。这有助于仅使用url中的一部分数字或必须的单词部分等,来构建更精确的路由。

特性

  • 支持任何类型路由中的正则表达式
  • 支持单一URL通过不同的请求方法(POST,GET,PUT,DELETE等)进行多重路由
  • 支持完整URL或仅请求URI
  • 所有配置都在简单的YAML文件中
  • 每个路由可以返回已命名的参数(您想要的任何数量,或者正则表达式中的数量)
  • 通过使用更新的pecl yaml-ext 2.0.0为php 7.0进行高性能yaml解析
  • 严格的编码标准,参数和返回值具有完整的类型定义(通过php 7.1)
  • PSR-4自动加载兼容结构
  • 使用PHPUnit进行单元测试
  • 易于用于任何框架

安装

composer require memcrab/router

依赖项

php扩展YAML

  • 针对Ubuntu/Debian
- apt-get update
- apt-get install php-pear
- apt-get install php-dev
- apt-get install php-xml php7.0-xml
- apt-get install libyaml-dev
- pecl channel-update pecl.php.net
- pecl install yaml-2.0.0
  • 针对OS X
- brew install php71 --with-pear
- brew install autoconf
- touch $(brew --prefix php71)/lib/php/.lock && chmod 0644 $(brew --prefix php71)/lib/php/.lock
- pecl install yaml-2.0.0

使用方法

  • 初始化路由器:memCrab\Router()
  • 加载路由:->loadRoutesFromYaml(string $filePath)
    • $filePath - 路由yaml文件的路径
  • 运行匹配:->matchRoute(string $url, string $method)
    • $url - URL(http://example.com/posts)或页面请求URI(/post
    • $method - http请求方法
  • 使用以下方式使用路由器数据:
    • getService() - 返回我们调用的组件
    • getAction() - 返回组件中将运行的操作
    • getParams() - 返回路由正则表达式参数

YAML配置示例

/:
  GET: [Index, getMain]
/post/:
  GET:    [Post, get]
  POST:   [Post, add]
  PATCH:  [Post, save]
  DELETE: [Post, delete]
/post/publish/:
  POST: [Post, setPublishing]
/catalog/([a-zA-Z0-9]+)-([a-zA-Z0-9]+)/: 
  GET: [Catalog, filter, key1, value1]

运行示例

<?php
declare (strict_types = 1);
require_once __DIR__ . "/vendor/autoload.php";

use memCrab\Exceptions\FileException;
use memCrab\Exceptions\RoutingException;
use memCrab\File\Yaml;
use memCrab\Router\Router;

try {
  # Read routes from yaml
  $Yaml = new Yaml();

  $routes = $Yaml->load("../src/routs.example.yaml", null)->getContent();

  # For enable cache You can use FileCache object as second parametr of
  # $Yaml->load() function. Use memCrab\Cache library for it.
  # Redis Cache: $FileCache = new RedisCache([Redis obj]);
  # PHP file Cache: $FileCache = new PHPCache([PathToTMLFolder]);

  # Initialize Router
  $Router = new Router();
  $Router->loadRoutes($routes);

  # Routing
  $Router->matchRoute("http://example.com/post/", "POST");
  # Run your Controller|Service|Component
  $ServiceName = $Router->getService();
  $ActionName = $Router->getAction();
  $Service = new $ServiceName();
  $Response = $Service->$Action($Router->getParams());
} catch (RoutingException $error) {
  $Respose = new \Response();
  $Respose->setErrorResponse($error);
} catch (FileException $error) {
  $Respose = new \Response();
  $Respose->setErrorResponse($error);
}

$Respose->sendHeaders();
$Respose->sendContent();

待办事项

  • 添加对后缀的支持 - URI中不涉及路由的右侧部分,如.html,.php,最后的"/"等
  • 添加对前缀的支持 - URI中不涉及路由的左侧部分,如语言部分(uk/us/fr/ru)或地理位置部分(europe/asia)等

MIT许可