maplephp/handler

MaplePHP框架的处理程序。

v1.2.1 2024-03-09 12:33 UTC

This package is auto-updated.

Last update: 2024-09-16 17:19:03 UTC


README

MaplePHP基于nikic/FastRoute构建。您可能会根据自己的偏好进行更改,但我真的不明白为什么要这样做,因为FastRoute非常出色,并且为高级用户使用正则表达式。您可以在 "app/Http/Routes/web.php" 中添加您的路由器。

用于MaplePHP框架。

结构

@routes->group([PATTERN], [CALLABLE], [MIDDELWARE]);
@routes->group([CALLABLE], [MIDDELWARE]);
@routes->group([CALLABLE]);

路由器

@routes->get([PATTERN], [ [CLASS], [METHOD] ]);

自解释示例

这是一个为高级用户自解释的示例

$routes->group("[POSSIBLE_PATTERNS]", function($routes) {
    
    // Router data - will inherit the group pattern and middlewares
    $routes->get("[PATTERN]", ['CLASS', "METHOD"]);

	// You can nest groups in infinity
    $routes->group(function($routes) {
    	// Router data - will inherit group and group parents patterns and middlewares
		$routes->get("[PATTERN]", ['CLASS', "METHOD"]);
		$routes->post("[PATTERN]", ['CLASS', "METHOD"]);

    }, [
	    [Http\Middlewares\LoggedIn::class, "privateZone"],
	]);

}, [
    [
    	Http\Middlewares\SessionStart::class,
		Http\Middlewares\DomManipulation::class
    ]
]);

工作原理

不同的路由器响应

// Catch a GET response
$routes->get("/{page:about}", ['Http\Controllers\Pages', "about"]);

// Catch a POST response
$routes->post("/{page:about}", ['Http\Controllers\Pages', "about"]);

// Catch a PUT response
$routes->put("/{page:about}", ['Http\Controllers\Pages', "about"]);

// Catch a DELETE response
$routes->delete("/{page:about}", ['Http\Controllers\Pages', "about"]);

// Catch a Custom response
$routes->map("CLI","/{page:about}", ['Http\Controllers\Pages', "about"]);

// Catch a Multiple responses
$routes->map(["GET", "POST"],"/{page:about}", ['Http\Controllers\Pages', "about"]);

// Take control over all the HTTP request errors (404, 403...). If removed, MaplePHP will generically try to handle the error responses. 
$routes->map("*", '[/{any:.*}]', ['Http\Controllers\HttpRequestError', "handleError"]);

// Group 
$routes->group("/{lang:en}", function($routes) {
	$routes->get("/{page:about}", ['Http\Controllers\Pages', "about"]);
    $routes->get("/{page:contact}", ['Http\Controllers\Pages', "contact"]);
});

您可以将路由器轻松地分组在模式之下。以下示例将显示页面/方法 about,如果您访问 example.com/en/about

// Group under language /en
$routes->group("/{lang:en}", function($routes) {
	$routes->get("/{page:about}", ['Http\Controllers\Pages', "about"]);
    $routes->get("/{page:contact}", ['Http\Controllers\Pages', "contact"]);
});

// Group under language all possible languages
$routes->group("/{lang:[^/]+}", function($routes) {
	$routes->get("/{page:about}", ['Http\Controllers\Pages', "about"]);
    $routes->get("/{page:contact}", ['Http\Controllers\Pages', "contact"]);
});

中间件

中间件是一种位于客户端和服务器之间的软件组件,充当中间人。中间件可以执行各种任务,例如身份验证、授权、缓存、日志记录、错误处理等。用更简单的话来说,中间件将快速轻松地扩展您在指定路由器上的应用程序功能。

$routes->group(function($routes) {
	$routes->get("/{form:login}", ['Http\Controllers\Users\Login', "form"]);

	// You can nest groups in infinity
	$routes->group(function($routes) {
        // Router data - will inherit group and group parents patterns and middlewares
        $routes->get("/{profile:profile}", ['Http\Controllers\Examples\PrivatePage', "profile"]);
    }, [
        [Http\Middlewares\LoggedIn::class, "privateZone"]
    ]);

}, [
    Http\Middlewares\SessionStart::class,
	Http\Middlewares\DomManipulation::class
]);

模式

您可以使用正则表达式(Regex)。有关更多信息,您也可以点击此处

在非常简单的应用程序中,您可以编写如下模式:

/param1
/param1/param2/param3

但在更复杂的应用程序中,了解一些更高级的模式是好的

找到所有、字符串和数字(计数为一个参数)

[^/]+

仅允许数字

\d+

找到所有动态参数

.+

如果匹配否则

(?:match|elseMatch)

强烈建议将一个KEY附加到模式。使用上面的示例,您可以编写更完整的路由,如下所示。

/{page:param1}
/{page:[^/]+}
OK: /about-us
NOT_FOUND: /about-us/environment
/{page:[^/]+}/{subpagePage:[^/]+}
OK: /about-us/environment
/{page:.+}
OK: /about-us/environment
/{page-id:\d+}
OK: /5242
NOT_FOUND: /about-us
/{product-id:\d+}/{slug:[^/]+}
OK: /5242/round-table
/{cat:.+}[/{pagination:pagin-\d+}]
OK: /cat1/cat2/.../cat5
OK: /cat1/cat2/.../cat5/pagin-2

完整示例

$routes->group(function($routes) {
    // Will handle all HTTP request errors 
	$routes->map("*", '[/{any:.*}]', ['Http\Controllers\HttpRequestError', "handleError"]);

    // Your routes
	$routes->get("/", ['Http\Controllers\Examples\Pages', "start"]);
	$routes->get("/{page:about}", ['Http\Controllers\Examples\Pages', "about"]);

    
    $routes->group(function($routes) {
        // Regular page with form
        $routes->get("/{form:login}", ['Http\Controllers\Users\Login', "form"]);
        // Open form in a modal with ajax call
        $routes->get("/{form:login}/{model:model}", ['Http\Controllers\Users\Login', "formModel"]);
        // Login request
        $routes->post("/{form:login}", ['Http\Controllers\Users\Login', "login"]);

    }, [
        [Http\Middlewares\LoggedIn::class, "publicZone"],
    ]);

    $routes->group(function($routes) {
        // Will handle all HTTP request errors 
        $routes->get("/{profile:profile}", ['Http\Controllers\Examples\PrivatePage', "profile"]);

    }, [
        [Http\Middlewares\LoggedIn::class, "privateZone"]
    ]);

}, [
    Http\Middlewares\SessionStart::class,
	Http\Middlewares\DomManipulation::class
]);