fixed and dynamic routes

This commit is contained in:
2026-05-31 19:09:32 +00:00
parent ebd793a2d3
commit e5939effd3
4 changed files with 53 additions and 33 deletions

View File

@@ -261,18 +261,32 @@ final class core
// Initializing the controller method arguments // Initializing the controller method arguments
$arguments = $route->parameters + $route->variables + $request->parameters; $arguments = $route->parameters + $route->variables + $request->parameters;
// Processing the method of the controller and exit (success) // Processing the controller method and exit (success)
$action = function () use ($route, $request, $arguments): string { $action = function () use ($route, $request, $arguments): string {
if (array_keys($arguments) === array_column((new reflection_method($route->controller, $route->method))->getParameters(), 'name')) { if (!isset($route->options['controller_method_arguments'])) {
// Arguments match the controller method arguments // Not initialized the option
// Skipping
goto nothing;
}
if ($route->options['controller_method_arguments'] === 'strict') {
// Strict arguments (spread operator)
// Exit (success) // Exit (success)
return (string) $route->controller->{$route->method}(...$arguments); return (string) $route->controller->{$route->method}(...$arguments);
} else { } else if ($route->options['controller_method_arguments'] === 'array') {
// Arguments not match the controller method arguments // The array argument (all in one)
// Exit (success) // Exit (success)
return (string) $route->controller->{$route->method}($arguments ? $arguments : null); return (string) $route->controller->{$route->method}($arguments ? $arguments : null);
} else {
// Nothing
nothing:
// Exit (success)
return (string) $route->controller->{$route->method}();
} }
}; };

View File

@@ -290,7 +290,7 @@ final class request
'max_multipart_body_parts', 'max_multipart_body_parts',
'max_file_uploads', 'max_file_uploads',
'upload_max_filesize' => true, 'upload_max_filesize' => true,
default => throw new exception_domain("Failed to recognize option: $key", status::internal_server_error->value) default => false
}, },
ARRAY_FILTER_USE_KEY ARRAY_FILTER_USE_KEY
); );

View File

@@ -7,8 +7,13 @@ namespace mirzaev\minimal;
// Files of the project // Files of the project
use mirzaev\minimal\controller, use mirzaev\minimal\controller,
mirzaev\minimal\middleware, mirzaev\minimal\middleware,
mirzaev\minimal\http\enumerations\status,
mirzaev\minimal\traits\middleware as middleware_trait; mirzaev\minimal\traits\middleware as middleware_trait;
// Built-in libraries
use RuntimeException as exception_runtime,
DomainException as exception_domain;
/** /**
* Route * Route
* *
@@ -64,6 +69,8 @@ final class route
/** /**
* Parameters * Parameters
* *
* Defined by request
*
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks) * @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
* *
* @var array $parameters Arguments for the $this->method (will be concatenated together with generated request parameters) * @var array $parameters Arguments for the $this->method (will be concatenated together with generated request parameters)
@@ -74,16 +81,29 @@ final class route
} }
/** /**
* Options * Variables
* *
* Required if $this->method !== method::post * Defined by route URN
*
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @var array $variables Arguments for the $this->method (will be concatenated together with generated request parameters)
*/
public array $variables = [] {
// Read
&get => $this->variables;
}
/**
* Options
* *
* @see https://wiki.php.net/rfc/rfc1867-non-post About request_parse_body() * @see https://wiki.php.net/rfc/rfc1867-non-post About request_parse_body()
* @see https://wiki.php.net/rfc/property-hooks Hooks (find a table about backed and virtual hooks) * @see https://wiki.php.net/rfc/property-hooks Hooks (find a table about backed and virtual hooks)
* *
* @throws exception_runtime if reinitialize the property * @throws exception_runtime when reinitializing the property
* @throws exception_domain if not recognied the option
* *
* @var array $options Options for `request_parse_body($options)` * @var array $options Options
*/ */
public array $options { public array $options {
// Write // Write
@@ -103,8 +123,9 @@ final class route
'max_input_vars', 'max_input_vars',
'max_multipart_body_parts', 'max_multipart_body_parts',
'max_file_uploads', 'max_file_uploads',
'upload_max_filesize' => true, 'upload_max_filesize',
default => throw new exception_domain("Failed to recognize option: $key", status::internal_server_error->value) 'controller_method_arguments' => true,
default => throw new exception_domain("Failed to recognize the option: $key", status::internal_server_error->value)
}, },
ARRAY_FILTER_USE_KEY ARRAY_FILTER_USE_KEY
); );
@@ -114,18 +135,6 @@ final class route
get => $this->options ?? []; get => $this->options ?? [];
} }
/**
* Parameters
*
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @var array $parameters Arguments for the $this->method (will be concatenated together with generated request parameters)
*/
public array $variables = [] {
// Read
&get => $this->variables;
}
/** /**
* Constructor * Constructor
* *
@@ -133,7 +142,7 @@ final class route
* @param string|null $method Name of the method of the method of $controller * @param string|null $method Name of the method of the method of $controller
* @param string|model|null $model Name of the model * @param string|model|null $model Name of the model
* @param array $parameters Arguments for the $method (will be concatenated together with generated request parameters) * @param array $parameters Arguments for the $method (will be concatenated together with generated request parameters)
* @param array $options Options for `request_parse_body` (Only for POST method) * @param array $options Options
* @param array $middlewares Middlewares stack * @param array $middlewares Middlewares stack
* *
* @return void * @return void
@@ -158,6 +167,9 @@ final class route
// Writing parameters // Writing parameters
$this->parameters = $parameters; $this->parameters = $parameters;
// Writing options
$this->options = $options;
// Declaring the register of the middlewares stack validity // Declaring the register of the middlewares stack validity
$stack = true; $stack = true;
@@ -183,11 +195,5 @@ final class route
// Writing the middlewares stack // Writing the middlewares stack
$this->middlewares = $middlewares; $this->middlewares = $middlewares;
} }
// Writing options
if (match ($method) {
'GET', 'PUT', 'PATCH', 'DELETE' => true,
default => false
}) $this->options = $options;
} }
} }

View File

@@ -57,13 +57,13 @@ final class router
*/ */
public function write(string $urn, route $route, string|array $method): self public function write(string $urn, route $route, string|array $method): self
{ {
foreach (is_array($method) ? $method : [$method] as $method) { foreach (is_array($method) ? $method : [$method] as $_method) {
// Iterate over methods of requests // Iterate over methods of requests
// Initializing the request // Initializing the request
$request = new request( $request = new request(
uri: $urn, uri: $urn,
method: $method, method: $_method,
environment: false environment: false
); );