Skip to content

feat: Namespace autorecognition #2

@xepozz

Description

@xepozz

There is a way to find the namespace of the controller automatically, instead of calling \Yiisoft\FileRouter\FileRouter::withNamespace.

It costs additional io call, regexp parsing and dependency yiisoft/aliases, but it's good feature not to configure the router.

$controllerDirectory = $this->aliases->get('src') . DIRECTORY_SEPARATOR . $this->baseControllerDirectory;
$classPath = $controllerDirectory . DIRECTORY_SEPARATOR . $directoryPath . DIRECTORY_SEPARATOR . $controller;
$filename = $classPath . '.php';
if (file_exists($filename)) {
    $content = file_get_contents($filename);
    $namespace = preg_match('#namespace\s+(.+?);#', $content, $matches) ? $matches[1] : '';
    if (class_exists($namespace . '\\' . $controller)) {
        return $namespace . '\\' . $controller;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions