master

laravel/framework

Last updated at: 29/12/2023 09:24

RouteUri.php

TLDR

The RouteUri class in the Illuminate\Routing namespace represents the route URI. It has a constructor that initializes the URI and binding fields. It also has a parse method to parse the given URI.

Classes

RouteUri

The RouteUri class represents the route URI. It has the following properties:

  • uri: The route URI.
  • bindingFields: The fields that should be used when resolving bindings.

The RouteUri class has the following methods:

  • __construct(string $uri, array $bindingFields = []): Constructs a new route URI instance with the given URI and binding fields.
  • parse(string $uri): Parses the given URI and returns a new RouteUri instance.
<?php

namespace Illuminate\Routing;

class RouteUri
{
    /**
     * The route URI.
     *
     * @var string
     */
    public $uri;

    /**
     * The fields that should be used when resolving bindings.
     *
     * @var array
     */
    public $bindingFields = [];

    /**
     * Create a new route URI instance.
     *
     * @param  string  $uri
     * @param  array  $bindingFields
     * @return void
     */
    public function __construct(string $uri, array $bindingFields = [])
    {
        $this->uri = $uri;
        $this->bindingFields = $bindingFields;
    }

    /**
     * Parse the given URI.
     *
     * @param  string  $uri
     * @return static
     */
    public static function parse($uri)
    {
        preg_match_all('/\{([\w\:]+?)\??\}/', $uri, $matches);

        $bindingFields = [];

        foreach ($matches[0] as $match) {
            if (! str_contains($match, ':')) {
                continue;
            }

            $segments = explode(':', trim($match, '{}?'));

            $bindingFields[$segments[0]] = $segments[1];

            $uri = str_contains($match, '?')
                ? str_replace($match, '{'.$segments[0].'?}', $uri)
                : str_replace($match, '{'.$segments[0].'}', $uri);
        }

        return new static($uri, $bindingFields);
    }
}