Fix PHP 8 router parameter dispatching and lesson slug playback lookup
This commit is contained in:
@@ -13,6 +13,7 @@ class Request
|
||||
private array $queryParams;
|
||||
private array $bodyParams;
|
||||
private array $headers;
|
||||
private array $routeParams = [];
|
||||
|
||||
// Explicit properties to store authentication details to avoid deprecation warnings in PHP 8.2+
|
||||
public ?int $user_id = null;
|
||||
@@ -78,6 +79,21 @@ class Request
|
||||
$this->queryParams = $queryParams;
|
||||
}
|
||||
|
||||
public function setParams(array $params): void
|
||||
{
|
||||
$this->routeParams = $params;
|
||||
}
|
||||
|
||||
public function getParams(): array
|
||||
{
|
||||
return $this->routeParams;
|
||||
}
|
||||
|
||||
public function getParam(string $key, $default = null)
|
||||
{
|
||||
return $this->routeParams[$key] ?? $default;
|
||||
}
|
||||
|
||||
public function get(string $key, $default = null)
|
||||
{
|
||||
return $this->bodyParams[$key] ?? ($this->queryParams[$key] ?? $default);
|
||||
|
||||
@@ -88,6 +88,7 @@ class Router
|
||||
if ($route['method'] === $method && preg_match($route['pattern'], $path, $matches)) {
|
||||
// Filter named captures from regex match
|
||||
$params = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
|
||||
$request->setParams($params);
|
||||
|
||||
// Run global middleware first
|
||||
foreach ($this->globalMiddleware as $mw) {
|
||||
@@ -108,13 +109,20 @@ class Router
|
||||
if (class_exists($controllerClass)) {
|
||||
$controller = new $controllerClass();
|
||||
if (method_exists($controller, $action)) {
|
||||
// Call action with Request, Response and URI dynamic parameters
|
||||
call_user_func_array([$controller, $action], array_merge([$request, $response], $params));
|
||||
$ref = new \ReflectionMethod($controller, $action);
|
||||
$paramCount = $ref->getNumberOfParameters();
|
||||
if ($paramCount <= 2) {
|
||||
$controller->$action($request, $response);
|
||||
} else {
|
||||
$args = array_values(array_merge([$request, $response], array_values($params)));
|
||||
call_user_func_array([$controller, $action], $args);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
} elseif (is_callable($handler)) {
|
||||
call_user_func_array($handler, array_merge([$request, $response], $params));
|
||||
$args = array_values(array_merge([$request, $response], array_values($params)));
|
||||
call_user_func_array($handler, $args);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user