Documentation

Routes
in package

Table of Contents

Constants

DELETE  = 'DELETE'
GET  = 'GET'
OPTIONS  = 'OPTIONS'
PATCH  = 'PATCH'
POST  = 'POST'
PUT  = 'PUT'

Properties

$request  : Request
$currentRoute  : array<string|int, mixed>|null
$middlewares  : array<string|int, mixed>
$prefix  : string
$response  : Response
$routes  : array<string|int, mixed>
$tmpRoutes  : array<string|int, mixed>

Methods

__construct()  : mixed
Routes constructor.
add()  : void
Adds a new route to the routing system.
append()  : Routes
Appends additional routes to the main route handler.
delete()  : Routes
Wrapper method for adding a new DELETE route.
get()  : Routes
Wrapper method for adding a new GET route.
get_routes()  : array<string|int, mixed>
Retrieves a list of all defined routes.
handle()  : void
Handles the execution of the routes and their associated middlewares.
middleware()  : Routes
Sets the middlewares to be applied to routes.
options()  : Routes
Wrapper method for adding a new OPTIONS route.
patch()  : Routes
Wrapper method for adding a new PATCH route.
post()  : Routes
Wrapper method for adding a new POST route.
prefix()  : Routes
Sets a prefix to be applied to all routes defined within the chain.
put()  : Routes
Wrapper method for adding a new PUT route.
route()  : Routes
Adds a new route to the temporary route list for chained method calls.
save()  : Routes
Commits temporary routes to the main route list.
execute_middleware()  : void
Executes all specified middleware before the route's callback.
get_all_arguments()  : array<string|int, mixed>|null
Retrieves the arguments for the route's callback method.
get_path()  : string
Returns the current URL path.
get_route_arguments()  : array<string|int, mixed>
Retrieves the arguments for a given route based on the URL path.
setup_callback()  : mixed
Sets up the callback properties for a route.

Constants

DELETE

public string DELETE = 'DELETE'

DELETE Constant representing a DELETE request method

GET

public string GET = 'GET'

GET Constant representing a GET request method

OPTIONS

public string OPTIONS = 'OPTIONS'

OPTIONS Constant representing an OPTIONS request method

PATCH

public string PATCH = 'PATCH'

PATCH Constant representing a PATCH request method

POST

public string POST = 'POST'

POST Constant representing a POST request method

PUT

public string PUT = 'PUT'

PUT Constant representing a PUT request method

Properties

$request

public Request $request

Instance of the Request class to handle incoming HTTP requests.

$currentRoute

private array<string|int, mixed>|null $currentRoute = NULL

Current route being processed, used for easy access in other methods.

$middlewares

private array<string|int, mixed> $middlewares = []

List of middleware to be executed before the routes.

$prefix

private string $prefix = ''

Prefix to be applied to all defined routes.

$response

private Response $response

Instance of the Response class to manage HTTP responses.

$routes

private array<string|int, mixed> $routes = []

List of all registered routes.

$tmpRoutes

private array<string|int, mixed> $tmpRoutes = []

Temporary holder of route information before it's committed to $routes.

Methods

__construct()

Routes constructor.

public __construct() : mixed

Initializes the request and response instances.

add()

Adds a new route to the routing system.

public add([string $path = '' ][, callable|array<string|int, mixed>|string|null $callback = NULL ][, string|array<string|int, mixed>|null $methods = self::GET ]) : void
Parameters
$path : string = ''

The URL path for the route.

$callback : callable|array<string|int, mixed>|string|null = NULL

The callback function, class method, or callable for the route.

$methods : string|array<string|int, mixed>|null = self::GET

The HTTP methods the route responds to (e.g., GET, POST).

append()

Appends additional routes to the main route handler.

public append(array<string|int, mixed> $routes) : Routes
Parameters
$routes : array<string|int, mixed>

List of routes from other route classes.

Return values
Routes

Returns the current Routes instance to allow method chaining.

delete()

Wrapper method for adding a new DELETE route.

public delete(string $path[, callable|array<string|int, mixed>|string|null $callback = NULL ]) : Routes
Parameters
$path : string

The URL path for the route.

$callback : callable|array<string|int, mixed>|string|null = NULL

The callback function, class method, or callable for the route.

Return values
Routes

Returns the current Routes instance to allow method chaining.

get()

Wrapper method for adding a new GET route.

public get(string $path[, callable|array<string|int, mixed>|string|null $callback = NULL ]) : Routes
Parameters
$path : string

The URL path for the route.

$callback : callable|array<string|int, mixed>|string|null = NULL

The callback function, class method, or callable for the route.

Return values
Routes

Returns the current Routes instance to allow method chaining.

get_routes()

Retrieves a list of all defined routes.

public get_routes() : array<string|int, mixed>
Return values
array<string|int, mixed>

The list of registered routes.

handle()

Handles the execution of the routes and their associated middlewares.

public handle() : void
Tags
throws
RouteNotFoundException

If the route cannot be found.

throws
CallbackNotFound

If the callback for the route cannot be found.

middleware()

Sets the middlewares to be applied to routes.

public middleware(array<string|int, mixed> $data) : Routes
Parameters
$data : array<string|int, mixed>

List of middlewares to be executed before the routes.

Return values
Routes

Returns the current Routes instance to allow method chaining.

options()

Wrapper method for adding a new OPTIONS route.

public options(string $path[, callable|array<string|int, mixed>|string|null $callback = NULL ]) : Routes
Parameters
$path : string

The URL path for the route.

$callback : callable|array<string|int, mixed>|string|null = NULL

The callback function, class method, or callable for the route.

Return values
Routes

Returns the current Routes instance to allow method chaining.

patch()

Wrapper method for adding a new PATCH route.

public patch(string $path[, callable|array<string|int, mixed>|string|null $callback = NULL ]) : Routes
Parameters
$path : string

The URL path for the route.

$callback : callable|array<string|int, mixed>|string|null = NULL

The callback function, class method, or callable for the route.

Return values
Routes

Returns the current Routes instance to allow method chaining.

post()

Wrapper method for adding a new POST route.

public post(string $path[, callable|array<string|int, mixed>|string|null $callback = NULL ]) : Routes
Parameters
$path : string

The URL path for the route.

$callback : callable|array<string|int, mixed>|string|null = NULL

The callback function, class method, or callable for the route.

Return values
Routes

Returns the current Routes instance to allow method chaining.

prefix()

Sets a prefix to be applied to all routes defined within the chain.

public prefix([string $prefix = '' ]) : Routes
Parameters
$prefix : string = ''

The prefix to be added to all routes.

Return values
Routes

Returns the current Routes instance to allow method chaining.

put()

Wrapper method for adding a new PUT route.

public put(string $path[, callable|array<string|int, mixed>|string|null $callback = NULL ]) : Routes
Parameters
$path : string

The URL path for the route.

$callback : callable|array<string|int, mixed>|string|null = NULL

The callback function, class method, or callable for the route.

Return values
Routes

Returns the current Routes instance to allow method chaining.

route()

Adds a new route to the temporary route list for chained method calls.

public route(string $path, callable|array<string|int, mixed>|string|null $callback[, string|array<string|int, mixed> $methods = self::GET ]) : Routes
Parameters
$path : string

The URL path for the route.

$callback : callable|array<string|int, mixed>|string|null

The callback function, class method, or callable for the route.

$methods : string|array<string|int, mixed> = self::GET

The HTTP methods the route responds to (e.g., GET, POST).

Return values
Routes

Returns the current Routes instance to allow method chaining.

save()

Commits temporary routes to the main route list.

public save([bool $cleanData = true ]) : Routes
Parameters
$cleanData : bool = true

If true, clears the temporary data after saving.

Return values
Routes

Returns the current Routes instance to allow method chaining.

execute_middleware()

Executes all specified middleware before the route's callback.

private execute_middleware(array<string|int, mixed> $data) : void
Parameters
$data : array<string|int, mixed>

List of middleware to be executed.

Tags
throws
CallbackNotFound

If the specified middleware is not found.

get_all_arguments()

Retrieves the arguments for the route's callback method.

private get_all_arguments(object|array<string|int, mixed>|string $function) : array<string|int, mixed>|null
Parameters
$function : object|array<string|int, mixed>|string

The callback function or method.

Return values
array<string|int, mixed>|null

Returns an associative array of arguments for the callback, or null on error.

get_path()

Returns the current URL path.

private get_path() : string
Return values
string

The current path being accessed.

get_route_arguments()

Retrieves the arguments for a given route based on the URL path.

private get_route_arguments(array<string|int, mixed> $route, string $path) : array<string|int, mixed>
Parameters
$route : array<string|int, mixed>

The route configuration.

$path : string

The current URL path.

Return values
array<string|int, mixed>

An associative array of arguments extracted from the path.

setup_callback()

Sets up the callback properties for a route.

private setup_callback(Closure|string|array<string|int, mixed> $callback) : mixed
Parameters
$callback : Closure|string|array<string|int, mixed>

The callback configuration.

Return values
mixed

Returns the callable data needed to execute the route callback.


        
On this page

Search results