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 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);
}

            
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 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;
}

            
getData() public method

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),
    };
}

            
host() public method

public self host ( string $host )
$host string

                public function host(string $host): self
{
    return $this->hosts($host);
}

            
hosts() public method

public self hosts ( string $hosts )
$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 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;
}

            
namePrefix() public method

public self namePrefix ( string $namePrefix )
$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 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;
}

            
routes() public method

public self routes ( self|Yiisoft\Router\Route $routes )
$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 {@see \Yiisoft\Router\Method::OPTIONS} request will be added automatically.

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;
}