Final Class Yiisoft\Router\Group
| Inheritance | Yiisoft\ |
|---|
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Creates a route group. | Yiisoft\ |
| create() | Create a new group instance. | Yiisoft\ |
| disableMiddleware() | Excludes middleware from being invoked when action is handled. | Yiisoft\ |
| getData() | Yiisoft\ |
|
| host() | Yiisoft\ |
|
| hosts() | Yiisoft\ |
|
| middleware() | Appends a handler middleware definition that should be invoked for a matched route. | Yiisoft\ |
| namePrefix() | Yiisoft\ |
|
| prependMiddleware() | Prepends a handler middleware definition that should be invoked for a matched route. | Yiisoft\ |
| routes() | Yiisoft\ |
|
| withCors() | Adds a middleware definition that handles CORS requests. | Yiisoft\ |
Method Details
Creates a route group.
| public mixed __construct ( string|null $prefix = null, Yiisoft\ | ||
| $prefix | string|null |
URL prefix to prepend to all routes of the group. |
| $routes | Yiisoft\ |
Routes and nested groups to include in the group. |
| $middlewares | array[]|callable[]|string[] |
Handler middleware definitions that should be invoked for a matched route. |
| $hosts | string[] |
Hosts that the group applies to. |
| $namePrefix | string|null |
Name prefix to prepend to all routes of the group. |
| $disabledMiddlewares | array[]|callable[]|string[] |
Middleware definitions to exclude when an action is handled. |
| $corsMiddleware | array|callable|string|null |
Middleware definition for CORS requests. |
public function __construct(
private ?string $prefix = null,
private array $routes = [],
array $middlewares = [],
array $hosts = [],
private ?string $namePrefix = null,
array $disabledMiddlewares = [],
array|callable|string|null $corsMiddleware = null,
) {
/** @infection-ignore-all Array keys are discarded by MiddlewareFilter::filter(). */
$this->middlewares = array_values($middlewares);
/** @infection-ignore-all Array keys are discarded by MiddlewareFilter::filter(). */
$this->disabledMiddlewares = array_values($disabledMiddlewares);
$this->corsMiddleware = $corsMiddleware;
$this->setHosts($hosts);
}
Create a new group instance.
| public static self create ( string|null $prefix = null ) | ||
| $prefix | string|null |
URL prefix to prepend to all routes of the group. |
public static function create(?string $prefix = null): self
{
return new self($prefix);
}
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
{
$new = clone $this;
array_push(
$new->disabledMiddlewares,
...array_values($definition),
);
$new->enabledMiddlewaresCache = null;
return $new;
}
| public mixed getData ( string $key ) | ||
| $key | string | |
public function getData(string $key): mixed
{
return match ($key) {
'prefix' => $this->prefix,
'namePrefix' => $this->namePrefix,
'host' => $this->hosts[0] ?? null,
'hosts' => $this->hosts,
'corsMiddleware' => $this->corsMiddleware,
'routes' => $this->routes,
'hasCorsMiddleware' => $this->corsMiddleware !== null,
'enabledMiddlewares' => $this->getEnabledMiddlewares(),
default => throw new InvalidArgumentException('Unknown data key: ' . $key),
};
}
| 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
{
$new = clone $this;
$new->setHosts($hosts);
return $new;
}
Appends a handler middleware definition that should be invoked for a matched route.
First added handler will be executed first.
| public self middleware ( array|callable|string $definition ) | ||
| $definition | array|callable|string | |
public function middleware(array|callable|string ...$definition): self
{
$new = clone $this;
array_push(
$new->middlewares,
...array_values($definition),
);
$new->enabledMiddlewaresCache = null;
return $new;
}
| public self namePrefix ( string $namePrefix ) | ||
| $namePrefix | string | |
public function namePrefix(string $namePrefix): self
{
$new = clone $this;
$new->namePrefix = $namePrefix;
return $new;
}
Prepends a handler middleware definition that should be invoked for a matched route.
First added handler will be executed last.
| public self prependMiddleware ( array|callable|string $definition ) | ||
| $definition | array|callable|string | |
public function prependMiddleware(array|callable|string ...$definition): self
{
$new = clone $this;
array_unshift(
$new->middlewares,
...array_values($definition),
);
$new->enabledMiddlewaresCache = null;
return $new;
}
| public self routes ( self|Yiisoft\ | ||
| $routes | self|Yiisoft\ |
|
public function routes(self|Route ...$routes): self
{
$new = clone $this;
$new->routes = $routes;
return $new;
}
Adds a middleware definition that handles CORS requests.
If set, routes for {@see \
| public self withCors ( array|callable|string|null $middlewareDefinition ) | ||
| $middlewareDefinition | array|callable|string|null |
Middleware definition for CORS requests. |
public function withCors(array|callable|string|null $middlewareDefinition): self
{
$group = clone $this;
$group->corsMiddleware = $middlewareDefinition;
return $group;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.