Route class

Framework\Routing\Route

Class Route.

Implements: JsonSerializable

Defined in libraries/routing/src/Route.php

Properties #

$router
-
$origin
-
$path
-
$action
-
$actionArguments
-
$name
-
$options
-

$router

protected \Framework\Routing\Router $router

$origin

protected string $origin

$path

protected string $path

$action

protected Closure|string $action

$actionArguments

protected array $actionArguments = array ( )

$name

protected ?string $name = NULL

$options

protected array $options = array ( )

Methods #

__construct()
Route constructor.
getAction()
Gets the Route action.
getActionArguments()
Gets the Route action arguments.
getName()
Gets the Route name.
getOptions()
Gets Route options.
getOrigin()
Gets the Route URL origin.
getPath()
Gets the Route URL path.
getUrl()
Gets the Route URL.
jsonSerialize()
-
run()
Runs the Route action.
setAction()
Sets the Route action.
setActionArguments()
Sets the Route action arguments.
setName()
Sets the Route name.
setOptions()
Sets options to be used in a specific environment application. For example: its possible set Access Control List options, Locations, Middleware filters, etc.
setPath()
Sets the Route URL path.
extractMethodAndArguments()
-
makeResponse()
Make the final Response used in the 'run' method.
makeResponseBodyPart()
Make a string to be appended in the Response body based in the route action result.
onNamedRoutePart()
-
setOrigin()
-
toArrayOfStrings()
-

__construct()

public function __construct(\Framework\Routing\Router $router, string $origin, string $path, Closure|string $action)

Route constructor.

Parameters
  • \Framework\Routing\Router $router - A Router instance
  • string $origin - URL Origin. A string in the following format: {scheme}://{hostname}[:{port}]
  • string $path - URL Path. A string starting with '/'
  • Closure|string $action - The action

getAction()

public function getAction(): Closure|string

Gets the Route action.

Returns Closure|string

getActionArguments()

public function getActionArguments(): array

Gets the Route action arguments.

Returns array

getName()

public function getName(): ?string

Gets the Route name.

Returns ?string

getOptions()

public function getOptions(): array

Gets Route options.

Returns array

getOrigin()

public function getOrigin(string ...$arguments): string

Gets the Route URL origin.

Parameters
  • string $arguments
Returns string

getPath()

public function getPath(string ...$arguments): string

Gets the Route URL path.

Parameters
  • string $arguments
Returns string

getUrl()

public function getUrl(array $originArgs = array ( ), array $pathArgs = array ( )): string

Gets the Route URL.

Note: Arguments must be passed if placeholders need to be filled.

Parameters
  • array $originArgs - Arguments to fill the URL Origin placeholders
  • array $pathArgs - Arguments to fill the URL Path placeholders
Returns string

jsonSerialize()

public function jsonSerialize(): string
Returns string

run()

public function run(mixed ...$construct): \Framework\HTTP\Response

Runs the Route action.

Parameters
  • mixed $construct
Returns \Framework\HTTP\Response - The Response with the action result appended on the body
throws
JsonException if the action result is an array, or an instance of JsonSerializable, and the Response cannot be set as JSON
throws
RoutingException if class is not an instance of {@see RouteActions}, action method not exists or if the result of the action method has not a valid type

setAction()

public function setAction(Closure|string $action): static

Sets the Route action.

Parameters
  • Closure|string $action - A Closure or a string in the format of the `__METHOD__` constant. Example: `App\Blog::show`. The action can be suffixed with ordered parameters, separated by slashes, to set how the arguments will be passed to the class method. Example: `App\Blog::show/0/2/1`. And, also with the asterisk wildcard, to pass all arguments in the incoming order. Example: `App\Blog::show/*`
Returns static

setActionArguments()

public function setActionArguments(array $arguments): static

Sets the Route action arguments.

Parameters
  • array $arguments - The arguments. Note that the indexes set the order of how the arguments are passed to the Action
Returns static

setName()

public function setName(string $name): static

Sets the Route name.

Parameters
  • string $name
Returns static

setOptions()

public function setOptions(array $options): static

Sets options to be used in a specific environment application. For example: its possible set Access Control List options, Locations, Middleware filters, etc.

Parameters
  • array $options
Returns static

setPath()

public function setPath(string $path): static

Sets the Route URL path.

Parameters
  • string $path
Returns static

extractMethodAndArguments()

protected function extractMethodAndArguments(string $part): array
Parameters
  • string $part - An action part like: index/0/2/1
Returns array - The action method in the first index, the action arguments in the second
throws
InvalidArgumentException for undefined action argument

makeResponse()

protected function makeResponse(mixed $result): \Framework\HTTP\Response

Make the final Response used in the 'run' method.

Parameters
  • mixed $result
Returns \Framework\HTTP\Response
throws
JsonException if the $result is an array, or an instance of JsonSerializable, and the Response cannot be set as JSON
throws
RoutingException if the $result type is invalid

makeResponseBodyPart()

protected function makeResponseBodyPart(mixed $result): string

Make a string to be appended in the Response body based in the route action result.

Parameters
  • mixed $result - The return value of the matched route action
Returns string
throws
JsonException if the $result is an array, or an instance of JsonSerializable, and the Response cannot be set as JSON
throws
RoutingException if the $result type is invalid

onNamedRoutePart()

protected function onNamedRoutePart(): string
Returns string

setOrigin()

protected function setOrigin(string $origin): static
Parameters
  • string $origin
Returns static

toArrayOfStrings()

protected static function toArrayOfStrings(array $array): array
Parameters
  • array $array
Returns array