fix(router): support both snake_case and kebab-case controller methods

This commit is contained in:
Philipp Urbschat 2026-09-12 23:41:28 +02:00
parent 0ec9143ec8
commit 3c78add335
Signed by: Phili
SSH key fingerprint: SHA256:ZSQWnldzrYiABzOV6vTICPe0h19pTpus7sCbm2S0po0

View file

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