Skip to content

Commit

Permalink
Updating README with documentation about autowiring and PSR-11 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Scheffler committed Mar 26, 2020
1 parent b967b27 commit 59b9919
Showing 1 changed file with 53 additions and 24 deletions.
77 changes: 53 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,15 @@ Limber is intended for advanced users who are comfortable setting up their own f

## Limber includes
* A router
* PSR-15 middleware support
* PSR-11 container support
* A thin `Application` layer to:
* Attach router
* Add global PSR-15 middleware
* Add middleware chain exception handler
* Dispatch PSR-7 ServerRequestInterface instances
* Send PSR-7 ResponseInterface instances
* Add a PSR-11 container instance
* PSR-7 HTTP message compliant
* PSR-11 container compliant
* PSR-15 middleware compliant
* A thin `Application` layer to tie everything together

## Installation

```bash
composer require nimbly/Limber
composer require nimbly/limber
```

## Quick start
Expand Down Expand Up @@ -59,9 +54,16 @@ $application->send($response);
Limber *does not ship* with a PSR-7 HTTP Message implementation so you will need to bring your own. Here are some options:

* [slim/psr7](https://github.com/slimphp/Slim-Psr7)
* [nimbly/Capsule](https://github.com/nimbly/Capsule)
* [laminas/laminas-diactoros](https://github.com/laminas/laminas-diactoros)
* [guzzlehttp/psr7](https://github.com/guzzle/psr7)
* [nimbly/Capsule](https://github.com/nimbly/Capsule)

## PSR-11

Limber *does not ship* with a PSR-11 Container implementation, so you will need to bring your own if you need one. Here are some options:

* [PHP-DI](http://php-di.org/)
* [nimbly/Carton](https://github.com/nimbly/Carton)

## Router

Expand Down Expand Up @@ -91,21 +93,20 @@ By default, Limber will add a `HEAD` method to each `GET` route.

### Route paths

Paths can be static or contain place holders.
Paths can be static or contain named parameters. Named parameters will be injected into your
route handler if the handler also contains a parameter of the same name.

```php
// This route is static
$router->get('/books/new', 'BooksController@getNewBooks');

// This route needs an ISBN in the path.
// This route defines a named parameter "isbn"
$router->get('/books/{isbn}', 'BooksController@getByIsbn');
```

### Route path patterns

Your path place holders can also enforce a specific regular expression pattern when being matched.

Just add the pattern after the placeholder name with a colon.
Your named parameters can also enforce a specific regular expression pattern when being matched - just add the pattern after the placeholder name with a colon.

Limber has several predefined path patterns you can use:

Expand All @@ -129,34 +130,57 @@ $router->get('/books/{id:isbn}', 'BooksController@getByIsbn');

### Route actions

Route actions may either be a `\callable` or a string in the format **Fully\Qualified\Namespace\ClassName@Method**.

When a `ServerRequestInterface` instance is dispatched to the `Route` action, Limber will always pass the `ServerRequestInterface` instance in as the *first* parameter. Any path parameters defined in the route will be passed into the action as well, in the order they appear in the Route URI pattern.
Route actions (or handlers) may either be a `\callable` or a string in the format **Fully\Qualified\Namespace\ClassName@Method** (for example `App\Handlers\v1\BookHandler@create`).

Route actions *must* return a `ResponseInterface` instance.

Limber uses reflection based autowiring to automatically resolve your route action's parameters - including the `ServerRequestInterface` instance
and any path parameters. This applies for both closure based handlers as well as **Class@Method** based handlers.

You may also optionally supply a PSR-11 compliant `ContainerInterface` instance to aid in route handler resolution. By doing this, you can
easily have your application specific dependencies resolved and injected into your handlers by Limber. See **PSR-11 Container support** section
for more information.

```php
// Closure based actions
$router->get("/books", function(ServerRequestInterface $request): ResponseInterface {
$router->get("/books/{id:isbn}", function(ServerRequestInterface $request, string $id): ResponseInterface {

$book = Books::find($id);

if( empty($book) ){
throw new NotFoundHttpException("Book not found.");
}

return new Response(
\json_encode(Books::all())
\json_encode($book)
);

});

// String references to ClassName@Method
$router->patch("/books/{id:isbn}", "App\Controllers\BooksController@update");

// If a ContainerInterface instance was assigned to the application and contains an InventoryService instance, it will be injected into this handler.
$router->post("/books", function(ServerRequestInterface $request, InventoryService $inventoryService): ResponseInterface {

$book = Book::make($request->getAll());

$inventoryService->add($book);

return new Response(
\json_encode($book)
);
});
```

### Route groups

You can group routes together using the `group` method and passing in parameters that you want applied to all routes within that group.
You can group routes together using the `group` method and passing in array of configurations that you want applied to all routes within that group.

* `scheme` *string* The HTTP scheme (http or https) to match against.
* `middleware` *array<string>* or *array<MiddlewareInterface>* or *array<callable>* An array of all middleware classes (fullname space) or actual instances of middleware.
* `prefix` <string> A string prepended to all URIs when matching the request.
* `namespace` <string> A string prepended to all string based actions before instantiating a new controller.
* `namespace` <string> A string prepended to all string based actions before instantiating a new class.
* `hostname` <string> A host name to be matched against.

```php
Expand Down Expand Up @@ -353,13 +377,18 @@ handlers to inject your application specific dependencies where needed.

```php
$container = new Psr11\Library\Container;
$container->register(
[
// register your service providers
]
);

$application->setContainer($container);
```

### Sending the Response

To send a PSR-7 `ResponseInterface` instance, call the `send` method.
To send a PSR-7 `ResponseInterface` instance, call the `send` method with the `ResponseInteface` instance.

```php
$application->send($response);
Expand Down

0 comments on commit 59b9919

Please sign in to comment.