albertoadolfo27 / friendly_route
PHP库,用于创建HTTP路由
1.1.0
2024-08-08 13:10 UTC
Requires
- php: >=8.0
README
PHP库,用于创建HTTP路由
入门
最低要求
- PHP >= 8.0
安装
使用composer安装
composer require albertoadolfo27/friendly_route
快速示例
追踪路由
<?php // Require the Composer autoloader. require_once "vendor/autoload.php"; use FriendlyRoute\Router; // Set the configuration. $config = array( "debug" => true, "projectDir" => "friendly_route" // Set the Project Directory if you are working on the localhost. Default empty string. ); // Instantiate a Router. $router = new Router($config); // Tracing routes $router->get("/", function () { echo "<h1>Hello World!</h1>"; }); $router->get("/user/{username}", function ($args) { $user = $args["username"]; echo "<h1>Welcome {$user}</h1>"; }); $router->notFound(function () { echo "<h1>404 NOT FOUND</h1>"; }); $router->run();
允许的HTTP方法
- GET
$router->get("/", function () { // Put the code to run });
或
$router->set("GET", "/", function () { // Put the code to run });
- POST
$router->post("/", function () { // Put the code to run });
或
$router->set("POST", "/", function () { // Put the code to run });
- PUT
$router->put("/", function () { // Put the code to run });
或
$router->set("PUT", "/", function () { // Put the code to run });
- DELETE
$router->delete("/", function () { // Put the code to run });
或
$router->set("DELETE", "/", function () { // Put the code to run });
绘制具有多个HTTP方法的路由
$router->set(array("GET", "POST"), "/", function () { // Put the code to run });
将路由数组添加到请求中
$router->get(["/hello","/hi"], function () { // Put the code to run });
方法不允许
$router->methodNotAllowed(function () { echo "<h1>405 METHOD NOT ALLOWED</h1>"; });