此包最新版本(1.0.8)没有可用的许可证信息。

1.0.8 2021-03-15 07:24 UTC

README

提供在PHP Slim Microframework之上的一层薄封装,以提供额外功能。

状态

Build Status

Code Climate

Scrutinizer Code Quality

安装

使用composer安装此包

composer require vonbraunlabs/slimx

SlimX\Controllers\Action

在MVC上下文中,Action的对象表示。启用HTTP请求头路由。在加载路由时,可以提供一个可调用的数组,而不仅仅是可调用本身。如果提供了一个数组,则将使用键来确定Accept

$entrypoints['testGet'] = new Action(
    'GET',
    '/test,
    [
        'application/vnd.vbl.slimx.v1+json' => function ($request, $response, $args) {
            $response->write("{'api-version': 'v1'}");

            return $response;
        },
        'application/vnd.vbl.slimx.v2+json' => function ($request, $response, $args) {
            $response->write("{'api-version': 'v2'}");

            return $response;
        }
    ]
);

SlimX\Controllers\AbstractController

Slim控制器可以扩展由SlimX提供的AbstractController。通过扩展AbstractController,您的控制器必须扩展loadActions方法,该方法负责加载所有Action对象。

使用pushEntrypoint将Action对象注册到Slim中。此外,构造函数将分别将\Slim\App和容器分配给属性appcontainer

以下是一个使用SlimX的AbstractController的控制器类示例

<?php

namespace My\Controllers;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use SlimX\Controllers\AbstractController;
use SlimX\Controllers\Action;

class DefaultController extends AbstractController
{
    /**
     * Implements the abstract method loadAction defined on AbstractController.
     * This method is called when it is time to load the actions into the
     * \Slim\App object.
     */
    public function loadActions()
    {
        $this->pushEntrypoint(new Action(
            'GET',
            '/publickey',
            ['application/vnd.my.project.v1+json' => [$this, 'getBooksAction']],
            [$this, 'handleApiVersionNotSpecified']
        ));
    }

    /**
     * Returns a list of books. In this example method an empty array.
     *
     * @param RequestInterface $request The HTTP request
     * @param ResponseInterface $response The generated HTTP response
     * @param array $args Optional arguments
     *
     * @return ResponseInterface Response processed by the Action
     */
    public function getBooksAction(
        RequestInterface $request,
        ResponseInterface $response,
        array $args
    ) {
        $response->write(json_encode([]));
        return $response;
    }

    /**
     * To be used as a callback by SlimX, handling requests that doesn't
     * provide a valid API version.
     *
     * @param $response ResponseInterface object
     * @return Returns the response, with status code and body set.
     */
    public function handleApiVersionNotSpecified(ResponseInterface $response)
    {
        return $this->container->get('error')->handle($response, 1000);
    }
}

SlimX\Models\Error

在设计API时,端点可能会被误用或,即使每个参数都正常,通常也有需要返回错误给用户的场景。Error类有助于保持错误消息的一致性。要使用它,按照以下示例分配错误列表

$error = new Error();
$error->setCodeList([
    1000 => [
        'status' => 404,
        'message' => 'User info not found'
    ],
    1001 => [
        'status' => 406,
        'message' => 'You must specify API version'
    ],
]);

之后,您可以使用handle方法,提供ResponseInterface和错误代码

return $error->handle($response, 1000);

它将为$response对象填充正确的HTTP状态码以及显示错误代码和消息的JSON消息

{"code": 1000, "message": "User info not found"}