corpus/router

简单路由器

v1.1.0 2022-12-15 05:19 UTC

README

Latest Stable Version License CI

简单的路由器集合

需求

  • php: >=7.1

安装

使用以下命令安装最新版本

composer require 'corpus/router'

使用方法

HttpRouter

<?php

require __DIR__ . '/../vendor/autoload.php';

// $_SERVER => ['REQUEST_METHOD' => [ 'method' => 'POST' ]]
$router = new \Corpus\Router\HttpRouter('\\Corpus\\Controllers', $_SERVER);

$route = $router->match('test/controller:action');

// $route =
//	[
//		'controller' => '\\Corpus\\Controllers\\test\\controller',
//		'action'     => 'action',
//		'options'    => [],
//		'request'    => [ 'method' => 'POST' ],
//	]

// ----------------

$route = $router->match('test/controller?query=whatwhat');

// $route =
//	[
//		'controller' => '\\Corpus\\Controllers\\test\\controller',
//		'action'     => NULL,
//		'options'    => [ 'query'  => 'whatwhat' ],
//		'request'    => [ 'method' => 'POST' ],
//	]

// ----------------

$route = $router->match($_SERVER['REQUEST_URI']);

// $route = Current Request

// ----------------

$url = $router->generate('myNamespace\\admin', 'index');

// $url = '/myNamespace/admin:index'

// ----------------

$url = $router->generate('\\Corpus\\Controllers\\myNamespace\\admin', 'index');

// $url = '/myNamespace/admin:index'

// ----------------

try {
	$url = $router->generate('\\Invalid\\Absolute\\Controller', 'index');
}catch (\Corpus\Router\Exceptions\NonRoutableException $e) {
	$url = 'fail';
}

// $url = 'fail'

文档

类: \Corpus\Router\HttpRouter

<?php
namespace Corpus\Router;

class HttpRouter {
	public const ACTION = 'action';
	public const CONTROLLER = 'controller';
	public const OPTIONS = 'options';
}

方法: HttpRouter->__construct

function __construct(string $rootNamespace [, array $server = []])
参数
  • 数组 $server - $_SERVER 数组 - 可选

方法: HttpRouter->match

function match(string $path) : ?array

将给定路径与路由数组匹配。

非空路由数组的存在并不保证 - 只保证格式正确。
是否存在路由由实现方的分发机制决定

返回的路由数组具有以下形状

[  
    // The controller action. Definition varies by router.  
    RouterInterface:ACTION     => 'action',  
  
    // An expected class name based on given rules. Not guaranteed to exist.  
    RouterInterface:CONTROLLER => '\Controller\www\index',  
  
    // Router specific but akin to $_GET - may contain additional options  
    RouterInterface:OPTIONS    => ['key' => 'value'],  
]  

将给定路径与路由数组匹配。

非空路由数组的存在并不保证 - 只保证格式正确。
是否存在路由由实现方的分发机制决定

返回的路由数组具有以下形状

[  
    // The controller action. Definition varies by router.  
    RouterInterface:ACTION     => 'action',  
  
    // An expected class name based on given rules. Not guaranteed to exist.  
    RouterInterface:CONTROLLER => '\Controller\www\index',  
  
    // Router specific but akin to $_GET - may contain additional options  
    RouterInterface:OPTIONS    => ['key' => 'value'],  
]  
参数
  • 字符串 $path - 要匹配的路径,包括查询字符串,例如 foo/bar.html?param=woo
返回
  • 数组 | null - 路由数组或失败时返回 null

方法: HttpRouter->generate

function generate($controller [, ?string $action = null [, array $options = []]]) : string

为给定的控制器、操作和选项生成 URL

参数
  • 对象 | 字符串 $controller - 实例或相对 'admin\index' 或绝对 '\Controllers\www\admin\index'

方法: HttpRouter->getNamespace

function getNamespace() : string
返回
  • 字符串 - 标准命名空间前缀

类: \Corpus\Router\CliRouter

<?php
namespace Corpus\Router;

class CliRouter {
	public const ARGUMENTS = 'arguments';
	public const ACTION = 'action';
	public const CONTROLLER = 'controller';
	public const OPTIONS = 'options';
}

方法: CliRouter->__construct

function __construct($rootNamespace [, array $arguments = []])
参数
  • 字符串 $rootNamespace - 控制器所在的命名空间前缀

方法: CliRouter->match

function match(string $path) : ?array

将给定路径与路由数组匹配。

非空路由数组的存在并不保证 - 只保证格式正确。
是否存在路由由实现方的分发机制决定

返回的路由数组具有以下形状

[  
    // The controller action. Definition varies by router.  
    RouterInterface:ACTION     => 'action',  
  
    // An expected class name based on given rules. Not guaranteed to exist.  
    RouterInterface:CONTROLLER => '\Controller\www\index',  
  
    // Router specific but akin to $_GET - may contain additional options  
    RouterInterface:OPTIONS    => ['key' => 'value'],  
]  
参数
  • 字符串 $path - 要匹配的路径,包括查询字符串,例如 foo/bar.html?param=woo
返回
  • 数组 | null - 路由数组或失败时返回 null

方法: CliRouter->getNamespace

function getNamespace() : string
返回
  • 字符串 - 标准命名空间前缀