refactor(router): Use Neuron object for route parameters

This commit is contained in:
kj
2025-10-16 12:04:05 -03:00
parent ac9a661bc0
commit 7e7ec68fd7
2 changed files with 13 additions and 12 deletions

View File

@@ -38,7 +38,7 @@ class Request extends Neuron
$this->put = new Neuron(); $this->put = new Neuron();
$this->patch = new Neuron(); $this->patch = new Neuron();
$this->delete = new Neuron(); $this->delete = new Neuron();
$this->params = new Neuron(Router::$currentParams); $this->params = Router::$params ?? new Neuron();
$this->body = file_get_contents("php://input"); $this->body = file_get_contents("php://input");
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : ''; $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';

View File

@@ -15,28 +15,28 @@ namespace Libs;
*/ */
class Router class Router
{ {
private static $get = []; private static array $get = [];
private static $post = []; private static array $post = [];
private static $put = []; private static array $put = [];
private static $patch = []; private static array $patch = [];
private static $delete = []; private static array $delete = [];
/** /**
* Stores the method and index of the last configured route, e.g., ['get', 0]. * Stores the method and index of the last configured route, e.g., ['get', 0].
* Used for chaining methods like middleware() or reconfigure(). * Used for chaining methods like middleware() or reconfigure().
* *
* @var array|null * @var array $last
*/ */
private static $last; private static array $last;
/** /**
* Stores the parameters extracted from the current matching route. * Stores the parameters extracted from the current matching route.
* *
* @var array * @var Neuron $params
*/ */
public static $currentParams = []; public static Neuron $params;
/** /**
* The callback function to be executed when no route matches. * The callback function to be executed when no route matches.
* *
* @var callable|string * @var callable $notFoundCallback
*/ */
public static $notFoundCallback = 'Libs\Router::defaultNotFound'; public static $notFoundCallback = 'Libs\Router::defaultNotFound';
@@ -345,9 +345,10 @@ class Router
// Checking and storing the variable parameters of the route // Checking and storing the variable parameters of the route
if (isset($matches[1])) { if (isset($matches[1])) {
static::$params = new Neuron();
foreach ($matches as $index => $match) { foreach ($matches as $index => $match) {
$paramName = $router['paramNames'][$index - 1]; $paramName = $router['paramNames'][$index - 1];
static::$currentParams[$paramName] = urldecode($match[0]); static::$params->{$paramName} = urldecode($match[0]);
} }
} }