CodeIgniter v4.5 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.

The RouteCollection provides the Router with the routes so that it can determine which controller should be run.

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.
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.
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.
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
Return values
callable(string): Array|string|null

getDefaultController()

Returns the name of the default controller. With Namespace.

public getDefaultController() : string
Return values
string

getDefaultMethod()

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

public getDefaultMethod() : string
Return values
string

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() : array<string|int, mixed>
Return values
array<string|int, mixed>

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
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
Return values
self

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
Return values
RouteCollectionInterface

shouldAutoRoute()

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

public shouldAutoRoute() : bool
Return values
bool

shouldTranslateURIDashes()

Returns the current value of the translateURIDashes setting.

public shouldTranslateURIDashes() : bool
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