|
1 | | -# Dispatcher |
| 1 | +# Documentation |
2 | 2 |
|
3 | 3 | ## Dispatcher Middleware |
4 | 4 |
|
| 5 | +The middleware inspects the requests using the extractor and if the result of the extraction was something else than `null`, it will pass the result on to the dispatcher. The dispatcher will try to dispatch it and implements the actual dispatching logic. |
| 6 | + |
| 7 | +```php |
| 8 | +new DispatcherMiddleware( |
| 9 | + new RequestAttributeExtractor(), |
| 10 | + new Dispatcher($psrContainer) |
| 11 | +); |
| 12 | +``` |
| 13 | + |
| 14 | +### Passing the handler through |
| 15 | + |
| 16 | +If you want to pass a route object or anything else from a middleware running before the dispatcher, you'll need to put the data into a request attribute. |
| 17 | + |
| 18 | +The RequestAttributeExtractor will check by default for a `handler` attribute in the request object. |
| 19 | + |
| 20 | +So for example in your routing middleware, if the router resolves to a route object, then pass it along: |
| 21 | + |
| 22 | +``` |
| 23 | +$request = $request->withAttribute('handler', $route); |
| 24 | +``` |
| 25 | + |
5 | 26 | ## Handler Extractors |
6 | 27 |
|
7 | 28 | They will extract a handler from the request object. |
8 | 29 |
|
9 | 30 | To implement your own extractor you'll have to implement the [HandlerExtractorInterface](../src/Infrastructure/Http/Dispatcher/HandlerExtractorInterface.php). |
10 | 31 |
|
11 | | -This library comes with a simple [RequestAttributeExtractor](../src/Infrastructure/Http/Dispatcher/RequestAttributeExtractor.php) that will check the request for an attribute, if it is not present NULL will be returned. |
| 32 | +This library comes with a simple [RequestAttributeExtractor](../src/Infrastructure/Http/Dispatcher/RequestAttributeExtractor.php) that will check the request for an attribute, if it is not present `null` will be returned. |
12 | 33 |
|
13 | 34 | ## Dispatchers |
| 35 | + |
| 36 | +Dispatchers take the handler and try to execute it depending what it is. The library comes with a dispatcher that will resolve `callable` and `string` handlers. The string must be of the format `<prefix>.<controller-or-handler>@<action>`. |
| 37 | + |
| 38 | +* `<prefix>` is optional but useful for plugins or extensions that reside in another namespace. |
| 39 | + * `<controller-or-handler>` is the actual class. |
| 40 | + * `<action>` is the method to call on the class. It is optional. |
| 41 | + |
| 42 | +You can change the separators via setter methods on the dispatcher object. |
| 43 | + If you want to implement your own dispatcher you'll have to implement the [DispatcherInterface](../src/Infrastructure/Http/Dispatcher/DispatcherInterface.php). |
| 44 | + |
| 45 | +The dispatcher that comes with the library uses a PSR container to resolve the actual controller or request handler class. |
| 46 | + |
| 47 | +```php |
| 48 | +new Dispatcher($psrContainer) |
| 49 | +``` |
0 commit comments