2017 © Pedro Peláez
 

library slim-api-wrapper

Slim Api Wrapper

image

anothy/slim-api-wrapper

Slim Api Wrapper

  • Thursday, August 17, 2017
  • by anothy
  • Repository
  • 1 Watchers
  • 0 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Slim Api Wrapper

Used as a way of accessing Slim App APIs internally. There are two types of accessing the APIs, directly where it skips the traversal of middleware(s), or using the full route where it traverses the middleware(s) attached to the Slim app., (*1)

Installation

Install with Composer., (*2)

$ composer require anothy/slim-api-wrapper

Usage

Setup container:, (*3)

$container['slim-api-wrapper'] = function (\Slim\Container $c) {
    return new \Anothy\SlimApiWrapper($c);
};

Calling a named-route using the call method.

The call method will skip the traversal of the middleware (in and out) and go straight to the Slim application., (*4)

$app = new \Slim\App();

$app->get('/books', function ($request, $response, $args) {
    // Show all books
})->setName('api-books');

$slimApiWrapper = $app->getContainer()->get('slim-api-wrapper');

$result = $slimApiWrapper->call('GET', 'api-books', [
   'queryParams' => [
       'page' => 1,
   ],
]);

The above example would look for the named-route api-books with GET method and with QueryString of page=1., (*5)

Calling a named-route using the callMiddlewareStack method.

The callMiddlewareStack method will traverse middleware(s) (in and out) added to the Slim application., (*6)

$result = $slimApiWrapper->callMiddlewareStack('GET', 'api-books', [
    'queryParams' => [
        'page' => 1,
    ],
]);

Adding route pattern placeholders

If your route has any pattern placeholders, you can add the namedArgs to the call., (*7)

$app->get('/books/{id:[0-9]+}', function ($request, $response, $args) {
    // Show all books by `id`
})->setName('api-books-by-id');

$result = $slimApiWrapper->call('GET', 'api-books-by-id', [
    'namedArgs' => [
        'id' => 1234,
    ],
]);

The above call is equivalent to doing a GET /books/1234., (*8)

Adding additional headers

Here is an example with the named-route api-books that require an Authorization header. A headers option is added with the HTTP_AUTHORIZATION key and value. The value is for an OAuth token., (*9)

$result = $slimApiWrapper->call('GET', 'api-books', [
    'headers' => [
        'HTTP_AUTHORIZATION' => 'Bearer ' . $token,
    ],
    'queryParams' => [
        'page' => 1,
    ],
]);

Adding a payload (body) to a request

$result = $slimApiWrapper->call('POST', 'api-books-post', [
    'headers' => [
        'HTTP_AUTHORIZATION' => 'Bearer ' . $token,
    ],
    'payload' => [
        'name'   => 'The Name of the Wind',
        'author' => 'Patrick Rothfuss',
    ],
]);

This adds payload to the request body as a JSON string., (*10)

The Versions

17/08 2017

dev-unit-testing

dev-unit-testing

Slim Api Wrapper

  Sources   Download

MIT

The Requires

 

The Development Requires

api framework slim

13/08 2017

dev-master

9999999-dev

Slim Api Wrapper

  Sources   Download

MIT

The Requires

 

The Development Requires

api framework slim

13/08 2017

0.1.4

0.1.4.0

Slim Api Wrapper

  Sources   Download

MIT

The Requires

 

The Development Requires

api framework slim

13/08 2017

0.1.2

0.1.2.0

Slim Api Wrapper

  Sources   Download

MIT

The Requires

 

The Development Requires

api framework slim

11/08 2017

0.1.1

0.1.1.0

Slim Api Wrapper

  Sources   Download

MIT

The Requires

 

The Development Requires

api framework slim

04/08 2017

0.1.0

0.1.0.0

Slim Api Wrapper

  Sources   Download

The Requires

 

The Development Requires