8000 [DX] URL value object · Issue #16250 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[DX] URL value object #16250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maryo opened this issue Oct 15, 2015 · 6 comments
Closed

[DX] URL value object #16250

maryo opened this issue Oct 15, 2015 · 6 comments

Comments

@maryo
Copy link
Contributor
maryo commented Oct 15, 2015

Sometimes you need to parse and modify URL yourself. There is parse_url function in PHP. But it returns all components as array. Rebuilding the URL from the array is a little bit cumbersome because you need lots of checks like here http://stackoverflow.com/questions/4354904/php-parse-url-reverse-parsed-url#answer-31691249

And there is also some hardcoded logic in UrlGenerator like this
https://github.com/symfony/Routing/blob/master/Generator/UrlGenerator.php#L58

So it's hard to build it the same way UrlGenerator does it. OFC one can use the generator for it but it's not usable for URLs which are not routed.

I think it would be nice to move this logic to a new value object so one can use it for creating/modifying URLs. Like this
https://github.com/nette/http/blob/master/src/Http/Url.php
Not only UrlGenerator could use it. For example RedirectResponse is another candidate which could return it instead of a string so one can just for example safely append query parameter to it. I was solving this issue in my custom authentication failure handler which overrides the default symfony one and appends query parameter to the RedirectResponse.

What do you think?

EDIT: Or if it was too big change then at least moving the hardcoded logic from urlgenerator would also be useful. Maybe even just an utility class...

@linaori
Copy link
Contributor
linaori commented Oct 15, 2015

I actually know one project in my company where we have a similar solution for this problem. I think it would be nice to have a more usable Url object.

@Tobion
Copy link
Contributor
Tobion commented Oct 16, 2015

I think this is just part of making components compatible with PSR-7. There is a UriInterface as part of PSR-7.

Not only UrlGenerator could use it. For example RedirectResponse is another candidate which could return it instead of a string

So I think what you mean is that

  1. UrlGenerator returns a UriInterface instead of a string.
  2. RedirectResponse could also accept a UriInterface instead of a string as argument.

For 2) it makes sense IMO. For 1) I don't see the real benefit. I would require the Routing component to use a concrete UriInterface implementation. It is a BC break and it doesn't bring much value as people who want to parse the returned url, can just wrap it themselves in a UriInterface implementation.

And there is also some hardcoded logic in UrlGenerator like this
https://github.com/symfony/Routing/blob/master/Generator/UrlGenerator.php#L58

This is an implementation detail of the UrlGenerator. And it woulnd't be exposed when we switch to an Uri object as well. So that argument is not relevant.

@Tobion
Copy link
Contributor
Tobion commented Oct 16, 2015

@iltar what hinders you to use a PSR-7 implementation? There is no need to reimplement this in symfony at this point.

@linaori
Copy link
Contributor
linaori commented Oct 16, 2015

I want to stay as close to Symfony as possible when it comes to this kind of code or where possible a generic vendor package. If there's a chance it might be in Symfony, I won't create my own code/package for it.

@Tobion
Copy link
Contributor
Tobion commented Oct 16, 2015

I'm closing this as duplicate of #15414 and #15212

@Tobion Tobion closed this as completed Oct 16, 2015
@maryo
Copy link
Contributor Author
maryo commented Oct 16, 2015

@Tobion

  1. You're probably right it doesn't worth the BC break troubles (not sure if keeping BC "forever" is a good approach though, it could be better to somehow develop a separate BC package for legacy projects). But i think it would still be better even if it used a concrete implementation. The returned values could be simply casted to string to retain BC. I don't agree with your last statement about the implementation detail. It is an implementation detail but it should probably be an implementation detail of the URI itself. If it was implemented this way then one could reuse it for every URI, not only for routed ones without copy-pasting the same logic. Or is there a reason for treating the routed URI encoding differently? If so then it could be a different implementation of the UriInterface.

This is an example of a similar logic
https://github.com/guzzle/psr7/blob/master/src/Uri.php

  1. One can actually wrap it everywhere, even in the case of RedirectResponse. But right. In this case it would not be a BC break.

Sorry I missed the UriInterface issue. I was searching for wrong keywords.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
0