Skip to content

Uri helper cannot be "fluently modified" when starting with just the path #55818

@KSneijders

Description

@KSneijders

Laravel Version

12.14.1

PHP Version

8.4.7

Database Driver & Version

No response

Description

The Uri helper cannot be "fluently modified" (as shown in the docs) when starting with a relative url (path only)

This will return errors due to the fluent changes happening one at a time, all resulting in invalid states. (Although adding a Host to a Uri with just path should result in a valid state).

Steps To Reproduce

  1. Open Laravel Tinker (sail tinker / php artisan tinker)
  2. Run the following:
use Illuminate\Support\Uri;

$uri = Uri::of('relative/path/to/resource');

dump($uri);  /* Viewing contents of current URI */

/* 
 * Scheme first, Host second:
 *   League\Uri\Exceptions\SyntaxError: The uri `https:relative/path/to/resource` is invalid for the `https` scheme. 
 */
$uri->withScheme('https')->withHost('example.com')->withPath('/other/path/to/resource');

/* 
 * Alternatively, Host first, Scheme second:
 *   League\Uri\Exceptions\SyntaxError: If an authority is present the path must be empty or start with a `/`.
 */
$uri->withHost('example.com')->withScheme('https')->withPath('/other/path/to/resource');

Now if this is not supported that's fine, but it is very unintuitive as you can create (and work with other parts of) the created Uri when only supplying a relative URL.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions