v1.5.0 2021-05-02 11:33 UTC

This package is auto-updated.

Last update: 2024-08-29 04:59:59 UTC


README

WaterPipe Logo

WaterPipe

downloads downloads downloads downloads

一个强大的PHP路由框架和请求/响应处理器

WaterPipe是一个库,允许您轻松地使用PHP处理HTTP请求和响应,为您提供构建完整RESTful API、创建Web应用程序的路由框架等全部功能。

示例

<?php

use ElementaryFramework\WaterPipe\WaterPipe;

use ElementaryFramework\WaterPipe\HTTP\Request\Request;

use ElementaryFramework\WaterPipe\HTTP\Response\Response;
use ElementaryFramework\WaterPipe\HTTP\Response\ResponseStatus;
use ElementaryFramework\WaterPipe\HTTP\Response\ResponseHeader;

// Create the root pipe
$root = new WaterPipe;

// Add a new route to the pipe with HTTP GET method (the home page)
$root->get("/", function (Request $req, Response $res) {
    $res->sendHtml("<b>Welcome to my web app !</b> <a href=\"/login\">Click here to login</a>");
});

// Add a new route to the pipe with HTTP GET method (the login page)
$root->get("/login", function (Request $req, Response $res) {
    $res->sendFile("./pages/login.html", ResponseStatus::OkCode);
});

// Add a new route to the pipe with HTTP POST method (the login page form validation)
$root->post("/login", function (Request $req, Response $res) {
    // Get $_POST values
    $body = $req->getBody();
    $username = $body["username"];
    $password = $body["password"];

    if (validate_username($username) && validate_password($password)) {
        // Checks if the client access this route with an AJAX request
        if ($req->isAjax()) {
            $res->sendJson(array(
                "success" => true
            ));
        } else {
            // Redirect the user to the members page
            $res->redirect("/members/{$username}");
        }
    } else {
        // Checks if the client access this route with an AJAX request
        if ($req->isAjax()) {
            $res->sendJson(array(
                "success" => false
            ));
        } else {
            // Redirect the user to the members page
            $res->redirect("/login");
        }
    }
});

// Add a new route to the pipe with HTTP GET method (the member's dashboard page)
$root->get("/members/:username", function (Request $req, Response $res) {
    $res->sendHtml("Welcome to your dashboard <b>{$req->uri['username']}</b> !");
});

// Add a new HTTP error handler (the 404 Not Found Error)
$root->error(ResponseStatus::NotFoundCode, function (Request $req, Response $res) {
    $res->sendText("404 Error: Not Found.", ResponseStatus::NotFoundCode);
});

// Finally... Run the pipe
$root->run();

特性

  • 高度设计,快速创建MVC应用程序和REST服务的路由;
  • 面向对象的HTTP请求响应管理;
  • 完全支持HTTP方法:GET、POST、PUT、DELETE、HEAD、PATCH和OPTIONS;
  • 轻松处理常见的HTTP错误(404、500);
  • 设计用于与React.js、AngularJS、Vue.js等前端框架配合工作,支持AJAX

安装

您可以使用composerWaterPipe安装到您的项目中

composer require elementaryframework/water-pipe

安装后,您可以通过ElementaryFramework\WaterPipe命名空间访问WaterPipe api。

如何使用?

刚接触WaterPipe?通过浏览我们的wiki了解如何构建路由框架和REST服务。

其他资源和教程

捐赠

喜欢Elementary Framework?通过小额捐赠帮助我们继续维护它,并提供更好的支持和令人惊叹的功能!

Donate PayPal Button

许可

© 版权所有 2018-2020 Aliens Group.

许可协议 MIT (阅读许可)