Router
Router
- Full name:
\Hazaar\Application\Router
Properties
defaultConfig
Default configuration.
public static array<string,mixed> $defaultConfig
- This property is static.
internal
Internal controllers.
public static array<string,string> $internal
- This property is static.
config
public \Hazaar\Map $config
instance
private static ?self $instance
- This property is static.
routeLoader
private \Hazaar\Application\Router\Loader $routeLoader
routes
private \Hazaar\Application\Route[] $routes
route
private ?\Hazaar\Application\Route $route
Methods
__construct
final public __construct(\Hazaar\Map $config): mixed
- This method is final.
Parameters:
Parameter | Type | Description |
---|---|---|
$config | \Hazaar\Map |
initialise
Initializes the Router object and evaluates the request.
final public initialise(\Hazaar\Application\Request $request): bool
- This method is final.
Parameters:
Parameter | Type | Description |
---|---|---|
$request | \Hazaar\Application\Request | the request object |
Throws:
reset
public static reset(): void
- This method is static.
addRoute
Adds a route to the router.
public addRoute(\Hazaar\Application\Route $route): void
This method sets the router for the given route and then adds the route to the list of routes managed by this router.
Parameters:
Parameter | Type | Description |
---|---|---|
$route | \Hazaar\Application\Route | the route to be added |
setRoute
Sets the current route for the router.
public setRoute(\Hazaar\Application\Route $route): void
This method assigns the provided route to the router and sets the router instance within the route.
Parameters:
Parameter | Type | Description |
---|---|---|
$route | \Hazaar\Application\Route | the route to be set |
getRoute
Retrieves the current route.
public getRoute(): null|\Hazaar\Application\Route
Return Value:
the current route, or null if no route is set
getErrorController
public getErrorController(): \Hazaar\Controller\Error
get
Registers a route that responds to HTTP GET requests.
public static get(string $path, mixed $callable): void
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | the URL path for the route |
$callable | mixed | the callback or controller action to handle the request |
post
Registers a route that responds to HTTP POST requests.
public static post(string $path, mixed $callable): void
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | the URL path for the route |
$callable | mixed | the callback or controller method to handle the request |
put
Registers a route that responds to HTTP PUT requests.
public static put(string $path, mixed $callable): void
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | the URI path that the route will respond to |
$callable | mixed | the handler for the route, which can be a callable or other valid route handler |
delete
Registers a route that responds to HTTP DELETE requests.
public static delete(string $path, mixed $callable): void
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | the URL path that the route should match |
$callable | mixed | the callback or controller action to be executed when the route is matched |
patch
Registers a route that responds to HTTP PATCH requests.
public static patch(string $path, mixed $callable): void
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | the URI path that the route will match |
$callable | mixed | the callback or controller action to be executed when the route is matched |
options
Registers a route that responds to HTTP OPTIONS requests.
public static options(string $path, mixed $callable): void
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | the URL path to match |
$callable | mixed | the callback or controller action to handle the request |
any
Registers a route that responds to any HTTP method.
public static any(string $path, mixed $callable): void
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | the path pattern to match |
$callable | mixed | the callback to execute when the route is matched |
set
Sets a new route for the application.
public static set(mixed $callable, ?string $path = null): void
This method sets a new route by accepting a callable and creating a new Route instance with it. If the Router instance is not initialized, the method will return without setting the route.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$callable | mixed | the callable to be used for the new route |
$path | ?string |
match
Matches a route with the given HTTP methods, path, and callable.
public static match(null|string[]|string $methods, string $path, mixed $callable): void
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$methods | null|string[]|string | The HTTP methods to match (e.g., ['GET', 'POST']). |
$path | string | The path to match (e.g., '/user/{id}'). |
$callable | mixed | The callable to execute when the route is matched. It can be a string in the format 'Class::method', an array with the class and method, or a Closure. |
evaluateRequest
Evaluates the given request and matches it against the defined routes.
private evaluateRequest(\Hazaar\Application\Request $request): null|\Hazaar\Application\Route
Parameters:
Parameter | Type | Description |
---|---|---|
$request | \Hazaar\Application\Request | the request to evaluate |
Return Value:
the matched route or null if no route matches
Automatically generated on 2024-11-14