rc router
Added the ability to write your own parsers
这是rc router 。这是一个基于正则表达式的简单路由器,允许您通过在路由中使用占位符来传递变量。
我这样做是为了动动脑筋,充分了解路由在 PHP 领域的工作原理。我写这篇文章是为了巩固我的知识,而不是仅仅阅读它并假设我知道它做了什么。
只需使用作曲家:
composer require robert430404/rc-router
这是一个 Composer 包,因此它依赖于 Composer 来自动加载类。然后,您创建 Router() 对象的新实例并开始将路由分配给该实例。一旦定义了路由,您就可以将 Router() 传递到 Resolver() 中,它会处理您的路由。
<?php
use RcRouter Router ;
use RcRouter Utilities Resolver ;
use RcRouter Exceptions RouteNotFoundException ;
include ' vendor/autoload.php ' ;
$ router = new Router ();
$ router -> request ([ ' GET ' ], ' / ' , function () {
echo ' Closure Handler Test ' ;
});
$ uri = $ _SERVER [ ' REQUEST_URI ' ]; // You do not have to use globals here if you have access to a different source.
$ method = $ _SERVER [ ' REQUEST_METHOD ' ]; // You simply need to pass these (uri and method) as strings to the Resolver.
try {
new Resolver ( $ uri , $ method , $ router );
} catch ( RouteNotFoundException $ e ) {
echo ' 404 not found ' ;
}
路由器支持字符串和整数 url 变量,并将它们以数组形式传回给您。您可以以任何您想要的方式进一步扩展它。路由器的使用方式非常灵活。
您可以向每个路由传递一个闭包或一个命名处理函数来控制路由匹配时发生的情况。
当找不到路由时,解析器会抛出 RouteNotFoundException,并允许您捕获并创建 404 处理程序。
您现在还可以编写自己的自定义解析器并将它们传递到解析器中,以使 Controller@Action 调用之类的事情更容易、更清晰地实现。
<?php
use RcRouter Router ;
use RcRouter Utilities Resolver ;
use RcRouter Exceptions RouteNotFoundException ;
use YourProject Routing CustomParser ;
include ' vendor/autoload.php ' ;
$ router = new Router ();
$ router -> request ([ ' GET ' ], ' / ' , function () {
echo ' Closure Handler Test ' ;
});
$ uri = $ _SERVER [ ' REQUEST_URI ' ]; // You do not have to use globals here if you have access to a different source.
$ method = $ _SERVER [ ' REQUEST_METHOD ' ]; // You simply need to pass these (uri and method) as strings to the Resolver.
try {
new Resolver ( $ uri , $ method , $ router , new CustomParser ());
} catch ( RouteNotFoundException $ e ) {
echo ' 404 not found ' ;
}
如果您不传递自定义解析器,它将默认为内置解析器。
当您的路线中有占位符时,它们会作为数组返回给您,您可以在处理程序中访问该数组。
您可以访问字符串占位符。定义路由时,您可以使用 {} 来定义字符串占位符,如下所示:
<?php
$ router -> request ([ ' GET ' ], ' /{placeholder} ' , function ( $ mapped ) {
// Deal with returned data and route response here.
});
您还可以访问整数占位符。当您定义路线时,您可以使用 {:i} 来定义整数占位符,如下所示:
<?php
$ router -> request ([ ' GET ' ], ' /{placeholder:i} ' , function ( $ mapped ) {
// Deal with returned data and route response here.
});
基于简单闭包的路线:
<?php
use RcRouter Router ;
use RcRouter Utilities Resolver ;
use RcRouter Exceptions RouteNotFoundException ;
include ' vendor/autoload.php ' ;
$ router = new Router ();
$ router -> request ([ ' GET ' ], ' / ' , function () {
echo ' Closure Handler Test ' ;
});
$ uri = $ _SERVER [ ' REQUEST_URI ' ]; // You do not have to use globals here if you have access to a different source.
$ method = $ _SERVER [ ' REQUEST_METHOD ' ]; // You simply need to pass these (uri and method) as strings to the Resolver.
try {
new Resolver ( $ uri , $ method , $ router );
} catch ( RouteNotFoundException $ e ) {
echo ' 404 not found ' ;
}
带有命名处理程序的简单路由:
<?php
use RcRouter Router ;
use RcRouter Utilities Resolver ;
use RcRouter Exceptions RouteNotFoundException ;
include ' vendor/autoload.php ' ;
$ router = new Router ();
$ router -> request ([ ' GET ' ], ' / ' , ' handler ' );
$ uri = $ _SERVER [ ' REQUEST_URI ' ]; // You do not have to use globals here if you have access to a different source.
$ method = $ _SERVER [ ' REQUEST_METHOD ' ]; // You simply need to pass these (uri and method) as strings to the Resolver.
try {
new Resolver ( $ uri , $ method , $ router );
} catch ( RouteNotFoundException $ e ) {
echo ' 404 not found ' ;
}
function handler ()
{
echo ' External Handler Test ' ;
}
基于正则表达式闭包的带有变量的路由:
<?php
use RcRouter Router ;
use RcRouter Utilities Resolver ;
use RcRouter Exceptions RouteNotFoundException ;
include ' vendor/autoload.php ' ;
$ router = new Router ();
$ router -> request ([ ' GET ' ], ' /{id:i}/{post:i} ' , function ( $ mapped ) {
echo ' <pre> ' ;
var_dump ( $ mapped );
echo ' </pre> ' ;
echo ' Route Found ' ;
});
$ uri = $ _SERVER [ ' REQUEST_URI ' ]; // You do not have to use globals here if you have access to a different source.
$ method = $ _SERVER [ ' REQUEST_METHOD ' ]; // You simply need to pass these (uri and method) as strings to the Resolver.
try {
new Resolver ( $ uri , $ method , $ router );
} catch ( RouteNotFoundException $ e ) {
echo ' 404 not found ' ;
}
带有变量和外部处理程序的正则表达式路由:
<?php
use RcRouter Router ;
use RcRouter Utilities Resolver ;
use RcRouter Exceptions RouteNotFoundException ;
include ' vendor/autoload.php ' ;
$ router = new Router ();
$ router -> request ([ ' GET ' ], ' /{id:i}/{post:i} ' , ' handler ' );
$ uri = $ _SERVER [ ' REQUEST_URI ' ]; // You do not have to use globals here if you have access to a different source.
$ method = $ _SERVER [ ' REQUEST_METHOD ' ]; // You simply need to pass these (uri and method) as strings to the Resolver.
try {
new Resolver ( $ uri , $ method , $ router );
} catch ( RouteNotFoundException $ e ) {
echo ' 404 not found ' ;
}
function handler ( $ mapped )
{
echo ' <pre> ' ;
var_dump ( $ mapped );
echo ' </pre> ' ;
echo ' Route Found ' ;
}
“$mapped”变量返回如下结构:
<?php
$ mapped = [
' all ' => [
' id ' => 0 ,
' name ' => ' Robert '
],
' int ' => [
' id ' => 0
],
' string ' => [
' name ' => ' Robert '
],
];