RouteCollection class

Framework\Routing\RouteCollection

Class RouteCollection.

Implements: Countable, JsonSerializable

Defined in libraries/routing/src/RouteCollection.php

Properties #

$router
-
$origin
-
$name
-
$routes
Array of HTTP Methods as keys and array of Routes as values.
$notFoundAction
The Error 404 page action.

$router

protected \Framework\Routing\Router $router

$origin

protected string $origin

$name

protected ?string $name

$routes

protected array $routes = array ( )

Array of HTTP Methods as keys and array of Routes as values.

$notFoundAction

protected Closure|string $notFoundAction

The Error 404 page action.

Methods #

__construct()
RouteCollection constructor.
__call()
-
__get()
-
__isset()
-
add()
Adds a Route to match many HTTP Methods.
count()
Count routes in the collection.
delete()
Adds a Route to match the HTTP DELETE Method.
get()
Adds a Route to match the HTTP GET Method.
group()
Groups many Routes into a URL path.
jsonSerialize()
-
namespace()
Updates Routes actions, which are strings, prepending a namespace.
notFound()
Sets the Route Not Found action for this collection.
options()
Adds a Route to match the HTTP OPTIONS Method.
patch()
Adds a Route to match the HTTP PATCH Method.
post()
Adds a Route to match the HTTP POST Method.
presenter()
Adds many Routes that can be used by a User Interface.
put()
Adds a Route to match the HTTP PUT Method.
redirect()
Adds a GET Route to match a path and automatically redirects to a URL.
resource()
Adds many Routes that can be used as a REST Resource.
addRoute()
-
addSimple()
-
getRouteName()
Get a Route name.
getRouteNotFound()
Gets the Route Not Found for this collection.
makeRoute()
-
makeRouteActionFromArray()
-
setOrigin()
-

__construct()

public function __construct(\Framework\Routing\Router $router, string $origin, ?string $name = NULL)

RouteCollection constructor.

Parameters
  • \Framework\Routing\Router $router - A Router instance
  • string $origin - URL Origin. A string in the following format: `{scheme}://{hostname}[:{port}]`
  • ?string $name - The collection name

__call()

public function __call(string $method, array $arguments)
Parameters
  • string $method
  • array $arguments
Returns Route|null
throws
BadMethodCallException for method not allowed or method not found

__get()

public function __get(string $property): mixed
Parameters
  • string $property
Returns mixed
throws
Error if cannot access property

__isset()

public function __isset(string $property): bool
Parameters
  • string $property
Returns bool

add()

public function add(array $httpMethods, string $path, Closure|array|string $action, ?string $name = NULL): \Framework\Routing\Route

Adds a Route to match many HTTP Methods.

Parameters
  • array $httpMethods - The HTTP Methods
  • string $path - The URL path
  • Closure|array|string $action - The Route action
  • ?string $name - The Route name
Returns \Framework\Routing\Route

count()

public function count(): int

Count routes in the collection.

Returns int

delete()

public function delete(string $path, Closure|array|string $action, ?string $name = NULL): \Framework\Routing\Route

Adds a Route to match the HTTP DELETE Method.

Parameters
  • string $path - The URL path
  • Closure|array|string $action - The Route action
  • ?string $name - The Route name
Returns \Framework\Routing\Route - The Route added to the collection

get()

public function get(string $path, Closure|array|string $action, ?string $name = NULL): \Framework\Routing\Route

Adds a Route to match the HTTP GET Method.

Parameters
  • string $path - The URL path
  • Closure|array|string $action - The Route action
  • ?string $name - The Route name
Returns \Framework\Routing\Route - The Route added to the collection

group()

public function group(string $basePath, array $routes, array $options = array ( )): array

Groups many Routes into a URL path.

Parameters
  • string $basePath - The URL path to group in
  • array $routes - The Routes to be grouped
  • array $options - Custom options passed to the Routes
Returns array - The same $routes with updated paths and options

jsonSerialize()

public function jsonSerialize(): array
Returns array

namespace()

public function namespace(string $namespace, array $routes): array

Updates Routes actions, which are strings, prepending a namespace.

Parameters
  • string $namespace - The namespace
  • array $routes - The Routes
Returns array - The same $routes with updated actions

notFound()

public function notFound(Closure|string $action): void

Sets the Route Not Found action for this collection.

Parameters
  • Closure|string $action - the Route function to run when no Route path is found for this collection
Returns void

options()

public function options(string $path, Closure|array|string $action, ?string $name = NULL): \Framework\Routing\Route

Adds a Route to match the HTTP OPTIONS Method.

Parameters
  • string $path - The URL path
  • Closure|array|string $action - The Route action
  • ?string $name - The Route name
Returns \Framework\Routing\Route - The Route added to the collection

patch()

public function patch(string $path, Closure|array|string $action, ?string $name = NULL): \Framework\Routing\Route

Adds a Route to match the HTTP PATCH Method.

Parameters
  • string $path - The URL path
  • Closure|array|string $action - The Route action
  • ?string $name - The Route name
Returns \Framework\Routing\Route - The Route added to the collection

post()

public function post(string $path, Closure|array|string $action, ?string $name = NULL): \Framework\Routing\Route

Adds a Route to match the HTTP POST Method.

Parameters
  • string $path - The URL path
  • Closure|array|string $action - The Route action
  • ?string $name - The Route name
Returns \Framework\Routing\Route - The Route added to the collection

presenter()

public function presenter(string $path, string $class, string $baseName, array $except = array ( ), string $placeholder = '{int}'): array

Adds many Routes that can be used by a User Interface.

Parameters
  • string $path - The URL path
  • string $class - The name of the class where the resource will point
  • string $baseName - The base name used as a Route name prefix
  • array $except - Actions not added. Allowed values are: index, new, create, show, edit, update, remove and delete
  • string $placeholder - The placeholder. Normally it matches an id, a number
Returns array - The Routes added to the collection

put()

public function put(string $path, Closure|array|string $action, ?string $name = NULL): \Framework\Routing\Route

Adds a Route to match the HTTP PUT Method.

Parameters
  • string $path - The URL path
  • Closure|array|string $action - The Route action
  • ?string $name - The Route name
Returns \Framework\Routing\Route - The Route added to the collection

redirect()

public function redirect(string $path, string $location, int $code = 307, ?string $name = NULL): \Framework\Routing\Route

Adds a GET Route to match a path and automatically redirects to a URL.

Parameters
  • string $path - The URL path
  • string $location - The URL to redirect
  • int $code - The status code of the response
  • ?string $name - The Route name
Returns \Framework\Routing\Route - The Route added to the collection

resource()

public function resource(string $path, string $class, string $baseName, array $except = array ( ), string $placeholder = '{int}'): array

Adds many Routes that can be used as a REST Resource.

Parameters
  • string $path - The URL path
  • string $class - The name of the class where the resource will point
  • string $baseName - The base name used as a Route name prefix
  • array $except - Actions not added. Allowed values are: index, create, show, update, replace and delete
  • string $placeholder - The placeholder. Normally it matches an id, a number
Returns array - The Routes added to the collection

addRoute()

protected function addRoute(string $httpMethod, \Framework\Routing\Route $route): static
Parameters
  • string $httpMethod
  • \Framework\Routing\Route $route
Returns static
throws
InvalidArgumentException for invalid method

addSimple()

protected function addSimple(string $method, string $path, Closure|array|string $action, ?string $name = NULL): \Framework\Routing\Route
Parameters
  • string $method
  • string $path
  • Closure|array|string $action
  • ?string $name
Returns \Framework\Routing\Route

getRouteName()

protected function getRouteName(string $name): string

Get a Route name.

Parameters
  • string $name - The current Route name
Returns string - The Route name prefixed with the collection name and a dot if it is set

getRouteNotFound()

protected function getRouteNotFound(): ?\Framework\Routing\Route

Gets the Route Not Found for this collection.

Returns ?\Framework\Routing\Route - The Route containing the Not Found Action or null if the Action was not set

makeRoute()

protected function makeRoute(string $path, Closure|array|string $action, ?string $name = NULL): \Framework\Routing\Route
Parameters
  • string $path
  • Closure|array|string $action
  • ?string $name
Returns \Framework\Routing\Route

makeRouteActionFromArray()

protected function makeRouteActionFromArray(array $action): string
Parameters
  • array $action
Returns string

setOrigin()

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