-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[11.x] Introduce Support\Url
and Support\UrlQueryParameters
classes for easier URL handling
#53370
base: 11.x
Are you sure you want to change the base?
Conversation
I think this would be a great addition! good job! |
Really great addition! I've used the |
Love this, superb utility. |
Wow amazing, you have inspired me to do some new work |
$url = url()->parse('https://example.com/path/to/resource?foo=bar&baz=qux'); I like this method 😉 |
Would it be out of the scope to add this functionality to the Otherwise it is macroable, those who do want it could just add it ourselves. Great work Steve!
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice !
What is the L11 standard practices for tests?
Would be nice if it also supported URI templates but that's probably a little out of scope |
Nice addition, thanks! I think it would be more powerful if $url->query()->get('filters'); // Or $url->query('filters')
$url->query()->has('filters');
$url->query()->count();
$url->query()->only('sort');
// ... Also // https://laravel.com/docs/11.x/collections
$url->path()->first(); // docs So we can have |
public function query(): UrlQueryParameters | ||
{ | ||
return UrlQueryParameters::parse($this->query); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function query(): UrlQueryParameters | |
{ | |
return UrlQueryParameters::parse($this->query); | |
public function query($item = null): UrlQueryParameters | |
{ | |
$this->parsedQuery ??= UrlQueryParameters::parse($this->query); | |
return $item ? $this->parsedQuery->get($item) : $this->parsedQuery; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review!
Not sure if we want to cache the parsed query here... Since the Url
properties are public, they can be changed at any time (the $query
prop) which wouldn't reflect the true state of the object if it's been cached already (I know this is edge case though).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that I would expect that I can pull out the query parameter object, make changes, and have it reflected on the URL.
I also agree that the public properties in their current form make that hard.
Perhaps the query
property should be a instance of the UrlQueryParameters
, rather than a string.
It would be nice to have documentation for this! |
Probably be best to wait until it gets merged before putting in the effort to document it. |
I've wanted a URL helper in Laravel for a while now. Glad to see this PR. I would love to see the ability to manipulate the URL and also a way for the I've always disliked the Laravel's At times I've needed to add AND remove query parameters from the current URL. I've love to see: $url = Request::toUrl()->withQuery([...])->withoutQuery([...]); In its current form, this helps does not seem to be able to manipulate with the URL in anyway. I always imagined the Laravel URL class would offer similar functionality to |
That would be awesome! 😍
Wouldn't that be $url = Request::toUrl()->withQueryParams([...])->withoutQueryParams([...]); as Going with Laravel's convention of
|
Other possible features:
What kind of validation do you want to have?
Helpers:
|
Love this wrapper around $url = Url::parse('https://example.com?foo=bar');
$foo = $url->query->foo; // bar |
Description
This PR introduces two new classes in the
Support
namespace to provide a more convenient way to work with URLs with an object-oriented representation using PHP's nativeparse_url
andparse_str
functions:Illuminate\Support\Url
Illuminate\Support\UrlQueryParameters
I copy these same classes over from project to project as I usually end up needing them. I figured others may find these classes a more (IDE) friendly replacement to the existing
parse_url
andparse_str
functions.Example
Let me know your thoughts! 👍 No hard feelings on closure ❤️