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

Implemented base path support in the UrlHelper #6

Merged

Conversation

weierophinney
Copy link
Member

This feature will allow prefixing the generated path with a detected and/or calculated base path, ensuring:

  • Routes do not need to accommodate the prefix.
  • All generated URIs will have the prefix.

This feature will allow prefixing the generated path with a detected
and/or calculated base path, ensuring:

- Routes do not need to accommodate the prefix.
- All generated URIs will have the prefix.
@weierophinney
Copy link
Member Author

Ping @RalfEggert — the need to prefix all routes with the detected locale has been bothering me, and I thought this might be a nice solution. The one drawback is that we will likely want to add base path support to the zend-view UrlHelper as well, which would proxy to this.

@mtymek
Copy link

mtymek commented Dec 24, 2015

Looks like I could use that for my base URL middleware as well. Nice!

@RalfEggert
Copy link

@weierophinney

Looks fine. I just wonder how to add the base bath within an action middleware that needs to redirect and return a Zend\Diactoros\Response\RedirectResponse. Currently I do it like this:

            return new RedirectResponse(
                $this->router->generateUri('pizza-show', $routeParams)
            );

To access the language I can only access the Locale::getDefault() or Locale::getPrimaryLanguage(). Or are there other ways to access the language for building URLs within a Middleware?

@RalfEggert
Copy link

Well, I can answer the question myself. Your suggested LocaleMiddleware could also set a request attribute and I could access that within my middleware.

Should I I update this recipe? I could remove the LocalizedUrlHelper and just use the normal url helper to set the base path. Or would you prefer another one with your suggestion?

https://github.com/zendframework/zend-expressive/pull/244/files

@weierophinney
Copy link
Member Author

@RalfEggert — no request attribute is necessary. In fact, what you can do is compose the UrlHelper and/or ServerUrlHelper into your middleware for the purpose of generating the URI for the redirect. Your example from above then becomes:

return new RedirectResponse(
    $this->helper->generate('pizza-show', $routeParams)
);

and, with the change this patch introduces, assuming the locale has been injected as the base path previously, it will be included in the generated URI.

As to the question of updating the localization recipe you've written: yes, once I've merged this patch and tagged a new release, let's do that.

@weierophinney weierophinney merged commit 44eaf53 into zendframework:develop Jan 1, 2016
weierophinney added a commit that referenced this pull request Jan 1, 2016
weierophinney added a commit that referenced this pull request Jan 1, 2016
@weierophinney weierophinney deleted the feature/base-path branch January 1, 2016 16:09
weierophinney added a commit to weierophinney/zend-expressive that referenced this pull request Jan 1, 2016
@RalfEggert
Copy link

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.

3 participants