0 follower

Final Class Yiisoft\Router\Route

InheritanceYiisoft\Router\Route
ImplementsStringable

Route defines a mapping from URL to callback / name and vice versa.

Public Methods

Hide inherited methods

Method Description Defined By
__debugInfo() Yiisoft\Router\Route
__toString() Yiisoft\Router\Route
action() Appends action handler. It is a primary middleware definition that should be invoked last for a matched route. Yiisoft\Router\Route
defaults() Parameter default values indexed by parameter names. Yiisoft\Router\Route
delete() Yiisoft\Router\Route
disableMiddleware() Excludes middleware from being invoked when action is handled. Yiisoft\Router\Route
get() Yiisoft\Router\Route
getData() Yiisoft\Router\Route
head() Yiisoft\Router\Route
host() Yiisoft\Router\Route
hosts() Yiisoft\Router\Route
methods() Yiisoft\Router\Route
middleware() Appends a handler middleware definition that should be invoked for a matched route. Yiisoft\Router\Route
name() Yiisoft\Router\Route
options() Yiisoft\Router\Route
override() Marks route as override. When added it will replace existing route with the same name. Yiisoft\Router\Route
patch() Yiisoft\Router\Route
pattern() Yiisoft\Router\Route
post() Yiisoft\Router\Route
prependMiddleware() Prepends a handler middleware definition that should be invoked for a matched route. Last added handlers will be executed first. Yiisoft\Router\Route
put() Yiisoft\Router\Route

Method Details

Hide inherited methods

__debugInfo() public method

public __debugInfo( ): mixed

                public function __debugInfo()
{
    return [
        'name' => $this->name,
        'methods' => $this->methods,
        'pattern' => $this->pattern,
        'hosts' => $this->hosts,
        'defaults' => $this->defaults,
        'override' => $this->override,
        'actionAdded' => $this->actionAdded,
        'middlewares' => $this->middlewares,
        'disabledMiddlewares' => $this->disabledMiddlewares,
        'enabledMiddlewares' => $this->getEnabledMiddlewares(),
    ];
}

            
__toString() public method

public __toString( ): string

                public function __toString(): string
{
    $result = $this->name === null
        ? ''
        : '[' . $this->name . '] ';
    if ($this->methods !== []) {
        $result .= implode(',', $this->methods) . ' ';
    }
    if ($this->hosts) {
        $quoted = array_map(static fn($host) => preg_quote($host, '/'), $this->hosts);
        if (!preg_match('/' . implode('|', $quoted) . '/', $this->pattern)) {
            $result .= implode('|', $this->hosts);
        }
    }
    $result .= $this->pattern;
    return $result;
}

            
action() public method

Appends action handler. It is a primary middleware definition that should be invoked last for a matched route.

public action( array|callable|string $middlewareDefinition ): self
$middlewareDefinition array|callable|string

                public function action(array|callable|string $middlewareDefinition): self
{
    $route = clone $this;
    $route->middlewares[] = $middlewareDefinition;
    $route->actionAdded = true;
    return $route;
}

            
defaults() public method

Parameter default values indexed by parameter names.

public defaults( array $defaults ): self
$defaults array

                public function defaults(array $defaults): self
{
    $route = clone $this;
    $route->defaults = array_map(\strval(...), $defaults);
    return $route;
}

            
delete() public static method

public static delete( string $pattern ): self
$pattern string

                public static function delete(string $pattern): self
{
    return self::methods([Method::DELETE], $pattern);
}

            
disableMiddleware() public method

Excludes middleware from being invoked when action is handled.

It is useful to avoid invoking one of the parent group middleware for a certain route.

public disableMiddleware( mixed $definition ): self
$definition mixed

                public function disableMiddleware(mixed ...$definition): self
{
    $route = clone $this;
    array_push(
        $route->disabledMiddlewares,
        ...array_values($definition),
    );
    $route->enabledMiddlewaresCache = null;
    return $route;
}

            
get() public static method

public static get( string $pattern ): self
$pattern string

                public static function get(string $pattern): self
{
    return self::methods([Method::GET], $pattern);
}

            
getData() public method

public getData( string $key ): mixed
$key string

                public function getData(string $key): mixed
{
    return match ($key) {
        'name' => $this->name
            ?? (implode(', ', $this->methods) . ' ' . implode('|', $this->hosts) . $this->pattern),
        'pattern' => $this->pattern,
        'host' => $this->hosts[0] ?? null,
        'hosts' => $this->hosts,
        'methods' => $this->methods,
        'defaults' => $this->defaults,
        'override' => $this->override,
        'hasMiddlewares' => $this->middlewares !== [],
        'enabledMiddlewares' => $this->getEnabledMiddlewares(),
        default => throw new InvalidArgumentException('Unknown data key: ' . $key),
    };
}

            
head() public static method

public static head( string $pattern ): self
$pattern string

                public static function head(string $pattern): self
{
    return self::methods([Method::HEAD], $pattern);
}

            
host() public method

public host( string $host ): self
$host string

                public function host(string $host): self
{
    return $this->hosts($host);
}

            
hosts() public method

public hosts( string $hosts ): self
$hosts string

                public function hosts(string ...$hosts): self
{
    $route = clone $this;
    $route->hosts = [];
    foreach ($hosts as $host) {
        $host = rtrim($host, '/');
        if ($host !== '' && !in_array($host, $route->hosts, true)) {
            $route->hosts[] = $host;
        }
    }
    return $route;
}

            
methods() public static method

public static methods( string[] $methods, string $pattern ): self
$methods string[]
$pattern string

                public static function methods(array $methods, string $pattern): self
{
    return new self($methods, $pattern);
}

            
middleware() public method

Appends a handler middleware definition that should be invoked for a matched route.

First added handler will be executed first. If no actions have been added, the middleware is added to the end of the list. Otherwise, it is added before the action.

public middleware( array|callable|string $definition ): self
$definition array|callable|string

                public function middleware(array|callable|string ...$definition): self
{
    $route = clone $this;
    if ($this->actionAdded) {
        /**
         * @psalm-suppress PropertyTypeCoercion Keys in the replacement array are not preserved.
         * @infection-ignore-all
         */
        array_splice(
            $route->middlewares,
            offset: count($route->middlewares) - 1,
            length: 0,
            replacement: $definition,
        );
    } else {
        array_push(
            $route->middlewares,
            ...array_values($definition),
        );
    }
    $route->enabledMiddlewaresCache = null;
    return $route;
}

            
name() public method

public name( string $name ): self
$name string

                public function name(string $name): self
{
    $route = clone $this;
    $route->name = $name;
    return $route;
}

            
options() public static method

public static options( string $pattern ): self
$pattern string

                public static function options(string $pattern): self
{
    return self::methods([Method::OPTIONS], $pattern);
}

            
override() public method

Marks route as override. When added it will replace existing route with the same name.

public override( ): self

                public function override(): self
{
    $route = clone $this;
    $route->override = true;
    return $route;
}

            
patch() public static method

public static patch( string $pattern ): self
$pattern string

                public static function patch(string $pattern): self
{
    return self::methods([Method::PATCH], $pattern);
}

            
pattern() public method

public pattern( string $pattern ): self
$pattern string

                public function pattern(string $pattern): self
{
    $new = clone $this;
    $new->pattern = $pattern;
    return $new;
}

            
post() public static method

public static post( string $pattern ): self
$pattern string

                public static function post(string $pattern): self
{
    return self::methods([Method::POST], $pattern);
}

            
prependMiddleware() public method

Prepends a handler middleware definition that should be invoked for a matched route. Last added handlers will be executed first.

Passed definitions will be added to beginning. For example:

// Resulting middleware stack order: Middleware1, Middleware2, Middleware3
Route::get('/')
  ->middleware(Middleware3::class)
  ->prependMiddleware(Middleware1::class, Middleware2::class)
public prependMiddleware( array|callable|string $definition ): self
$definition array|callable|string

                public function prependMiddleware(array|callable|string ...$definition): self
{
    $route = clone $this;
    array_unshift(
        $route->middlewares,
        ...array_values($definition),
    );
    $route->enabledMiddlewaresCache = null;
    return $route;
}

            
put() public static method

public static put( string $pattern ): self
$pattern string

                public static function put(string $pattern): self
{
    return self::methods([Method::PUT], $pattern);
}