Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Implement UrlHelper middleware #2

Conversation

weierophinney
Copy link
Member

As noted in a comment on a zend-expressive issue, the current approach to registering the UrlHelper is not feasible, due to the following issues:

  • In cases where the UrlHelper is pulled during creation of the TemplateRendererInterface instance, and a TemplatedErrorHandler (or derivative, such as the whoops error handler) is also used, a circular dependency occurs. This is due to the UrlHelperFactory also pulling the Application service (as the TemplatedErrorHandler instance is pulled inside the ApplicationFactory).
  • In cases where the UrlHelper creation is delayed until first use (e.g., in zend-expressive-zendviewrenderer's url helper factory), the helper is created after route result observers are updated, which means the helper never receives the route result.

This patch does the following:

  • Creates UrlHelperMiddleware, which composes a UrlHelper and a RouteResultSubjectInterface instance; when invoked, it attaches the helper to the subject as an observer.
  • Creates UrlHelperMiddlewareFactory, which creates a UrlHelperMiddleware instance, using the UrlHelper service, and either the RouteResultSubjectInterface or Application service instance.
  • Modifies the UrlHelperFactory to no longer pull the Application instance and attach the helper to it.

The plan is to update zend-expressive-skeleton to auto-register the UrlHelperMiddleware as pipeline middleware, just as we have done with the ServerUrlMiddleware.

Updates the `UrlHelperFactory` to no longer register the generated
`UrlHelper` instance with the `Application` service. This can lead to
circular-dependency issues when using a `TemplatedFinalHandler`;
additionally, if done too late can mean the helper is registered too
late to be notified.
The `UrlHelperMiddleware` simply attaches the composed `UrlHelper`
instance with the composed `RouteResultSubjectInterface` instance
(typically a `Zend\Expressive\Application` instance), and returns the
results of calling `$next()`.

This patch also includes a factory for retrieving an instance of the
middleware.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant