0 follower

Final Class Yiisoft\FileRouter\FileRouter

InheritanceYiisoft\FileRouter\FileRouter
ImplementsPsr\Http\Server\MiddlewareInterface

Provides a convention-based router middleware that chooses controller based on its namespace and class name.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\FileRouter\FileRouter
process() Yiisoft\FileRouter\FileRouter
withBaseControllerDirectory() Sets the directory where controllers are located. Yiisoft\FileRouter\FileRouter
withClassPostfix() Sets the postfix for controller class names. Yiisoft\FileRouter\FileRouter
withDefaultControllerName() Sets the default controller name. Yiisoft\FileRouter\FileRouter
withNamespace() Sets the namespace for controller classes. Yiisoft\FileRouter\FileRouter
withRoutePrefix() Sets the route prefix. Yiisoft\FileRouter\FileRouter

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher $middlewareDispatcher )
$middlewareDispatcher \Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher

                public function __construct(
    private readonly MiddlewareDispatcher $middlewareDispatcher,
) {
}

            
process() public method

public \Psr\Http\Message\ResponseInterface process ( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler )
$request \Psr\Http\Message\ServerRequestInterface
$handler \Psr\Http\Server\RequestHandlerInterface

                public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
    $possibleEntryPoints = $this->parseRequestPath($request);
    foreach ($possibleEntryPoints as $possibleEntryPoint) {
        [$controllerClass, $possibleAction] = $possibleEntryPoint;
        if (!class_exists($controllerClass)) {
            continue;
        }
        /**
         * We believe that `$actions` in controller class, if exists, is a correct array.
         *
         * @var string|null $action
         *
         * @psalm-suppress InvalidPropertyFetch, MixedArrayAccess
         */
        $action = $possibleAction ?? ($controllerClass::$actions ?? [
            'HEAD' => 'head',
            'OPTIONS' => 'options',
            'GET' => 'index',
            'POST' => 'create',
            'PUT' => 'update',
            'PATCH' => 'patch',
            'DELETE' => 'delete',
        ])[$request->getMethod()] ?? null;
        if ($action === null) {
            continue;
        }
        if (!method_exists($controllerClass, $action)) {
            continue;
        }
        /**
         * We believe that `$middlewares` in controller class, if exists, is a correct array.
         *
         * @var array[]|callable[]|string[] $middlewares
         *
         * @psalm-suppress InvalidPropertyFetch, MixedArrayAccess
         */
        $middlewares = $controllerClass::$middlewares[$action] ?? [];
        $middlewares[] = [$controllerClass, $action];
        $middlewareDispatcher = $this->middlewareDispatcher->withMiddlewares($middlewares);
        return $middlewareDispatcher->dispatch($request, $handler);
    }
    return $handler->handle($request);
}

            
withBaseControllerDirectory() public method

Sets the directory where controllers are located.

See also withNamespace() if you want to set the namespace for controller classes.

public self withBaseControllerDirectory ( string $directory )
$directory string

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

            
withClassPostfix() public method

Sets the postfix for controller class names.

public self withClassPostfix ( string $postfix )
$postfix string

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

            
withDefaultControllerName() public method

Sets the default controller name.

public self withDefaultControllerName ( string $name )
$name string

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

            
withNamespace() public method

Sets the namespace for controller classes.

See also withBaseControllerDirectory() if you want to set the directory where controllers are located.

public self withNamespace ( string $namespace )
$namespace string

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

            
withRoutePrefix() public method

Sets the route prefix.

public self withRoutePrefix ( string $prefix )
$prefix string

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