zendframework / zend-expressive

PSR-15 middleware in minutes!
BSD 3-Clause "New" or "Revised" License
711 stars 197 forks source link

Allow to segregate middleware on host #235

Closed bakura10 closed 6 years ago

bakura10 commented 8 years ago

Hi,

Similar to how we can segregate by path:

[
                'path'       => '/api',
                'middleware' => [
                    ApiAuthenticationMiddleware::class,
                    AuthorizationMiddleware::class
                ]
            ]

Would it be possible to also segregate by host?

[
                'host'       => 'api',
                'middleware' => [
                    ApiAuthenticationMiddleware::class,
                    AuthorizationMiddleware::class
                ]
            ]
weierophinney commented 8 years ago

That's something you can do with nested middleware applications.

As an example, you can have pipeline middleware that checks against the host, and then dispatches another Expressive application based on the match:

$host = $request->getHeaderLine('Host');

switch (strtolower($host)) {
    case 'api':
        $api = $this->container->get('ApiApplication');
        return $api($request, response, $next);
    default:
        return $next($request, $response);
}

Just a quick point of clarification: the path-based matching in pipeline middleware is a feature of Stratigility, which inherits the feature from Sencha Connect. Its primary purpose is path-based segregation; if you want to do host-based segregation, you either run the application on a separate host, or you use a solution like I outline above.

bakura10 commented 8 years ago

Sounds like a nice solution. But maybe Stratigility implementation could deviate a bit to add this feature? That sounds like a pretty simple feature, as today in Expressive there is basically no easy way through config to perform routing based on host.

weierophinney commented 8 years ago

@bakura10 I don't want to bloat Stratigility too much, but that should be relatively easy to add. Want to contribute a PR?

bakura10 commented 8 years ago

Yep will try to do that when I have a bit of time :).

artwis commented 8 years ago

Hey, has anyone worked on this feature? If not I'd be willing to submit PRs to Stratigility and Expressive to allow host based middleware pipe routing. @weierophinney

michalbundyra commented 6 years ago

It's possible now with Expressive 3. See: