Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [Auto Routing Improved] fallback to default controller's default method #7406

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public function read(string $class, string $defaultController = 'Home', string $
$classInUri,
$classname,
$methodName,
$httpVerb
$httpVerb,
$method
);

if ($routeForDefaultController !== []) {
Expand All @@ -85,23 +86,7 @@ public function read(string $class, string $defaultController = 'Home', string $
continue;
}

$params = [];
$routeParams = '';
$refParams = $method->getParameters();

foreach ($refParams as $param) {
$required = true;
if ($param->isOptional()) {
$required = false;

$routeParams .= '[/..]';
} else {
$routeParams .= '/..';
}

// [variable_name => required?]
$params[$param->getName()] = $required;
}
[$params, $routeParams] = $this->getParameters($method);

// Route for the default method.
$output[] = [
Expand All @@ -117,23 +102,7 @@ public function read(string $class, string $defaultController = 'Home', string $

$route = $classInUri . '/' . $methodInUri;

$params = [];
$routeParams = '';
$refParams = $method->getParameters();

foreach ($refParams as $param) {
$required = true;
if ($param->isOptional()) {
$required = false;

$routeParams .= '[/..]';
} else {
$routeParams .= '/..';
}

// [variable_name => required?]
$params[$param->getName()] = $required;
}
[$params, $routeParams] = $this->getParameters($method);

// If it is the default controller, the method will not be
// routed.
Expand All @@ -155,6 +124,29 @@ public function read(string $class, string $defaultController = 'Home', string $
return $output;
}

private function getParameters(ReflectionMethod $method): array
{
$params = [];
$routeParams = '';
$refParams = $method->getParameters();

foreach ($refParams as $param) {
$required = true;
if ($param->isOptional()) {
$required = false;

$routeParams .= '[/..]';
} else {
$routeParams .= '/..';
}

// [variable_name => required?]
$params[$param->getName()] = $required;
}

return [$params, $routeParams];
}

/**
* @phpstan-param class-string $classname
*
Expand Down Expand Up @@ -189,7 +181,8 @@ private function getRouteForDefaultController(
string $uriByClass,
string $classname,
string $methodName,
string $httpVerb
string $httpVerb,
ReflectionMethod $method
): array {
$output = [];

Expand All @@ -198,12 +191,18 @@ private function getRouteForDefaultController(
$routeWithoutController = rtrim(preg_replace($pattern, '', $uriByClass), '/');
$routeWithoutController = $routeWithoutController ?: '/';

[$params, $routeParams] = $this->getParameters($method);

if ($routeWithoutController === '/' && $routeParams !== '') {
$routeWithoutController = '';
}

$output[] = [
'method' => $httpVerb,
'route' => $routeWithoutController,
'route_params' => '',
'route_params' => $routeParams,
'handler' => '\\' . $classname . '::' . $methodName,
'params' => [],
'params' => $params,
];
}

Expand Down
Loading