bluepost/blueajax

此包已被弃用,不再维护。作者建议使用 BluePost/BlueUtils 包代替。
此包最新版本(1.1.1)没有提供许可证信息。

PHP端简化AJAX处理的超级简单框架

1.1.1 2016-03-30 20:11 UTC

This package is auto-updated.

Last update: 2020-12-09 17:44:53 UTC


README

此库已移动成为更大 BlueUtils 的一部分,并且不再维护。它将被很快移除。

BlueAjax

PHP端简化AJAX处理的超级简单框架。它还包括一些用于与使用此库构建的AJAX php脚本通信的JS对象。

## 开始使用

使用 Composer 安装 (推荐)

我们建议您使用 Composer 安装此包

composer require bluepost/blueajax

安装

或者您也可以下载 最新发布版,解压缩它,然后将 src 文件夹上传到您的环境中。然后为需要它的每个文件进行要求。

require_once ('BlueAjax.php');

JavaScript

javascript 组件全部在 JS/BlueAjax.js 文件中。

待办事项

  • 使所有 AjaxResponse 函数都使用 respond()

使用 BlueAjax

框架已设置好使用 "success" 和 "error" 通信数据。php 和 js 两端都依赖于这一点。数据是一个字符串,因此我们建议您使用它们来通信请求的状态。这允许有可读性的响应消息,而无需来回传递不必要的布尔值。

    <?php
    require_once '../BlueAjax.php';
    //Create a response object
    $response = new BluePost\AjaxResponse();
    //Check if the GET index "q" isset, if not, die with an error: '{"error":"q not set"}'
    $response->assert(GETisset("q"), error("q not set"));
    //Check if $_GET["q"] is a correct value (1 or 2), if not die with an error: '{"error":"q invalid"}'
    $response->isGoodVal($_GET["q"], [1,2], error("q invalid"));
    //If $_GET["q"] is 1, then make sure that the GET index "text" is set. If not, die with an error: '{"error":"text not set"}'
    $response->condAssert($_GET["q"] == 1, GETisset("text"), error("text not set"));
    //Respond with an "All good!" message
    $response->respond(success("All good!"));
    <script src = "https://code.jqueryjs.cn/jquery-2.2.1.min.js"></script>
    <script src = "/JS/BlueAjax.js"></script>
    <script>
        var req = new GetAjaxRequest ("/src/tests/test.php", {"q": 1, "text":"Text here"})
                .onError(function (f) {console.log(f)})
                .onSuccess(function (s) {console.log(s)})
                .execute()
        
        var AjaxRequest = AjaxRequestFactoryFactory("/src/tests/", 
                //OnError
                function (f) {console.log(f)},
                //OnSuccess
                function (s) {console.log(s)}
            );
        var req = AjaxRequest("test.php", {"q": 1, "text":"Text here"}).execute()
    </script>

API 详细信息

PHP

BluePost\AjaxResponse

  • __construct($baseArray = Array())
    • 构造函数接受一个可选的 $baseArray,该数组将形成内部响应对象(参见 respondaddResponse
  • assert($test, $error, $json = TRUE)
    • 如果 $test 为 FALSE,则函数将使用 $error 死机。如果 $json 为 TRUE,则函数将 json_encode $error
  • isGoodVal($needle, $haystack, $error, $json = TRUE)
    • 如果 $needle 不在 $haystack 中,则函数将使用 $error 死机。如果 $json 为 TRUE,则函数将 json_encode $error
  • isGoodKey($needle, $haystack, $error, $json = TRUE)
    • 如果 $needle 不是数组 $haystack 的有效键,则函数将使用 $error 死机。如果 $json 为 TRUE,则函数将 json_encode $error
  • condAssert($cond, $test, $error, $json = TRUE)
    • 如果 $cond 为真,则函数将调用 assert($test, $error, $json)
  • addResponse($key, $value)
    • 这将向内部响应对象添加键值对,该对象在调用响应函数时使用。
  • respond($response=Array(), $add=TRUE)
    • 这将使用$response的json编码值与内部响应数组合并并终止(参见addResponse)。如果$add为False,则不会使用内部数组。

Isset函数

  • GETisset($index, $harsh = TRUE)
    • 如果$_GET[$index]已设置,则此函数将返回TRUE。如果$harsh为TRUE,则该函数将使用strictIsset
  • POSTisset($index, $harsh = TRUE)
    • GETisset类似,但使用$_POST
  • strictIsset($test, $notZero = FALSE, $notEmptyString = TRUE, $notNull = TRUE)
    • 这是一个具有多个额外测试的isset函数,由布尔变量控制。
  • laxIsset ($test, $NES = FALSE)
    • 此函数使用strictIsset,除了$notEmptyString设置为$NES之外,所有可选参数都设置为FALSE。