vonbraunlabs / slimx
Installs: 2 446
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 6
Forks: 0
Open Issues: 0
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-10-15 15:00:48 UTC
README
Provides a thin layer over PHP Slim Microframework, to provide eXtra features.
Status
Install
Install the package using composer:
composer require vonbraunlabs/slimx
SlimX\Controllers\Action
The OO representation of an Action, on the MVC context. Enables HTTP request
header routing. While loading the routes, an array of callables can be provided,
instead of just the callable. If an array is provided, the keys will be used to
determine the Accept
header:
$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 controllers may extend the AbstractController, provided by the SlimX. By extending the AbstractController, your controller must extend the loadActions method, that is responsible for loading all Action objects.
Use the pushEntrypoint
to register the Action objects into Slim. Moreover, the
constructior will assign \Slim\App
and the container to the attributes app
and container
respectively.
Here is a example of a controller class using SlimX's 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
When designing an API, there is a number of ways an endpoint can be misused or,
even if every parameter is OK, there is usually scenarios where an error must be
returned to the user. The Error
class helps maintaining consistency on error
messages. To use it, assign the error list, following the example bellow:
$error = new Error(); $error->setCodeList([ 1000 => [ 'status' => 404, 'message' => 'User info not found' ], 1001 => [ 'status' => 406, 'message' => 'You must specify API version' ], ]);
Afterwards, you can use the method handle
, providing the
ResponseInterface
and the error code:
return $error->handle($response, 1000);
It will fill the $response object with the right http status code and the JSON message that show the error code and message:
{"code": 1000, "message": "User info not found"}