CodeIgniter v4.5 API

Router
in package
implements RouterInterface

Request router.

Tags
see
RouterTest

Table of Contents

Interfaces

RouterInterface
Expected behavior of a Router.

Constants

HTTP_METHODS  = [\CodeIgniter\HTTP\Method::GET, \CodeIgniter\HTTP\Method::HEAD, \CodeIgniter\HTTP\Method::POST, \CodeIgniter\HTTP\Method::PATCH, \CodeIgniter\HTTP\Method::PUT, \CodeIgniter\HTTP\Method::DELETE, \CodeIgniter\HTTP\Method::OPTIONS, \CodeIgniter\HTTP\Method::TRACE, \CodeIgniter\HTTP\Method::CONNECT, 'CLI']
List of allowed HTTP methods (and CLI for command line use).

Properties

$autoRouter  : AutoRouterInterface|null
$collection  : RouteCollectionInterface
A RouteCollection instance.
$controller  : callable(mixed...): Array|string
The name of the controller class.
$detectedLocale  : string
The locale that was detected in a route.
$directory  : string|null
Sub-directory that contains the requested controller class.
$filtersInfo  : array<int, string>
The filter info from Route Collection if the matched route should be filtered.
$indexPage  : string
The name of the front controller.
$matchedRoute  : array<string|int, mixed>|null
The route that was matched for this request.
$matchedRouteOptions  : array<string|int, mixed>|null
The options set for the matched route.
$method  : string
The name of the method to use.
$params  : array<string|int, mixed>
An array of binds that were collected so they can be sent to closure routes.
$permittedURIChars  : string
Permitted URI chars
$translateURIDashes  : bool
Whether dashes in URI's should be converted to underscores when determining method names.

Methods

__construct()  : mixed
Stores a reference to the RouteCollection object.
autoRoute()  : void
Checks Auto Routes.
controllerName()  : callable(mixed...): Array|string
Returns the name of the matched controller or closure.
directory()  : string
Returns the name of the sub-directory the controller is in, if any. Relative to APPPATH.'Controllers'.
get404Override()  : mixed
Returns the 404 Override settings from the Collection.
getFilters()  : array<int, string>
Returns the filter info for the matched route, if any.
getLocale()  : string
Returns the detected locale, if any, or null.
getMatchedRoute()  : array<string|int, mixed>|null
Returns the routing information that was matched for this request, if a route was defined.
getMatchedRouteOptions()  : array<string|int, mixed>|null
Returns all options set for the matched route
handle()  : callable(mixed...): Array|string
Finds the controller corresponding to the URI.
hasLocale()  : bool
Returns true/false based on whether the current route contained a {locale} placeholder.
methodName()  : string
Returns the name of the method to run in the chosen controller.
params()  : array<string|int, mixed>
Returns the binds that have been matched and collected during the parsing process as an array, ready to send to instance->method(...$params).
setDirectory()  : void
Sets the sub-directory that the controller is in.
setIndexPage()  : RouterInterface
Sets the value that should be used to match the index.php file. Defaults to index.php but this allows you to modify it in case you are using something like mod_rewrite to remove the page. This allows you to set it a blank.
setTranslateURIDashes()  : self
Tells the system whether we should translate URI dashes or not in the URI from a dash to an underscore.
checkRoutes()  : bool
Checks Defined Routes.
scanControllers()  : array<string|int, mixed>
Scans the controller directory, attempting to locate a controller matching the supplied uri $segments
setDefaultController()  : void
Sets the default controller based on the info set in the RouteCollection.
setMatchedRoute()  : void
setRequest()  : void
Set request route
validateRequest()  : array<string|int, mixed>
Scans the controller directory, attempting to locate a controller matching the supplied uri $segments
checkDisallowedChars()  : void
Checks disallowed characters
isValidSegment()  : bool
Returns true if the supplied $segment string represents a valid PSR-4 compliant namespace/directory segment
replaceBackReferences()  : string
Replace string `$n` with `$matches[n]` value.

Constants

HTTP_METHODS

List of allowed HTTP methods (and CLI for command line use).

public mixed HTTP_METHODS = [\CodeIgniter\HTTP\Method::GET, \CodeIgniter\HTTP\Method::HEAD, \CodeIgniter\HTTP\Method::POST, \CodeIgniter\HTTP\Method::PATCH, \CodeIgniter\HTTP\Method::PUT, \CodeIgniter\HTTP\Method::DELETE, \CodeIgniter\HTTP\Method::OPTIONS, \CodeIgniter\HTTP\Method::TRACE, \CodeIgniter\HTTP\Method::CONNECT, 'CLI']

Properties

$controller

The name of the controller class.

protected callable(mixed...): Array|string $controller

$detectedLocale

The locale that was detected in a route.

protected string $detectedLocale

$directory

Sub-directory that contains the requested controller class.

protected string|null $directory

Primarily used by 'autoRoute'.

$filtersInfo

The filter info from Route Collection if the matched route should be filtered.

protected array<int, string> $filtersInfo = []

$indexPage

The name of the front controller.

protected string $indexPage = 'index.php'

$matchedRoute

The route that was matched for this request.

protected array<string|int, mixed>|null $matchedRoute

$matchedRouteOptions

The options set for the matched route.

protected array<string|int, mixed>|null $matchedRouteOptions

$method

The name of the method to use.

protected string $method

$params

An array of binds that were collected so they can be sent to closure routes.

protected array<string|int, mixed> $params = []

$permittedURIChars

Permitted URI chars

protected string $permittedURIChars = ''

The default value is '' (do not check) for backward compatibility.

$translateURIDashes

Whether dashes in URI's should be converted to underscores when determining method names.

protected bool $translateURIDashes = false

Methods

autoRoute()

Checks Auto Routes.

public autoRoute(string $uri) : void

Attempts to match a URI path against Controllers and directories found in APPPATH/Controllers, to find a matching route.

Parameters
$uri : string

controllerName()

Returns the name of the matched controller or closure.

public controllerName() : callable(mixed...): Array|string
Return values
callable(mixed...): Array|string

Controller classname or Closure

directory()

Returns the name of the sub-directory the controller is in, if any. Relative to APPPATH.'Controllers'.

public directory() : string

Only used when auto-routing is turned on.

Return values
string

get404Override()

Returns the 404 Override settings from the Collection.

public get404Override() : mixed

If the override is a string, will split to controller/index array.

getFilters()

Returns the filter info for the matched route, if any.

public getFilters() : array<int, string>
Return values
array<int, string>

getLocale()

Returns the detected locale, if any, or null.

public getLocale() : string
Return values
string

getMatchedRoute()

Returns the routing information that was matched for this request, if a route was defined.

public getMatchedRoute() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null

getMatchedRouteOptions()

Returns all options set for the matched route

public getMatchedRouteOptions() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null

handle()

Finds the controller corresponding to the URI.

public handle([string|null $uri = null ]) : callable(mixed...): Array|string
Parameters
$uri : string|null = null

URI path relative to baseURL

Tags
throws
BadRequestException
throws
PageNotFoundException
throws
RedirectException
Return values
callable(mixed...): Array|string

Controller classname or Closure

hasLocale()

Returns true/false based on whether the current route contained a {locale} placeholder.

public hasLocale() : bool
Return values
bool

methodName()

Returns the name of the method to run in the chosen controller.

public methodName() : string
Return values
string

params()

Returns the binds that have been matched and collected during the parsing process as an array, ready to send to instance->method(...$params).

public params() : array<string|int, mixed>
Return values
array<string|int, mixed>

setDirectory()

Sets the sub-directory that the controller is in.

public setDirectory([string|null $dir = null ][, bool $append = false ][, bool $validate = true ]) : void

This method should be removed.

Parameters
$dir : string|null = null
$append : bool = false
$validate : bool = true

if true, checks to make sure $dir consists of only PSR4 compliant segments

setIndexPage()

Sets the value that should be used to match the index.php file. Defaults to index.php but this allows you to modify it in case you are using something like mod_rewrite to remove the page. This allows you to set it a blank.

public setIndexPage(string $page) : RouterInterface
Parameters
$page : string
Return values
RouterInterface

setTranslateURIDashes()

Tells the system whether we should translate URI dashes or not in the URI from a dash to an underscore.

public setTranslateURIDashes([bool $val = false ]) : self

This method should be removed.

Parameters
$val : bool = false
Return values
self

checkRoutes()

Checks Defined Routes.

protected checkRoutes(string $uri) : bool

Compares the uri string against the routes that the RouteCollection class defined for us, attempting to find a match. This method will modify $this->controller, etal as needed.

Parameters
$uri : string

The URI path to compare against the routes

Tags
throws
RedirectException
Return values
bool

Whether the route was matched or not.

scanControllers()

Scans the controller directory, attempting to locate a controller matching the supplied uri $segments

protected scanControllers(array<string|int, mixed> $segments) : array<string|int, mixed>

Not used. Moved to AutoRouter class.

Parameters
$segments : array<string|int, mixed>

URI segments

Return values
array<string|int, mixed>

returns an array of remaining uri segments that don't map onto a directory

setDefaultController()

Sets the default controller based on the info set in the RouteCollection.

protected setDefaultController() : void

This was an unnecessary method, so it is no longer used.

setMatchedRoute()

protected setMatchedRoute(string $route, callable|string $handler) : void
Parameters
$route : string
$handler : callable|string

setRequest()

Set request route

protected setRequest([array<string|int, mixed> $segments = [] ]) : void

Takes an array of URI segments as input and sets the class/method to be called.

Parameters
$segments : array<string|int, mixed> = []

URI segments

validateRequest()

Scans the controller directory, attempting to locate a controller matching the supplied uri $segments

protected validateRequest(array<string|int, mixed> $segments) : array<string|int, mixed>

this function name does not properly describe its behavior so it has been deprecated

Parameters
$segments : array<string|int, mixed>

URI segments

Tags
codeCoverageIgnore
Return values
array<string|int, mixed>

returns an array of remaining uri segments that don't map onto a directory

checkDisallowedChars()

Checks disallowed characters

private checkDisallowedChars(string $uri) : void
Parameters
$uri : string

isValidSegment()

Returns true if the supplied $segment string represents a valid PSR-4 compliant namespace/directory segment

private isValidSegment(string $segment) : bool

Moved to AutoRouter class.

regex comes from https://www.php.net/manual/en/language.variables.basics.php

Parameters
$segment : string
Return values
bool

replaceBackReferences()

Replace string `$n` with `$matches[n]` value.

private replaceBackReferences(string $input, array<string|int, mixed> $matches) : string
Parameters
$input : string
$matches : array<string|int, mixed>
Return values
string

        
On this page

Search results