一个独立,易于使用,可扩展且简单地了解PHP Web应用程序URL路由库,Mybe增强您的开发工具包或为您提供了一些灵感。
下载源代码并放置项目目录的正确位置。
获取路由器对象实例
use RouterOne Router ;
$ router = Router:: getInstance ();在单个文件中创建路由映射,并位于Projject的特定DIR中,在文件$this请参考混凝土$router Object实例。对于HTTP请求动词, RouteOne仅支持GET , POST (只需与PHP的$ _GET&$ _POST保持一致,如果需要或必要,请完全扩展。
例如, Foo.php 。 Routerone可以支持多个路由映射文件,例如frontend_route_map.php , backend_route_map.php ,。
// Closure function call
$ this -> get ( ' / ' , function () {
echo ' Hello! RouterOne ' ;
});
// `WelcomeController::hello()` call
$ this -> get ( ' / ' , [WeclomeController::class, ' hello ' ]);
设置路由图文件目录路径并加载它。 (路由图文件默认扩展名为.php )
$ router -> setIncludePath (` YOUR_ROUTE_MAP_FILE_DIR `);
$ router -> load ([ ' Foo ' ]); // Just file's name without extension或这样打电话
$ router -> setIncludePath (` YOUR_ROUTE_MAP_FILE_DIR `)-> load ([ ' Foo ' ]); // Just file's name without extension运行调度并启用所有已加载的路线,将返回有关实际调用路由的结果。
$ res = $ router -> dispatch ();控制器路线行动:
// Http GET
$ this -> get ( ' index ' , [ Controllers SiteController::class, ' index ' ]);
// Http POST
$ this -> post ( ' news/add ' , [ Controllers NewsController::class, ' add ' ]);封闭路线行动:
// Http GET
$ this -> get ( ' index ' , function () {
/**
* Some logic process code here
*/
});
// Http POST
$ this -> post ( ' news/add ' , function () {
/**
* Some logic process code here
*/
});使用路由参数,包装{}动态参数,然后通过外观顺序转移到控制器方法或Clousre函数参数。
$ this -> get ( ' test/{param1}/{param2} ' , [ Controllers TestController::class, ' params ' ]);
class TestController
{
public function params ( $ param1 , $ param2 )
{
// Some code ...
}
} $ this -> get ( ' test/{param1}/{param2} ' , function ( $ param1 , $ param2 ) {
// Some code
});中间件应该是对RouteMiddleWareInterface的实现,您可以在任意目录中找到中间软件类文件,例如MiddleWare Dir;
一个典型的中间软件类包含带有路由Action- $action参数的句柄handle()方法,如下:
use RouterOne MiddleWare RouteMiddleWareInterface ;
class AuthCheckMiddleWare implements RouteMiddleWareInterface
{
public static function handle ( $ action )
{
if ( ! AdminUser::Logged) {
exit ( ' Please login first. ' );
}
$ action ();
}
}在某些情况下,您可能需要在拒绝路线操作后进行一些过程,只需将中间软件逻辑放置在$action()呼叫语句之后即可。
use RouterOne MiddleWare RouteMiddleWareInterface ;
class AfterMiddleWare implements RouteMiddleWareInterface
{
public static function handle ( $ action )
{
$ action ();
echo ' This text will print after route action excuted. ' ;
}
}当定义中间软件时,可以通过路由器的middleware()方法设置路由为grouped表单, middleware()具有两个参数,第一个是middle-ware类名称阵列和支持更多的中间Wares,另一个是封闭功能,包括通用路由映射。
$ this -> middleware (
[
AuthCheckMiddleWare::class,
...
. . .
], function () {
$ this -> get ( ' admin/index ' , [ Controllers Admin AdminController::class, ' index ' ]);
$ this -> get ( ' admin/news/list ' , [ Controllers Admin NewsController::class, ' list ' ]);
. . .
. . .
});也可以nested
$ this ->middleware(
[
OuterMiddleWare::class,
], function () {
. . .
$ this ->middleware([
InnerMiddleWare::class
], function () {
$ this ->get(...
$ this ->post(...
. . .
});
...
. . .
});prefix()和suffix()方法也是grouped路由,它们可以方便地添加实用的前缀和后缀到特定路由。
添加前缀static/ ,然后将URLS':// domain/static/page1','// domain/static/page2'匹配。
$ this -> prefix ( ' static/ ' , function () {
$ this -> get ( ' page1 ' , ...);
$ this -> get ( ' page2 ' , ...);
. . .
});添加the ,然后将URLS':// domain/thepage1','// domain/thepage2'匹配。
$ this -> prefix ( ' the ' , function () {
$ this -> get ( ' page1 ' , ...);
$ this -> get ( ' page2 ' , ...);
. . .
});与使用prefix()相同,添加后缀.html ,然后URL更改为'://domain/page1.html'。
$ this -> suffix ( ' .html ' , function () {
$ this -> get ( ' page1 ' , ...);
. . .
. . .
});在prefix()和suffix()之间可以彼此nested 。
$ this -> prefix ( ' static/ ' , function () {
$ this -> get ( ' page1 ' , ...); // request url '://domain/static/page1' matched here
. . .
$ this -> suffix ( ' .html ' , function () {
$ this -> get ( ' page2 ' , ...); // request url '://domain/static/page2.html' matched here
});
});如果您的应用程序具有多个URL域,则使用domain()方法可以区分这些域,并将请求引导到相应的路由组。
$ this -> domain ( ' www.hereisyoursite.com ' , function () { // PC Pages
$ this -> get ( ' index ' , ...);
. . .
});
$ this -> domain ( ' m.hereisyoursite.com ' , function () { // Mobile Pages
$ this -> get ( ' index ' , ...);
. . .
});
$ this -> domain ( ' new.hereisyoursite.com ' , function () { // Current Domain routes
$ this -> get ( ' index ' , ...);
. . .
});
$ this -> domain ( ' old.hereisyoursite.com ' , function () { // Legacy Domain routes
$ this -> get ( ' index ' , ...);
. . .
});