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
$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 {
if (array_keys($arguments) === array_column((new reflection_method($route->controller, $route->method))->getParameters(), 'name')) {
// Arguments match the controller method arguments
if (!isset($route->options['controller_method_arguments'])) {
// Not initialized the option
// Skipping
goto nothing;
}
if ($route->options['controller_method_arguments'] === 'strict') {
// Strict arguments (spread operator)
// Exit (success)
return (string) $route->controller->{$route->method}(...$arguments);
} else {
// Arguments not match the controller method arguments
} else if ($route->options['controller_method_arguments'] === 'array') {
// The array argument (all in one)
// Exit (success)
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_file_uploads',
'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
);

View File

@@ -7,8 +7,13 @@ namespace mirzaev\minimal;
// Files of the project
use mirzaev\minimal\controller,
mirzaev\minimal\middleware,
mirzaev\minimal\http\enumerations\status,
mirzaev\minimal\traits\middleware as middleware_trait;
// Built-in libraries
use RuntimeException as exception_runtime,
DomainException as exception_domain;
/**
* Route
*
@@ -64,6 +69,8 @@ final class route
/**
* Parameters
*
* Defined by request
*
* @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)
@@ -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/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 {
// Write
@@ -103,8 +123,9 @@ final class route
'max_input_vars',
'max_multipart_body_parts',
'max_file_uploads',
'upload_max_filesize' => true,
default => throw new exception_domain("Failed to recognize option: $key", status::internal_server_error->value)
'upload_max_filesize',
'controller_method_arguments' => true,
default => throw new exception_domain("Failed to recognize the option: $key", status::internal_server_error->value)
},
ARRAY_FILTER_USE_KEY
);
@@ -114,18 +135,6 @@ final class route
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
*
@@ -133,7 +142,7 @@ final class route
* @param string|null $method Name of the method of the method of $controller
* @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 $options Options for `request_parse_body` (Only for POST method)
* @param array $options Options
* @param array $middlewares Middlewares stack
*
* @return void
@@ -158,6 +167,9 @@ final class route
// Writing parameters
$this->parameters = $parameters;
// Writing options
$this->options = $options;
// Declaring the register of the middlewares stack validity
$stack = true;
@@ -183,11 +195,5 @@ final class route
// Writing the middlewares stack
$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
{
foreach (is_array($method) ? $method : [$method] as $method) {
foreach (is_array($method) ? $method : [$method] as $_method) {
// Iterate over methods of requests
// Initializing the request
$request = new request(
uri: $urn,
method: $method,
method: $_method,
environment: false
);