RouterInterface
in
Handles the routing
Every web application needs a router for handling routes. A route is a HTTP method and path for a specific action.
Example routes:
- GET /books - returns a list of books
- GET /books/123 - returns the details of a book with ID 123
- POST /books/123/save - saves the details of a book with ID 123
Table of Contents
- add() : void
- Stores a route with a callable
- addPrefixVariable() : int
- Adds a prefix variable callable for all of the routes
- currentRoute() : string
- Returns with the current route
- currentSegment() : mixed
- Returns with a segment value of the current route by index
- matchCurrentRoute() : array<string|int, mixed>
- Matches the current route and returns with the associated callable and parameters
- prefixVariables() : array<string|int, mixed>
- routes() : array<string|int, mixed>
- Returns with all the stored routes
- url() : string
- Returns with a URL for the given route
Methods
add()
Stores a route with a callable
public
add(string $route, callable|array<string|int, mixed> $callable[, string $method = 'GET' ]) : void
The $callable can use the [ExampleClass::class, 'exampleMethod'] format too.
The route can have variables with question mark: '/route/?' then the callable method in this case must have one parameter!
The $method can be any of the HTTP methods or BOTH. BOTH will add the route to the GET and to the POST as well.
For example, the route is GET /books/?:
$router->add('/books/?', [BooksController::class, 'view']);
then the callable should look like
class BooksController {
function view($id) {
}
}
Parameters
- $route : string
- $callable : callable|array<string|int, mixed>
- $method : string = 'GET'
Return values
void —addPrefixVariable()
Adds a prefix variable callable for all of the routes
public
addPrefixVariable(callable|array<string|int, mixed> $callable) : int
For example, if you have a prefix variable that calls the Translation::locale() method
and then you call the url method with '/something' and you configured the app.base_url
to 'https://example.com' it will return with
https://example.com/en/something
Parameters
- $callable : callable|array<string|int, mixed>
Tags
Return values
int —The segment index of the newly added prefix variable
currentRoute()
Returns with the current route
public
currentRoute() : string
The route HTTP query parameter name can be configured with the router.route_parameter.
If no parameter exists the default will be the home route '/'
Tags
Return values
string —currentSegment()
Returns with a segment value of the current route by index
public
currentSegment(int $index[, mixed $default = null ]) : mixed
Parameters
- $index : int
- $default : mixed = null
Return values
mixed —matchCurrentRoute()
Matches the current route and returns with the associated callable and parameters
public
matchCurrentRoute() : array<string|int, mixed>
For example, the current route is '/books/123/comments/45', the stored route is '/books/?/comments/?' and it matches then the result will be:
[the stored callable for the route, ['123', '45']]
If no match for the current route it will return with ROUTE_NOT_FOUND alias [null, null]
Return values
array<string|int, mixed> —The associated callable and parameters with the current route in [callable, [parameters]] format
prefixVariables()
public
prefixVariables() : array<string|int, mixed>
Return values
array<string|int, mixed> —routes()
Returns with all the stored routes
public
routes() : array<string|int, mixed>
The result format will be ['HTTP method' => ['/route' => callable]]
Return values
array<string|int, mixed> —url()
Returns with a URL for the given route
public
url([string|null $route = null ][, array<string|int, mixed> $params = [] ][, string $amp = '&' ]) : string
Heavily depends on the configuration of the application.
For example the parameters are the following:
-
$route= '/books/123' -
$params= ['name' => 'joe'] -
$amp= '&'
and the application config is the following:
app.base_url = "http://example.com" router.use_rewrite = false router.index_file = "index.php" router.route_parameter = "route"
then the result will be:
http://example.com/index.php?route=/books/123&name=joe
If the router.use_rewrite set to true, the router.index_file and the router.route_parameter will not be
in the result, but for this you have to configure your webserver to redirect non existing
file & directory HTTP queries to your /index.php?route={URI}
http://example.com/books/123?name=joe
If you have a prefix variable added (usually locale) and that has the value 'en', the result will be:
http://example.com/en/books/123?name=joe
Parameters
- $route : string|null = null
- $params : array<string|int, mixed> = []
- $amp : string = '&'