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 '
],
];