Skip to content

[12.x] Introducing toUri to the Stringable Class #55862

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

Conversation

devajmeireles
Copy link
Contributor

@devajmeireles devajmeireles commented May 26, 2025

With the recent addition of the Illuminate\Support\Uri class, we can interact with Uri to manipulate or retrieve specific items from the Uri. However, we often have URLs inside of text sentences that can be extracted using the Stringable class.

Here is a real-world example:

Go to {https://euhosting.com/support} for support.

We can get the URL using Stringable, like this:

$url = str($sentence)->between('{', '}')->value(); // https://euhosting.com/support

However, there are cases where we are interacting with, for example, authenticated users (clients), where we can add extra parameters to the URL to direct the user to a page where the client can be identified by URL parameters, or better yet, define for example a query parameter, such as: priority=emergency due to some special condition of the client.

With this PR, we can achieve this result by combining Stringable and Uri by the toUri method:

$sentence = 'Go to {https://euhosting.com/support} for support.';

$uri = str($sentence)->between('{', '}')->toUri(); // or Str::of($sentence)->between('{', '}')->toUri();

if (auth()->user()->isVip()) {
    $uri = $uri->withQuery(['customer' => auth()->user()->publicId()])
        ->withQuery(['priority' => 'emergency']);
}

return $uri->value();

In the above example, the return of value() will be:

  • https://euhosting.com/support or https://euhosting.com/support?customer=cus_17DC&priority=emergency

According to the authenticated user's isVip status.

@taylorotwell taylorotwell merged commit 2d7c499 into laravel:12.x May 26, 2025
61 checks passed
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

Successfully merging this pull request may close these issues.

2 participants