\Omega\Routing Router

Router class.

The Router class is responsible for managing routes and dispatching requests to the appropriate handlers.

Summary

Methods
Properties
Constants
errorHandler
dispatch
getCurrent
dispatchNotAllowed
dispatchNotFound
dispatchError
redirect
route
get
post
put
delete
patch
options
any
routes
No public constants found
No protected methods found
current
errorHandlers
No protected constants found
getPaths
match
addRoute
No private properties found
No private constants found

Properties

$routes

$routes : array

Routes array.

Type

array<string|int, mixed> — Holds an array of routes.

$current

$current : \Omega\Routing\Route

Current route.

Type

Route — Holds an instance of Route.

$errorHandlers

$errorHandlers : array

Error handler.

Type

array<string|int, mixed> — Holds an array of error handler.

Methods

errorHandler()

errorHandler(int  $code, callable  $handler) : void

Adds an error handler for a specific HTTP error code.

Parameters

int $code

The HTTP error code.

callable $handler

The error handler.

Returns

void —

dispatch()

dispatch() : mixed

Dispatches the router, matching the current request and executing the corresponding route handler.

Returns

mixed —

The result of the route handler.

getCurrent()

getCurrent() : ?\Omega\Routing\Route

Gets the current route being dispatched.

Returns

?\Omega\Routing\Route —

Return an instance of Route or null if no route is matched.

dispatchNotAllowed()

dispatchNotAllowed() : mixed

Dispatch a not allowed response.

Returns

mixed —

dispatchNotFound()

dispatchNotFound() : mixed

Dispatch a not found response.

Returns

mixed —

dispatchError()

dispatchError() : mixed

Dispatch a server error response.

Returns

mixed —

redirect()

redirect(string  $path) : void

Redirects the request to a new path.

Parameters

string $path

The path to redirect to.

Returns

void —

route()

route(string  $name, array  $parameters = []) : string

Generates a URL for a named route.

Parameters

string $name

The name of the route.

array $parameters

An array of parameters to replace in the route path.

Throws

\Exception

Returns

string —

The generated route URL.

get()

get(string  $path, mixed  $handler, ?string  $name = null) : \Omega\Routing\Route

Get method.

Parameters

string $path

Holds the path pattern for the route.

mixed $handler

Holds the handler for the route.

?string $name

Holds the route name or null.

Returns

\Omega\Routing\Route —

Returns the added Route instance.

post()

post(string  $path, mixed  $handler, ?string  $name = null) : \Omega\Routing\Route

Post method.

Parameters

string $path

Holds the path pattern for the route.

mixed $handler

Holds the handler for the route.

?string $name

Holds the route name or null.

Returns

\Omega\Routing\Route —

Returns the added Route instance.

put()

put(string  $path, mixed  $handler, ?string  $name = null) : \Omega\Routing\Route

Put method.

Parameters

string $path

Holds the path pattern for the route.

mixed $handler

Holds the handler for the route.

?string $name

Holds the route name or null.

Returns

\Omega\Routing\Route —

Returns the added Route instance.

delete()

delete(string  $path, mixed  $handler, ?string  $name = null) : \Omega\Routing\Route

Delete method.

Parameters

string $path

Holds the path pattern for the route.

mixed $handler

Holds the handler for the route.

?string $name

Holds the route name or null.

Returns

\Omega\Routing\Route —

Returns the added Route instance.

patch()

patch(string  $path, mixed  $handler, ?string  $name = null) : \Omega\Routing\Route

Patch method.

Parameters

string $path

Holds the path pattern for the route.

mixed $handler

Holds the handler for the route.

?string $name

Holds the route name or null.

Returns

\Omega\Routing\Route —

Returns the added Route instance.

options()

options(string  $path, mixed  $handler, ?string  $name = null) : \Omega\Routing\Route

Options method.

Parameters

string $path

Holds the path pattern for the route.

mixed $handler

Holds the handler for the route.

?string $name

Holds the route name or null.

Returns

\Omega\Routing\Route —

Returns the added Route instance.

any()

any(string  $path, mixed  $handler, ?string  $name = null) : \Omega\Routing\Route

Any method.

Parameters

string $path

Holds the path pattern for the route.

mixed $handler

Holds the handler for the route.

?string $name

Holds the route name or null.

Returns

\Omega\Routing\Route —

Returns the added Route instance.

getPaths()

getPaths() : array

Gets an array of paths registered in the router.

Returns

array —

Return an array of paths.

match()

match(string  $method, string  $path) : ?\Omega\Routing\Route

Matches a route based on the HTTP method and path.

Parameters

string $method

The HTTP method.

string $path

The path to match.

Returns

?\Omega\Routing\Route —

Return an instance of Route or null if no route is matched.

addRoute()

addRoute(string  $method, string  $path, mixed  $handler, string|null  $name = null) : \Omega\Routing\Route

Add a route to the router.

Parameters

string $method

Holds the HTTP method for the route.

string $path

Holds the path pattern for the route.

mixed $handler

Holds the handler for the route.

string|null $name

Holds the route name or null.

Returns

\Omega\Routing\Route —

Return the added Route instance.