Router
in package
implements
RouterInterface
Request router.
Interfaces, Classes and Traits
- RouterInterface
- Expected behavior of a Router.
Table of Contents
- $collection : RouteCollectionInterface
- A RouteCollection instance.
- $controller : 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.
- $filterInfo : string|null
- 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.
- $translateURIDashes : bool
- Whether dashes in URI's should be converted to underscores when determining method names.
- __construct() : mixed
- Stores a reference to the RouteCollection object.
- autoRoute() : mixed
- Attempts to match a URI path against Controllers and directories found in APPPATH/Controllers, to find a matching route.
- controllerName() : mixed
- Returns the name of the matched controller.
- 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.
- getFilter() : 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() : mixed|string
- Scans the URI and attempts to match the current URI to the one of the defined routes in the RouteCollection.
- 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 container.
- 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() : mixed
- Sets the sub-directory that the controller is in.
- setIndexPage() : $this
- 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 your are using something like mod_rewrite to remove the page. This allows you to set it a blank.
- setTranslateURIDashes() : $this
- Tells the system whether we should translate URI dashes or not in the URI from a dash to an underscore.
- checkRoutes() : bool
- Compares the uri string against the routes that the RouteCollection class defined for us, attempting to find a match.
- scanControllers() : array<string|int, mixed>
- Scans the controller directory, attempting to locate a controller matching the supplied uri $segments
- setDefaultController() : mixed
- Sets the default controller based on the info set in the RouteCollection.
- setRequest() : mixed
- Set request route
- validateRequest() : array<string|int, mixed>
- Scans the controller directory, attempting to locate a controller matching the supplied uri $segments
- isValidSegment() : bool
- Returns true if the supplied $segment string represents a valid PSR-4 compliant namespace/directory segment
Properties
$collection
A RouteCollection instance.
protected
RouteCollectionInterface
$collection
$controller
The name of the controller class.
protected
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'.
$filterInfo
The filter info from Route Collection if the matched route should be filtered.
protected
string|null
$filterInfo
$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
= []
$translateURIDashes
Whether dashes in URI's should be converted to underscores when determining method names.
protected
bool
$translateURIDashes
= false
Methods
__construct()
Stores a reference to the RouteCollection object.
public
__construct(RouteCollectionInterface $routes[, Request $request = null ]) : mixed
Parameters
- $routes : RouteCollectionInterface
- $request : Request = null
Return values
mixed —autoRoute()
Attempts to match a URI path against Controllers and directories found in APPPATH/Controllers, to find a matching route.
public
autoRoute(string $uri) : mixed
Parameters
- $uri : string
Return values
mixed —controllerName()
Returns the name of the matched controller.
public
controllerName() : mixed
Return values
mixed —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.
Return values
mixed —getFilter()
Returns the filter info for the matched route, if any.
public
getFilter() : string
Return values
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()
Scans the URI and attempts to match the current URI to the one of the defined routes in the RouteCollection.
public
handle([string|null $uri = null ]) : mixed|string
Parameters
- $uri : string|null = null
Tags
Return values
mixed|string —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 container.
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 ]) : mixed
Parameters
- $dir : string|null = null
- $append : bool = false
- $validate : bool = true
-
if true, checks to make sure $dir consists of only PSR4 compliant segments
Return values
mixed —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 your are using something like mod_rewrite to remove the page. This allows you to set it a blank.
public
setIndexPage(string $page) : $this
Parameters
- $page : string
Return values
$this —setTranslateURIDashes()
Tells the system whether we should translate URI dashes or not in the URI from a dash to an underscore.
public
setTranslateURIDashes([bool|false $val = false ]) : $this
Parameters
- $val : bool|false = false
Return values
$this —checkRoutes()
Compares the uri string against the routes that the RouteCollection class defined for us, attempting to find a match.
protected
checkRoutes(string $uri) : bool
This method will modify $this->controller, etal as needed.
Parameters
- $uri : string
-
The URI path to compare against the routes
Tags
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>
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() : mixed
Return values
mixed —setRequest()
Set request route
protected
setRequest([array<string|int, mixed> $segments = [] ]) : mixed
Takes an array of URI segments as input and sets the class/method to be called.
Parameters
- $segments : array<string|int, mixed> = []
-
URI segments
Return values
mixed —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>
Parameters
- $segments : array<string|int, mixed>
-
URI segments
Tags
Return values
array<string|int, mixed> —returns an array of remaining uri segments that don't map onto a directory
isValidSegment()
Returns true if the supplied $segment string represents a valid PSR-4 compliant namespace/directory segment
private
isValidSegment(string $segment) : bool
regex comes from https://www.php.net/manual/en/language.variables.basics.php
Parameters
- $segment : string