loandbeholdru/slimcontrol

使用 SlimPHP 快速构建 REST API 的工具集

dev-master 2023-06-05 20:24 UTC

This package is auto-updated.

Last update: 2024-10-01 00:08:21 UTC


README

快速构建 Slimphp REST api 的工具集

安装

建议使用 Composer 安装 SlimControl。

$ composer require loandbeholdru/slimcontrol

待办事项 / 进行中

该包包含了我使用超过 5 年的许多功能,但这个 README 非常简陋。
文档正在进行中...

用法

快速构建控制器

class postreport extends controllerApi
{
    const IN = ["report", "key", "data"];
    const ASSERT = ["report", "key" => "keycheck"];
    const DEFAULT = ["report" => "setdef"]


    protected function process()
    {
        // You can do it because all opts are checked
        extract($this->args);

        return $this->replyjson(compact('report', 'key', 'data'));
    }
    // assert key
    protected function keycheck($parameter, $valifexist){
        return $valifexist === "notwrongkey";
    }
    // set default val of report if it's absent
    protected function setdef($parameter){
        return "default val of report";
    }
}

而不是使用常量,你可以覆盖 3 个方法

//...
    protected function defs()
    {
        return static::DEFAULTS;
    }

    protected function ins()
    {
        return static::IN;
    }

    protected function asss() 
    {
        return static::ASSERT;
    }
...

简单的请求响应

$this->replyjson($message, string $status = statuses::SUCCESS, int $code = 200)

返回 200 OK

{
  "message" : "...",
  "status" : "success"
}

预定义的状态码

class statuses
{
    const SUCCESS = 'success';
    const ERROR = 'error';
    const AUTH_FAIL = 'auth_fail';
    const DB_ERROR = 'db_error';
    const REQUEST_ERROR = 'request_error';
    const DISK_ERROR = 'disk_error';
}

一些有趣的中间件

... 文档将继续

贡献

... 总是受欢迎的。很多时候,指出作者未曾考虑过或遇到的使用案例非常有用。