CodeIgniter v4.5 API

AutoRouterImproved
in package
implements AutoRouterInterface

FinalYes

New Secure Router for Auto-Routing

Tags
see
AutoRouterImprovedTest

Table of Contents

Interfaces

AutoRouterInterface
Expected behavior of a AutoRouter.

Properties

$controller  : string
The name of the controller class.
$controllerPos  : int|null
The position of the Controller in the URI segments.
$defaultController  : string
$defaultMethod  : string
The name of the default method without HTTP verb prefix.
$directory  : string|null
Sub-directory that contains the requested controller class.
$method  : string
The name of the method to use.
$methodPos  : int|null
The position of the Method in the URI segments.
$moduleRoutes  : array<string|int, mixed>
Map of URI segments and namespaces.
$namespace  : string
The namespace for controllers.
$paramPos  : int|null
The position of the first Parameter in the URI segments.
$params  : array<int, string>
An array of params to the controller method.
$protectedControllers  : array<string|int, mixed>
List of controllers in Defined Routes that should not be accessed via this Auto-Routing.
$segments  : array<int, string>
The URI segments.
$translateURIDashes  : bool
Whether dashes in URI's should be converted to underscores when determining method names.
$translateUriToCamelCase  : bool
Whether to translate dashes in URIs for controller/method to CamelCase.
$uri  : string|null
The current URI

Methods

__construct()  : mixed
getRoute()  : array<string|int, mixed>
Finds controller, method and params from the URI.
checkParameters()  : void
checkRemap()  : void
checkUnderscore()  : void
checkUriForController()  : void
Check URI for controller for $translateUriToCamelCase
checkUriForMethod()  : void
Check URI for method for $translateUriToCamelCase
createSegments()  : array<string|int, mixed>
isValidSegment()  : bool
Returns true if the supplied $segment string represents a valid PSR-4 compliant namespace/directory segment
protectDefinedRoutes()  : void
searchFirstController()  : bool
Search for the first controller corresponding to the URI segment.
searchLastDefaultController()  : bool
Search for the last default controller corresponding to the URI segments.
setDirectory()  : void
Get the directory path from the controller and set it to the property.
translateURI()  : string
Translates URI segment to CamelCase or replaces `-` with `_`.

Properties

$controllerPos

The position of the Controller in the URI segments.

private int|null $controllerPos = null

Null for the default controller.

$defaultMethod read-only

The name of the default method without HTTP verb prefix.

private string $defaultMethod

$directory

Sub-directory that contains the requested controller class.

private string|null $directory = null

$methodPos

The position of the Method in the URI segments.

private int|null $methodPos = null

Null for the default method.

$moduleRoutes

Map of URI segments and namespaces.

private array<string|int, mixed> $moduleRoutes

The key is the first URI segment. The value is the controller namespace. E.g., [ 'blog' => 'Acme\Blog\Controllers', ]

[ uri_segment => namespace ]

$paramPos

The position of the first Parameter in the URI segments.

private int|null $paramPos = null

Null for the no parameters.

$params

An array of params to the controller method.

private array<int, string> $params = []

$protectedControllers read-only

List of controllers in Defined Routes that should not be accessed via this Auto-Routing.

private array<string|int, mixed> $protectedControllers

$translateURIDashes read-only

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

private bool $translateURIDashes

$translateUriToCamelCase read-only

Whether to translate dashes in URIs for controller/method to CamelCase.

private bool $translateUriToCamelCase

E.g., blog-controller -> BlogController

Methods

__construct()

public __construct(array<int, class-string> $protectedControllers, string $namespace, string $defaultController, string $defaultMethod, bool $translateURIDashes) : mixed
Parameters
$protectedControllers : array<int, class-string>
$namespace : string
$defaultController : string

Short classname

$defaultMethod : string
$translateURIDashes : bool

getRoute()

Finds controller, method and params from the URI.

public getRoute(string $uri, string $httpVerb) : array<string|int, mixed>
Parameters
$uri : string
$httpVerb : string

HTTP verb like GET,POST

Return values
array<string|int, mixed>

[directory_name, controller_name, controller_method, params]

checkUriForController()

Check URI for controller for $translateUriToCamelCase

private checkUriForController(string $classname) : void
Parameters
$classname : string

Controller classname that is generated from URI. The case may be a bit incorrect.

checkUriForMethod()

Check URI for method for $translateUriToCamelCase

private checkUriForMethod(string $method) : void
Parameters
$method : string

Controller method name that is generated from URI. The case may be a bit incorrect.

createSegments()

private createSegments(string $uri) : array<string|int, mixed>
Parameters
$uri : string
Return values
array<string|int, mixed>

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

searchFirstController()

Search for the first controller corresponding to the URI segment.

private searchFirstController() : bool

If there is a controller corresponding to the first segment, the search ends there. The remaining segments are parameters to the controller.

Return values
bool

true if a controller class is found.

searchLastDefaultController()

Search for the last default controller corresponding to the URI segments.

private searchLastDefaultController() : bool
Return values
bool

true if a controller class is found.

setDirectory()

Get the directory path from the controller and set it to the property.

private setDirectory() : void

translateURI()

Translates URI segment to CamelCase or replaces `-` with `_`.

private translateURI(string $segment) : string
Parameters
$segment : string
Return values
string

        
On this page

Search results