Class Yiisoft\Router\Route
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Stringable |
| Subclasses | Yiisoft\ |
Route defines a mapping from URL to callback / name and vice versa.
Public Methods
Method Details
Creates a route.
| public mixed __construct ( string $pattern, string[] $methods, string|null $name = null, array|callable|string|null $action = null, array[]|callable[]|string[] $middlewares = [], array | ||
| $pattern | string |
URL pattern to match. |
| $methods | string[] |
HTTP methods to match. |
| $name | string|null |
Route name. |
| $action | array|callable|string|null |
Primary middleware definition that should be invoked last for a matched route. |
| $middlewares | array[]|callable[]|string[] |
Handler middleware definitions that should be invoked for a matched route. |
| $defaults | array |
Parameter default values indexed by parameter names. |
| $hosts | string[] |
Hosts that the route applies to. |
| $override | boolean |
Whether the route should replace an existing route with the same name. |
| $disabledMiddlewares | array[]|callable[]|string[] |
Middleware definitions to exclude when the action is handled. |
public function __construct(
private string $pattern,
private array $methods,
private ?string $name = null,
array|callable|string|null $action = null,
array $middlewares = [],
array $defaults = [],
array $hosts = [],
private bool $override = false,
array $disabledMiddlewares = [],
) {
/** @infection-ignore-all Array keys are discarded by MiddlewareFilter::filter(). */
$this->middlewares = array_values($middlewares);
$this->setDefaults($defaults);
/** @infection-ignore-all Array keys are discarded by MiddlewareFilter::filter(). */
$this->disabledMiddlewares = array_values($disabledMiddlewares);
$this->setHosts($hosts);
if ($action !== null) {
$this->middlewares[] = $action;
$this->actionAdded = true;
}
}
| public mixed __debugInfo ( ) |
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(),
];
}
| public string __toString ( ) |
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;
}
Appends action handler. It is a primary middleware definition that should be invoked last for a matched route.
| public self action ( array|callable|string $middlewareDefinition ) | ||
| $middlewareDefinition | array|callable|string | |
public function action(array|callable|string $middlewareDefinition): self
{
$route = clone $this;
$route->middlewares[] = $middlewareDefinition;
$route->actionAdded = true;
return $route;
}
Parameter default values indexed by parameter names.
| public self defaults ( array $defaults ) | ||
| $defaults | array | |
public function defaults(array $defaults): self
{
$route = clone $this;
$route->setDefaults($defaults);
return $route;
}
| public static self delete ( string $pattern ) | ||
| $pattern | string | |
public static function delete(string $pattern): self
{
/** @psalm-suppress DeprecatedMethod Retain delegation between deprecated factories for compatibility. */
return self::methods([Method::DELETE], $pattern);
}
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 self disableMiddleware ( mixed $definition ) | ||
| $definition | mixed | |
public function disableMiddleware(mixed ...$definition): self
{
$route = clone $this;
array_push(
$route->disabledMiddlewares,
...array_values($definition),
);
$route->enabledMiddlewaresCache = null;
return $route;
}
| public static self get ( string $pattern ) | ||
| $pattern | string | |
public static function get(string $pattern): self
{
/** @psalm-suppress DeprecatedMethod Retain delegation between deprecated factories for compatibility. */
return self::methods([Method::GET], $pattern);
}
| public mixed getData ( string $key ) | ||
| $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),
};
}
| public static self head ( string $pattern ) | ||
| $pattern | string | |
public static function head(string $pattern): self
{
/** @psalm-suppress DeprecatedMethod Retain delegation between deprecated factories for compatibility. */
return self::methods([Method::HEAD], $pattern);
}
| public self host ( string $host ) | ||
| $host | string | |
public function host(string $host): self
{
return $this->hosts($host);
}
| public self hosts ( string $hosts ) | ||
| $hosts | string | |
public function hosts(string ...$hosts): self
{
$route = clone $this;
$route->setHosts($hosts);
return $route;
}
| public static self methods ( string[] $methods, string $pattern ) | ||
| $methods | string[] | |
| $pattern | string | |
public static function methods(array $methods, string $pattern): self
{
if (static::class !== self::class) {
throw new LogicException(sprintf(
'Static factory methods are only valid on %s itself, not %s. Use the constructor instead.',
self::class,
static::class,
));
}
return new self($pattern, $methods);
}
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 self middleware ( array|callable|string $definition ) | ||
| $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;
}
| public self name ( string|\ | ||
| $name | string|\ |
Name of the route. A backed enumeration is resolved to its backing value, so the route name is still a string afterwards. |
public function name(string|BackedEnum $name): self
{
$route = clone $this;
$route->name = $name instanceof BackedEnum ? (string) $name->value : $name;
return $route;
}
| public static self options ( string $pattern ) | ||
| $pattern | string | |
public static function options(string $pattern): self
{
/** @psalm-suppress DeprecatedMethod Retain delegation between deprecated factories for compatibility. */
return self::methods([Method::OPTIONS], $pattern);
}
Marks route as override. When added it will replace existing route with the same name.
| public self override ( ) |
public function override(): self
{
$route = clone $this;
$route->override = true;
return $route;
}
| public static self patch ( string $pattern ) | ||
| $pattern | string | |
public static function patch(string $pattern): self
{
/** @psalm-suppress DeprecatedMethod Retain delegation between deprecated factories for compatibility. */
return self::methods([Method::PATCH], $pattern);
}
| public self pattern ( string $pattern ) | ||
| $pattern | string | |
public function pattern(string $pattern): self
{
$new = clone $this;
$new->pattern = $pattern;
return $new;
}
| public static self post ( string $pattern ) | ||
| $pattern | string | |
public static function post(string $pattern): self
{
/** @psalm-suppress DeprecatedMethod Retain delegation between deprecated factories for compatibility. */
return self::methods([Method::POST], $pattern);
}
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
(new \Yiisoft\Router\Route\Get('/'))
->middleware(Middleware3::class)
->prependMiddleware(Middleware1::class, Middleware2::class)
| public self prependMiddleware ( array|callable|string $definition ) | ||
| $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;
}
| public static self put ( string $pattern ) | ||
| $pattern | string | |
public static function put(string $pattern): self
{
/** @psalm-suppress DeprecatedMethod Retain delegation between deprecated factories for compatibility. */
return self::methods([Method::PUT], $pattern);
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.