zendframework / zend-stratigility

Middleware for PHP built on top of PSR-7 and PSR-15
BSD 3-Clause "New" or "Revised" License
235 stars 57 forks source link

Path prefix stripped in Next #73

Closed acelaya closed 8 years ago

acelaya commented 8 years ago

In an Expressive app, I've been trying to do some logic based on the route path in a middleware where the path is set to a specific value ('/rest').

return [

    'middleware_pipeline' => [
        'pre-routing' => [
            'path' => '/rest',
            'middleware' => [
                Middleware\PathVersionMiddleware::class,
            ],
            'priority' => 11,
        ],
    ],

    // [...]

];

I've seen that Zend\Stratigility\Next strips the specified part from the beginning of the path for that middleware only, so if the dispatched route is '/rest/v1/foo/bar', doing $request->getUri()->getPath() inside that middleware results in /v1/foo/bar, while doing it on any other middleware where the path has not been set, results in /rest/v1/foo/bar

Is that the intended behavior? I find it a little bit counterintuitive.

geerteltink commented 8 years ago

It's for re-usability, read that last sentence:

Each middleware can itself be middleware, and can attach to specific paths, allowing you to mix and match applications under a common domain. As an example, you could put API middleware next to middleware that serves its documentation, next to middleware that serves files, and segregate each by URI:

$app->pipe('/api', $apiMiddleware);

The handlers in each middleware attached this way will see a URI with that path segment stripped, allowing them to be developed separately and re-used under any path you wish.

https://docs.zendframework.com/zend-stratigility/middleware/

acelaya commented 8 years ago

Ok, that makes sense :) I'm closing the issue then