fix(router): support both snake_case and kebab-case controller methods
This commit is contained in:
parent
0ec9143ec8
commit
3c78add335
1 changed files with 7 additions and 3 deletions
|
|
@ -18,11 +18,15 @@ class Router {
|
||||||
}
|
}
|
||||||
require_once $controllerPath . $this->controller . '.php';
|
require_once $controllerPath . $this->controller . '.php';
|
||||||
$this->controller = new $this->controller;
|
$this->controller = new $this->controller;
|
||||||
// Method Check (Case-insensitive & kebab-case support)
|
// Method Check (Case-insensitive & kebab-case/snake_case support)
|
||||||
if (isset($url[1])) {
|
if (isset($url[1])) {
|
||||||
$candidateMethod = str_replace('-', '', strtolower($url[1]));
|
$rawMethod = strtolower($url[1]);
|
||||||
|
$normalizedMethod = str_replace(['-', '_'], '', $rawMethod);
|
||||||
foreach (get_class_methods($this->controller) as $methodName) {
|
foreach (get_class_methods($this->controller) as $methodName) {
|
||||||
if (strcasecmp(str_replace('_', '', $methodName), $candidateMethod) === 0 && strpos($methodName, '_') !== 0) {
|
if (strpos($methodName, '_') === 0) {
|
||||||
|
continue; // Skip magic/internal methods
|
||||||
|
}
|
||||||
|
if (strcasecmp($methodName, $rawMethod) === 0 || strcasecmp(str_replace(['-', '_'], '', $methodName), $normalizedMethod) === 0) {
|
||||||
$this->method = $methodName;
|
$this->method = $methodName;
|
||||||
unset($url[1]);
|
unset($url[1]);
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue