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 __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(),
];
}
| 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;
}
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;
}
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;
}
| public static delete( string $pattern ): self | ||
| $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 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;
}
| public static get( string $pattern ): self | ||
| $pattern | string | |
public static function get(string $pattern): self
{
return self::methods([Method::GET], $pattern);
}
| 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),
};
}
| public static head( string $pattern ): self | ||
| $pattern | string | |
public static function head(string $pattern): self
{
return self::methods([Method::HEAD], $pattern);
}
| public host( string $host ): self | ||
| $host | string | |
public function host(string $host): self
{
return $this->hosts($host);
}
| 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;
}
| 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);
}
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;
}
| public name( string $name ): self | ||
| $name | string | |
public function name(string $name): self
{
$route = clone $this;
$route->name = $name;
return $route;
}
| public static options( string $pattern ): self | ||
| $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 override( ): self |
public function override(): self
{
$route = clone $this;
$route->override = true;
return $route;
}
| public static patch( string $pattern ): self | ||
| $pattern | string | |
public static function patch(string $pattern): self
{
return self::methods([Method::PATCH], $pattern);
}
| public pattern( string $pattern ): self | ||
| $pattern | string | |
public function pattern(string $pattern): self
{
$new = clone $this;
$new->pattern = $pattern;
return $new;
}
| public static post( string $pattern ): self | ||
| $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 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;
}
Signup or Login in order to comment.