vonbraunlabs / slimx
此包最新版本(1.0.8)没有可用的许可证信息。
1.0.8
2021-03-15 07:24 UTC
Requires
- php: >=7.1
- gabordemooij/redbean: ^5.3
- slim/slim: ^3.0
Requires (Dev)
- guzzlehttp/guzzle: ^6.2
- phpstan/phpstan: ^0.9.1
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.2
- dev-master
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.1.32
- 0.1.31
- 0.1.30
- 0.1.29
- 0.1.28
- 0.1.27
- 0.1.26
- 0.1.25
- 0.1.24
- 0.1.23
- 0.1.22
- 0.1.21
- 0.1.20
- 0.1.19
- 0.1.18
- 0.1.17
- 0.1.16
- 0.1.15
- 0.1.14
- 0.1.13
- 0.1.12
- 0.1.11
- 0.1.10
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-add-license-1
This package is auto-updated.
Last update: 2024-09-15 14:45:41 UTC
README
提供在PHP Slim Microframework之上的一层薄封装,以提供额外功能。
状态
安装
使用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
和容器分配给属性app
和container
。
以下是一个使用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"}