0 follower

Final Class Yiisoft\Router\Group

InheritanceYiisoft\Router\Group

Public Methods

Hide inherited methods

Method Description Defined By
create() Create a new group instance. Yiisoft\Router\Group
disableMiddleware() Excludes middleware from being invoked when action is handled. Yiisoft\Router\Group
getData() Yiisoft\Router\Group
host() Yiisoft\Router\Group
hosts() Yiisoft\Router\Group
middleware() Appends a handler middleware definition that should be invoked for a matched route. Yiisoft\Router\Group
namePrefix() Yiisoft\Router\Group
prependMiddleware() Prepends a handler middleware definition that should be invoked for a matched route. Yiisoft\Router\Group
routes() Yiisoft\Router\Group
withCors() Adds a middleware definition that handles CORS requests. Yiisoft\Router\Group

Method Details

Hide inherited methods

create() public static method

Create a new group instance.

public static create( string|null $prefix null ): self
$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);
}

            
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
{
    $new = clone $this;
    array_push(
        $new->disabledMiddlewares,
        ...array_values($definition),
    );
    $new->enabledMiddlewaresCache = null;
    return $new;
}

            
getData() public method

public getData( string $key ): mixed
$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),
    };
}

            
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
{
    $new = clone $this;
    foreach ($hosts as $host) {
        $host = rtrim($host, '/');
        if ($host !== '' && !in_array($host, $new->hosts, true)) {
            $new->hosts[] = $host;
        }
    }
    return $new;
}

            
middleware() public method

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

First added handler will be executed first.

public middleware( array|callable|string $definition ): self
$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;
}

            
namePrefix() public method

public namePrefix( string $namePrefix ): self
$namePrefix string

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

            
prependMiddleware() public method

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

First added handler will be executed last.

public prependMiddleware( array|callable|string $definition ): self
$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;
}

            
routes() public method

public routes( self|Yiisoft\Router\Route $routes ): self
$routes self|Yiisoft\Router\Route

                public function routes(self|Route ...$routes): self
{
    $new = clone $this;
    $new->routes = $routes;
    return $new;
}

            
withCors() public method

Adds a middleware definition that handles CORS requests.

If set, routes for \Yiisoft\Router\Method::OPTIONS request will be added automatically.

public withCors( array|callable|string|null $middlewareDefinition ): self
$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;
}