CodeIgniter v4.6 API

RouteCollectionInterface

Interface RouteCollectionInterface

A Route Collection's sole job is to hold a series of routes. The required number of methods is kept very small on purpose, but implementors may add a number of additional methods to customize how the routes are defined.

Table of Contents

Methods

add()  : RouteCollectionInterface
Adds a single route to the collection.
addPlaceholder()  : RouteCollectionInterface
Registers a new constraint with the system. Constraints are used by the routes as placeholders for regular expressions to make defining the routes more human-friendly.
get404Override()  : callable(string): Array|string|null
Returns the 404 Override setting, which can be null, a closure or the controller/string.
getDefaultController()  : string
Returns the name of the default controller. With Namespace.
getDefaultMethod()  : string
Returns the name of the default method to use within the controller.
getDefaultNamespace()  : string
Returns the default namespace.
getFiltersForRoute()  : array<int, string>
Returns the filters that should be applied for a single route, along with any parameters it might have. Parameters are found by splitting the parameter name on a colon to separate the filter name from the parameter list, and the splitting the result on commas. So:
getHTTPVerb()  : string
Returns the current HTTP Verb being used.
getRedirectCode()  : int
Grabs the HTTP status code from a redirecting Route.
getRoutes()  : array<string|int, mixed>
Returns the raw array of available routes.
getRoutesOptions()  : array<string, int|string>
Returns one or all routes options
isFiltered()  : bool
Checks a route (using the "from") to see if it's filtered or not.
isRedirect()  : bool
Determines if the route is a redirecting route.
reverseRoute()  : false|string
Attempts to look up a route based on its destination.
set404Override()  : self
Sets the class/method that should be called if routing doesn't find a match. It can be either a closure or the controller/method name exactly like a route is defined: Users::index
setAutoRoute()  : self
If TRUE, the system will attempt to match the URI against Controllers by matching each segment against folders/files in APPPATH/Controllers, when a match wasn't found against defined routes.
setDefaultController()  : RouteCollectionInterface
Sets the default controller to use when no other controller has been specified.
setDefaultMethod()  : RouteCollectionInterface
Sets the default method to call on the controller when no other method has been set in the route.
setDefaultNamespace()  : RouteCollectionInterface
Sets the default namespace to use for Controllers when no other namespace has been specified.
setHTTPVerb()  : $this
Sets the current HTTP verb.
setTranslateURIDashes()  : RouteCollectionInterface
Tells the system whether to convert dashes in URI strings into underscores. In some search engines, including Google, dashes create more meaning and make it easier for the search engine to find words and meaning in the URI for better SEO. But it doesn't work well with PHP method names.
shouldAutoRoute()  : bool
Returns the flag that tells whether to autoRoute URI against Controllers.
shouldTranslateURIDashes()  : bool
Returns the current value of the translateURIDashes setting.
shouldUseSupportedLocalesOnly()  : bool
Get the flag that limit or not the routes with {locale} placeholder to App::$supportedLocales

Methods

add()

Adds a single route to the collection.

public add(string $from, array<string|int, mixed>|callable(mixed...): Array|string $to[, array<string|int, mixed>|null $options = null ]) : RouteCollectionInterface
Parameters
$from : string

The route path (with placeholders or regex)

$to : array<string|int, mixed>|callable(mixed...): Array|string

The route handler

$options : array<string|int, mixed>|null = null

The route options

Return values
RouteCollectionInterface

addPlaceholder()

Registers a new constraint with the system. Constraints are used by the routes as placeholders for regular expressions to make defining the routes more human-friendly.

public addPlaceholder(array<string|int, mixed>|string $placeholder[, string|null $pattern = null ]) : RouteCollectionInterface

You can pass an associative array as $placeholder, and have multiple placeholders added at once.

Parameters
$placeholder : array<string|int, mixed>|string
$pattern : string|null = null

The regex pattern

Return values
RouteCollectionInterface

get404Override()

Returns the 404 Override setting, which can be null, a closure or the controller/string.

public get404Override() : callable(string): Array|string|null
Tags
TODO

This method is not related to the route collection. So this should be removed in the future.

Return values
callable(string): Array|string|null

getDefaultController()

Returns the name of the default controller. With Namespace.

public getDefaultController() : string
Tags
TODO

The default controller is only for auto-routing. So this should be removed in the future.

Return values
string

getDefaultMethod()

Returns the name of the default method to use within the controller.

public getDefaultMethod() : string
Return values
string

getDefaultNamespace()

Returns the default namespace.

public getDefaultNamespace() : string
Return values
string

getFiltersForRoute()

Returns the filters that should be applied for a single route, along with any parameters it might have. Parameters are found by splitting the parameter name on a colon to separate the filter name from the parameter list, and the splitting the result on commas. So:

public getFiltersForRoute(string $search[, string|null $verb = null ]) : array<int, string>

'role:admin,manager'

has a filter of "role", with parameters of ['admin', 'manager'].

Parameters
$search : string

routeKey

$verb : string|null = null

HTTP verb like GET,POST or * or CLI.

Return values
array<int, string>

filter_name or filter_name:arguments like 'role:admin,manager'

getHTTPVerb()

Returns the current HTTP Verb being used.

public getHTTPVerb() : string
Return values
string

getRedirectCode()

Grabs the HTTP status code from a redirecting Route.

public getRedirectCode(string $routeKey) : int
Parameters
$routeKey : string
Return values
int

getRoutes()

Returns the raw array of available routes.

public getRoutes([non-empty-string|null $verb = null ][, bool $includeWildcard = true ]) : array<string|int, mixed>
Parameters
$verb : non-empty-string|null = null

HTTP verb like GET,POST or * or CLI.

$includeWildcard : bool = true

Whether to include '*' routes.

Return values
array<string|int, mixed>

getRoutesOptions()

Returns one or all routes options

public getRoutesOptions([string|null $from = null ][, string|null $verb = null ]) : array<string, int|string>
Parameters
$from : string|null = null

The route path (with placeholders or regex)

$verb : string|null = null

HTTP verb like GET,POST or * or CLI.

Return values
array<string, int|string>

[key => value]

isFiltered()

Checks a route (using the "from") to see if it's filtered or not.

public isFiltered(string $search[, string|null $verb = null ]) : bool
Parameters
$search : string
$verb : string|null = null

HTTP verb like GET,POST or * or CLI.

Return values
bool

isRedirect()

Determines if the route is a redirecting route.

public isRedirect(string $routeKey) : bool
Parameters
$routeKey : string
Return values
bool

reverseRoute()

Attempts to look up a route based on its destination.

public reverseRoute(string $search, int|string ...$params) : false|string

If a route exists:

 'path/(:any)/(:any)' => 'Controller::method/$1/$2'

This method allows you to know the Controller and method and get the route that leads to it.

 // Equals 'path/$param1/$param2'
 reverseRoute('Controller::method', $param1, $param2);
Parameters
$search : string

Named route or Controller::method

$params : int|string
Return values
false|string

The route (URI path relative to baseURL) or false if not found.

set404Override()

Sets the class/method that should be called if routing doesn't find a match. It can be either a closure or the controller/method name exactly like a route is defined: Users::index

public set404Override([callable|null $callable = null ]) : self

This setting is passed to the Router class and handled there.

Parameters
$callable : callable|null = null
Tags
TODO

This method is not related to the route collection. So this should be removed in the future.

Return values
self

setAutoRoute()

If TRUE, the system will attempt to match the URI against Controllers by matching each segment against folders/files in APPPATH/Controllers, when a match wasn't found against defined routes.

public setAutoRoute(bool $value) : self

If FALSE, will stop searching and do NO automatic routing.

Parameters
$value : bool
Tags
TODO

This method is only for auto-routing. So this should be removed in the future.

Return values
self

setHTTPVerb()

Sets the current HTTP verb.

public setHTTPVerb(string $verb) : $this
Parameters
$verb : string

HTTP verb

Return values
$this

setTranslateURIDashes()

Tells the system whether to convert dashes in URI strings into underscores. In some search engines, including Google, dashes create more meaning and make it easier for the search engine to find words and meaning in the URI for better SEO. But it doesn't work well with PHP method names.

public setTranslateURIDashes(bool $value) : RouteCollectionInterface

...

Parameters
$value : bool
Tags
TODO

This method is only for auto-routing. So this should be removed in the future.

Return values
RouteCollectionInterface

shouldAutoRoute()

Returns the flag that tells whether to autoRoute URI against Controllers.

public shouldAutoRoute() : bool
Tags
TODO

This method is only for auto-routing. So this should be removed in the future.

Return values
bool

shouldTranslateURIDashes()

Returns the current value of the translateURIDashes setting.

public shouldTranslateURIDashes() : bool
Tags
TODO

This method is only for auto-routing. So this should be removed in the future.

Return values
bool

shouldUseSupportedLocalesOnly()

Get the flag that limit or not the routes with {locale} placeholder to App::$supportedLocales

public shouldUseSupportedLocalesOnly() : bool
Return values
bool

        
On this page

Search results