CodeIgniter v4.5 API

MockCodeIgniter extends CodeIgniter
in package

This class is the core of the framework, and will analyse the request, route it to a controller, and send back the response.

Of course, there are variations to that flow, but this is the brains.

Table of Contents

Constants

CI_VERSION  = '4.5.5'
The current version of CodeIgniter Framework

Properties

$benchmark  : Timer
Timer instance.
$bufferLevel  : int
Application output buffering level
$cacheTTL  : int
Cache expiration time
$config  : App
Main application configuration
$context  : string|null
Context web: Invoked by HTTP request php-cli: Invoked by CLI via `php public/index.php`
$controller  : (callable|string)|string|null
Controller to use.
$enableFilters  : bool
Whether to enable Control Filters.
$method  : string
Controller method to invoke.
$output  : string
Output handler to use.
$pageCache  : ResponseCache
Web Page Caching
$request  : CLIRequest|IncomingRequest|null
Current request.
$response  : ResponseInterface
Current response.
$returnResponse  : bool
Whether to return Response object or send response.
$router  : Router
Router to use.
$startTime  : float|null
App startup time.
$totalTime  : float
Total app execution time

Methods

__construct()  : mixed
Constructor.
cache()  : void
Tells the app that the final output should be cached.
cachePage()  : bool
Caches the full response from the current request. Used for full-page caching for very high performance.
disableFilters()  : void
Disables Controller Filters.
displayCache()  : false|ResponseInterface
Determines if a response has been cached for the given URI.
displayPerformanceMetrics()  : string
Replaces the elapsed_time and memory_usage tag.
getPerformanceStats()  : array<string|int, mixed>
Returns an array with our basic performance stats collected.
initialize()  : void
Handles some basic app and environment setup.
run()  : ResponseInterface|void
Launch the application!
setContext()  : $this
Sets the app context.
spoofRequestMethod()  : void
Modifies the Request Object to use a different method if a POST variable called _method is found.
storePreviousURL()  : void
If we have a session object to use, store the current URI as the previous URI. This is called just prior to sending the response to the client, and will make it available next request.
bootstrapEnvironment()  : void
Load any custom boot files based upon the current environment.
callExit()  : void
Exits the application, setting the exit code for CLI-based applications that might be watching.
createController()  : Controller
Instantiates the controller class.
detectEnvironment()  : void
You can load different configurations depending on your current environment. Setting the environment also influences things like logging and error reporting.
determinePath()  : string
Determines the path to use for us to try to route to, based on the CLI/IncomingRequest path.
display404errors()  : ResponseInterface|void
Displays a 404 Page Not Found error. If set, will try to call the 404Override controller/method that was set in routing config.
forceSecureAccess()  : void
Force Secure Site Access? If the config value 'forceGlobalSecureRequests' is true, will enforce that all requests to this site are made through HTTPS. Will redirect the user to the current page with HTTPS, as well as set the HTTP Strict Transport Security header for those browsers that support it.
gatherOutput()  : void
Gathers the script output from the buffer, replaces some execution time tag in the output and displays the debug toolbar, if required.
generateCacheName()  : string
Generates the cache name to use for our full-page caching.
getRequestObject()  : void
Get our Request object, (either IncomingRequest or CLIRequest).
getResponseObject()  : void
Get our Response object, and set some default values, including the HTTP protocol version and a default successful response.
handleRequest()  : ResponseInterface
Handles the main request logic and fires the controller.
initializeKint()  : void
Initializes Kint
outputBufferingEnd()  : string
outputBufferingStart()  : void
resolvePlatformExtensions()  : void
Checks system for missing required PHP extensions.
runController()  : false|ResponseInterface|string|void
Runs the controller, allowing for _remap methods to function.
sendResponse()  : void
Sends the output of this request back to the client.
startBenchmark()  : void
Start the Benchmark
startController()  : ResponseInterface|string|void
Now that everything has been setup, this method attempts to run the controller method and make the script go. If it's not able to, will show the appropriate Page Not Found error.
tryToRouteIt()  : array<int, string>|string|null
Try to Route It - As it sounds like, works with the router to match a route against the current URI. If the route is a "redirect route", will also handle the redirect.
autoloadKint()  : void
configureKint()  : void
isPhpCli()  : bool
Invoked via php-cli command?
isWeb()  : bool
Web access?
runRequiredAfterFilters()  : void
Run required after filters.
runRequiredBeforeFilters()  : ResponseInterface|null
Run required before filters.

Constants

CI_VERSION

The current version of CodeIgniter Framework

public mixed CI_VERSION = '4.5.5'

Properties

$bufferLevel

Application output buffering level

protected int $bufferLevel

$cacheTTL

Cache expiration time

Moved to ResponseCache::$ttl. No longer used.

protected static int $cacheTTL = 0

seconds

$config

Main application configuration

protected App $config

$context

Context web: Invoked by HTTP request php-cli: Invoked by CLI via `php public/index.php`

protected string|null $context = 'web'

$controller

Controller to use.

protected (callable|string)|string|null $controller

$enableFilters

Whether to enable Control Filters.

protected bool $enableFilters = true

$method

Controller method to invoke.

protected string $method

$output

Output handler to use.

protected string $output

$returnResponse

Whether to return Response object or send response.

No longer used.

protected bool $returnResponse = false

$startTime

App startup time.

protected float|null $startTime

$totalTime

Total app execution time

protected float $totalTime

Methods

__construct()

Constructor.

public __construct(App $config) : mixed
Parameters
$config : App

cache()

Tells the app that the final output should be cached.

public static cache(int $time) : void

Moved to ResponseCache::setTtl(). No longer used.

Parameters
$time : int

cachePage()

Caches the full response from the current request. Used for full-page caching for very high performance.

public cachePage(Cache $config) : bool

No longer used.

Parameters
$config : Cache
Return values
bool

disableFilters()

Disables Controller Filters.

public disableFilters() : void

displayCache()

Determines if a response has been cached for the given URI.

public displayCache(Cache $config) : false|ResponseInterface

PageCache required filter is used. No longer used.

The parameter $config is deprecated. No longer used.

Parameters
$config : Cache
Tags
throws
Exception
Return values
false|ResponseInterface

displayPerformanceMetrics()

Replaces the elapsed_time and memory_usage tag.

public displayPerformanceMetrics(string $output) : string

PerformanceMetrics required filter is used. No longer used.

Parameters
$output : string
Return values
string

getPerformanceStats()

Returns an array with our basic performance stats collected.

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

initialize()

Handles some basic app and environment setup.

public initialize() : void

run()

Launch the application!

public run([RouteCollectionInterface|null $routes = null ][, bool $returnResponse = false ]) : ResponseInterface|void

This is "the loop" if you will. The main entry point into the script that gets the required class instances, fires off the filters, tries to route the response, loads the controller and generally makes all the pieces work together.

Parameters
$routes : RouteCollectionInterface|null = null
$returnResponse : bool = false

Used for testing purposes only.

Return values
ResponseInterface|void

setContext()

Sets the app context.

public setContext(string $context) : $this
Parameters
$context : string
Tags
phpstan-param

'php-cli'|'web' $context

Return values
$this

spoofRequestMethod()

Modifies the Request Object to use a different method if a POST variable called _method is found.

public spoofRequestMethod() : void

storePreviousURL()

If we have a session object to use, store the current URI as the previous URI. This is called just prior to sending the response to the client, and will make it available next request.

public storePreviousURL(string|URI $uri) : void

This helps provider safer, more reliable previous_url() detection.

Parameters
$uri : string|URI

bootstrapEnvironment()

Load any custom boot files based upon the current environment.

protected bootstrapEnvironment() : void

Moved to system/bootstrap.php.

If no boot file exists, we shouldn't continue because something is wrong. At the very least, they should have error reporting setup.

callExit()

Exits the application, setting the exit code for CLI-based applications that might be watching.

protected callExit(int $code) : void

No longer Used. Moved to index.php.

Parameters
$code : int

detectEnvironment()

You can load different configurations depending on your current environment. Setting the environment also influences things like logging and error reporting.

protected detectEnvironment() : void

No longer used. Moved to index.php and spark.

This can be set to anything, but default usage is:

development testing production

Tags
codeCoverageIgnore

determinePath()

Determines the path to use for us to try to route to, based on the CLI/IncomingRequest path.

protected determinePath() : string

No longer used.

Return values
string

forceSecureAccess()

Force Secure Site Access? If the config value 'forceGlobalSecureRequests' is true, will enforce that all requests to this site are made through HTTPS. Will redirect the user to the current page with HTTPS, as well as set the HTTP Strict Transport Security header for those browsers that support it.

protected forceSecureAccess([int $duration = 31536000 ]) : void

No longer used. Moved to ForceHTTPS filter.

Parameters
$duration : int = 31536000

How long the Strict Transport Security should be enforced for this URL.

gatherOutput()

Gathers the script output from the buffer, replaces some execution time tag in the output and displays the debug toolbar, if required.

protected gatherOutput([Cache|null $cacheConfig = null ][, ResponseInterface|string|null $returned = null ]) : void

$cacheConfig is deprecated.

Parameters
$cacheConfig : Cache|null = null

Deprecated. No longer used.

$returned : ResponseInterface|string|null = null

generateCacheName()

Generates the cache name to use for our full-page caching.

protected generateCacheName(Cache $config) : string

No longer used.

Parameters
$config : Cache
Return values
string

getRequestObject()

Get our Request object, (either IncomingRequest or CLIRequest).

protected getRequestObject() : void

getResponseObject()

Get our Response object, and set some default values, including the HTTP protocol version and a default successful response.

protected getResponseObject() : void

initializeKint()

Initializes Kint

protected initializeKint() : void

Moved to Autoloader.

outputBufferingEnd()

protected outputBufferingEnd() : string
Return values
string

outputBufferingStart()

protected outputBufferingStart() : void

resolvePlatformExtensions()

Checks system for missing required PHP extensions.

protected resolvePlatformExtensions() : void

Moved to system/bootstrap.php.

Tags
throws
FrameworkException
codeCoverageIgnore

runController()

Runs the controller, allowing for _remap methods to function.

protected runController(Controller $class) : false|ResponseInterface|string|void

CI4 supports three types of requests:

  1. Web: URI segments become parameters, sent to Controllers via Routes, output controlled by Headers to browser
  2. PHP CLI: accessed by CLI via php public/index.php, arguments become URI segments, sent to Controllers via Routes, output varies
Parameters
$class : Controller
Return values
false|ResponseInterface|string|void

sendResponse()

Sends the output of this request back to the client.

protected sendResponse() : void

This is what they've been waiting for!

startBenchmark()

Start the Benchmark

protected startBenchmark() : void

The timer is used to display total script execution both in the debug toolbar, and potentially on the displayed page.

startController()

Now that everything has been setup, this method attempts to run the controller method and make the script go. If it's not able to, will show the appropriate Page Not Found error.

protected startController() : ResponseInterface|string|void
Return values
ResponseInterface|string|void

tryToRouteIt()

Try to Route It - As it sounds like, works with the router to match a route against the current URI. If the route is a "redirect route", will also handle the redirect.

protected tryToRouteIt([RouteCollectionInterface|null $routes = null ]) : array<int, string>|string|null
Parameters
$routes : RouteCollectionInterface|null = null

A collection interface to use in place of the config file.

Tags
throws
RedirectException
Return values
array<int, string>|string|null

Route filters, that is, the filters specified in the routes file

autoloadKint()

private autoloadKint() : void

Moved to Autoloader.

configureKint()

private configureKint() : void

Moved to Autoloader.

isPhpCli()

Invoked via php-cli command?

private isPhpCli() : bool
Return values
bool

isWeb()

Web access?

private isWeb() : bool
Return values
bool

runRequiredAfterFilters()

Run required after filters.

private runRequiredAfterFilters(Filters $filters) : void
Parameters
$filters : Filters

        
On this page

Search results