Final Class Yiisoft\Router\Route
| Inheritance | Yiisoft\Router\Route |
|---|---|
| Implements | Stringable |
Route defines a mapping from URL to callback / name and vice versa.
Public Methods
Method Details
| 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->defaults = array_map(\strval(...), $defaults);
return $route;
}
| public static self delete ( string $pattern ) | ||
| $pattern | string | |
public static function delete(string $pattern): self
{
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
{
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
{
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->hosts = [];
foreach ($hosts as $host) {
$host = rtrim($host, '/');
if ($host !== '' && !in_array($host, $route->hosts, true)) {
$route->hosts[] = $host;
}
}
return $route;
}
| public static self methods ( string[] $methods, string $pattern ) | ||
| $methods | string[] | |
| $pattern | string | |
public static function methods(array $methods, string $pattern): self
{
return new self($methods, $pattern);
}
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 ) | ||
| $name | string | |
public function name(string $name): self
{
$route = clone $this;
$route->name = $name;
return $route;
}
| public static self options ( string $pattern ) | ||
| $pattern | string | |
public static function options(string $pattern): self
{
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
{
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
{
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
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;
}
Signup or Login in order to comment.