Merge pull request #7146 from robbieaverill/pulls/4.0/middleware-docs

DOCS Fix typos and formatting in middleware documentation
This commit is contained in:
Daniel Hensby 2017-07-19 15:20:45 +01:00 committed by GitHub
commit 79c74006fd

View File

@ -5,30 +5,31 @@ summary: Create objects for modifying request and response objects across contro
HTTP Middlewares allow you to put code that will run before or after. These might be used for HTTP Middlewares allow you to put code that will run before or after. These might be used for
authentication, logging, caching, request processing, and many other purposes. Note this interface authentication, logging, caching, request processing, and many other purposes. Note this interface
replaces the SilverStripe 3 interface, [RequestFilter](api:SilverStripe\Control\RequestFilter), which still works but is deprecated. replaces the SilverStripe 3 interface [RequestFilter](api:SilverStripe\Control\RequestFilter), which still works but is deprecated.
To create a middleware class, implement `SilverStripe\Control\HTTPMiddleware` and define the To create a middleware class, implement `SilverStripe\Control\Middleware\HTTPMiddleware` and define the
`process(HTTPRequest $request, callbale $delegate)` method. You can do anything you like in this `process(HTTPRequest $request, callable $delegate)` method. You can do anything you like in this
method, but to continue normal execution, you should call `$response = $delegate($request)` method, but to continue normal execution, you should call `$response = $delegate($request)`
at some point in this method. at some point in this method.
In addition, you should return an HTTPResponse object. In normal cases, this should be the In addition, you should return an `HTTPResponse` object. In normal cases, this should be the
$response object returned by `$delegate`, perhaps with some modification. However, sometimes you `$response` object returned by `$delegate`, perhaps with some modification. However, sometimes you
will deliberately return a different response, e.g. an error response or a redirection. will deliberately return a different response, e.g. an error response or a redirection.
**mysite/code/CustomMiddleware.php** **mysite/code/CustomMiddleware.php**
:::php ```php
<?php <?php
use SilverStripe\Control\Middleware\HTTPMiddleware use SilverStripe\Control\Middleware\HTTPMiddleware
use SilverStripe\Control\HTTPRequest;
class CustomMiddleware implements HTTPMiddleware {
class CustomMiddleware implements HTTPMiddleware
{
public $Secret = 'SECRET'; public $Secret = 'SECRET';
public function process(HTTPRequest $request, callable $delegate) { public function process(HTTPRequest $request, callable $delegate)
{
// You can break execution by not calling $delegate. // You can break execution by not calling $delegate.
if ($request->getHeader('X-Special-Header') !== $this->Secret) { if ($request->getHeader('X-Special-Header') !== $this->Secret) {
return new HTTPResponse('You missed the special header', 400); return new HTTPResponse('You missed the special header', 400);
@ -42,35 +43,37 @@ will deliberately return a different response, e.g. an error response or a redir
$response = $delegate($request); $response = $delegate($request);
// You can modify the response after it has been generated // You can modify the response after it has been generated
$response->addHeader('X-Middleware-Applied', 'CustomMiddleware') $response->addHeader('X-Middleware-Applied', 'CustomMiddleware');
// Don't forget to the return the response! // Don't forget to the return the response!
return $response; return $response;
} }
} }
```
Once you have created your middleware class, you must attach it to the Director config to make Once you have created your middleware class, you must attach it to the `Director` config to make
use of it. use of it.
## Global middleware ## Global middleware
By adding the service or class name to the Director::Middlewares property via injector, By adding the service or class name to the `Director.Middlewares` property via injector,
array, a middleware will be executed on every request: array, a middleware will be executed on every request:
**mysite/_config/app.yml** **mysite/_config/app.yml**
:::yml ```yaml
--- ---
Name: myrequestprocessors Name: myrequestprocessors
After: After:
- requestprocessors - requestprocessors
--- ---
SilverStripe\Core\Injector\Injector: SilverStripe\Core\Injector\Injector:
SilverStripe\Control\Director: SilverStripe\Control\Director:
properties: properties:
Middlewares: Middlewares:
CustomMiddleware: %$CustomMiddleware CustomMiddleware: %$CustomMiddleware
```
Because these are service names, you can configure properties into a custom service if you would Because these are service names, you can configure properties into a custom service if you would
@ -78,8 +81,8 @@ like:
**mysite/_config/app.yml** **mysite/_config/app.yml**
:::yml ```yaml
SilverStripe\Core\Injector\Injector: SilverStripe\Core\Injector\Injector:
SilverStripe\Control\Director: SilverStripe\Control\Director:
properties: properties:
Middlewares: Middlewares:
@ -88,6 +91,7 @@ like:
class: 'CustomMiddleware' class: 'CustomMiddleware'
properties: properties:
Secret: "DIFFERENT-ONE" Secret: "DIFFERENT-ONE"
```
## Route-specific middleware ## Route-specific middleware
@ -99,20 +103,21 @@ property. The controller which does the work should be registered under the
**mysite/_config/app.yml** **mysite/_config/app.yml**
:::yml ```yaml
SilverStripe\Core\Injector\Injector: SilverStripe\Core\Injector\Injector:
SpecialRouteMiddleware: SpecialRouteMiddleware:
class: SilverStripe\Control\Middleware\RequestHandlerMiddlewareAdapter class: SilverStripe\Control\Middleware\RequestHandlerMiddlewareAdapter
properties properties:
RequestHandler: %$MyController RequestHandler: %$MyController
Middlewares: Middlewares:
- %$CustomMiddleware - %$CustomMiddleware
- %$AnotherMiddleware - %$AnotherMiddleware
SilverStripe\Control\Director: SilverStripe\Control\Director:
rules: rules:
special\section: special\section:
Controller: %$SpecialRouteMiddleware Controller: %$SpecialRouteMiddleware
```
## API Documentation ## API Documentation
* [HTTPMiddleware](api:SilverStripe\Control\HTTPMiddleware) * [HTTPMiddleware](api:SilverStripe\Control\Middleware\HTTPMiddleware)