On 09/05/17 21:11, Rasmus Schultz wrote:

> I'd like to encourage anyone reading this to please post the biggest,
> most complex middleware stack they've ever used. Just the stack, not the
> implementations or individual component bootstrapping necessarily. Let's
> see what we're really dealing with?

This is a draft meta-code of the mw stack we use in out API (based on
Slim3):

```
$app
  ->add(Cors)
  ->group('/public', function() {
    $this->add(Format)
  })
  ->group('/sys', function() {
    $this
      ->add(Version)
      ->add(Format)
      ->add(SysAuth)
  })
  ->group('/v1', function() {
    $this
      ->add(Version)
      ->add(Format)
      ->add(V1Auth)

    // This is still in planning:
    $this
      ->get('/files/{name}')->add(BinaryFormat)
  })
;
```

A little explanation:

Cors checks the CORS headers before routing, and refuses the request
altogether if wrong, or adds headers to the response when needed.

Format checks the Accept header in the request before routing, and
transforms the output in JSON or XML in the Response after routing.

Version checks the middle and minor version in the Accept-Version in the
Request, and sets some Request parameters that some actions use to adapt
the response.

SysAuth authenticates automatic system via special tokens (for
health-like endpoints), and naturally returns 401 on unauthorized.

V1Auth autenticates "real" users of the APIs.

BinaryFormat is planned to override Format by checking if the file has
been requested in html or pdf, or in png or jpeg, or in other binary
formats in the future, and it's applied only on some specific routes.

This is very simple to achieve with the current `function($req, $res,
$next): $res` architecture. For example, this is (roughly) the Format mw:

```
$format = $this->parseAccept($req->getHeader('Accept'));

if (!in_array($format, ['json','xml'])) {
  return $res->withStatus(406);
}

$response = $next($req, $res);

return $this->format($response, $format);
```

I am a bit confused by all the proposed alternatives, to be honest, and
would love to understand how this would be implemented in both.

Thanks.
Bye.

-- 
You received this message because you are subscribed to the Google Groups "PHP 
Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to php-fig+unsubscr...@googlegroups.com.
To post to this group, send email to php-fig@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/php-fig/48fb0356-4690-963e-4276-fd2290764619%40amiran.it.
For more options, visit https://groups.google.com/d/optout.

Reply via email to