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

Stripping matched path in middleware pipeline #82

Closed michalbundyra closed 7 years ago

michalbundyra commented 7 years ago

Middleware pipped to a path stripped in the original request matched path. So if we attach middleware to path /api: $app->pipe('/api', Middleware::class); and we call /api/foo our $request in Middleware will contain path /foo not /api/foo.

Still, it is possible to get original request uri: $originalUri = $request->getAttribute('originalUri');

but it seems to be odd for me that original request is modified. There is short explanation in the documentation:

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/

I've discussed this issue with @weierophinney on IRC and it's expected behaviour.

It is expected behavior. The idea behind using a path when piping is to allow segregating applications. It's not intended for routing. So, the idea is that you can develop an application in isolation, and do the routing as if it were the application root. You can then nest it into another application under a sub-path, and your routing and whatnot continue to work. As an example, I might develop an API in isolation from the rest of the application, but then compose it into the application under the /api sub-path. I can do that without worrying about whether or not my routing now breaks because of the addition of the sub-path.

It would be nice to update documentation with a bit more explanations, because IMHO changing original request is not something what you can expect.

weierophinney commented 7 years ago

I'm adding documentation now, but want to note that doc/book/middleware.md clearly states the following:

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.