CodeIgniter v4.5 API

Services extends BaseService
in package

Services Configuration file.

Services are simply other classes/libraries that the system uses to do its job. This is used by CodeIgniter to allow the core of the framework to be swapped out easily without affecting the usage within the rest of your application.

This is used in place of a Dependency Injection container primarily due to its simplicity, which allows a better long-term maintenance of the applications built on top of CodeIgniter. A bonus side-effect is that IDEs are able to determine what class you are calling whereas with DI Containers there usually isn't a way for them to do this.

Tags
see
http://blog.ircmaxell.com/2015/11/simple-easy-risk-and-change.html
see
http://www.infoq.com/presentations/Simple-Made-Easy
see
ServicesTest

Table of Contents

Properties

$discovered  : bool
Have we already discovered other Services?
$factories  : array<string, callable(mixed ...$params): object>
Factory method list.
$instances  : array<string, object>
Cache for instance of any services that have been requested as a "shared" instance.
$mocks  : array<string, object>
Mock objects for testing which are returned if exist.
$services  : array<string|int, mixed>
A cache of other service classes we've found.
$serviceNames  : array<int, string>
A cache of the names of services classes found.

Methods

__callStatic()  : object|null
Provides the ability to perform case-insensitive calling of service names.
autoloader()  : Autoloader
The Autoloader class is the central class that handles our spl_autoload_register method, and helper methods.
cache()  : CacheInterface
cache()  : CacheInterface
The cache class provides a simple way to store and retrieve complex data for later.
clirequest()  : CLIRequest
codeigniter()  : CodeIgniter
codeigniter()  : CodeIgniter
CodeIgniter, the core of the framework.
commands()  : Commands
commands()  : Commands
The commands utility for running and working with CLI commands.
createRequest()  : void
csp()  : ContentSecurityPolicy
csp()  : ContentSecurityPolicy
Content Security Policy
curlrequest()  : CURLRequest
curlrequest()  : CURLRequest
The CURL Request class acts as a simple HTTP client for interacting with other servers, typically through APIs.
email()  : Email
email()  : Email
The Email class allows you to send email via mail, sendmail, SMTP.
encrypter()  : EncrypterInterface
encrypter()  : EncrypterInterface
The Encryption class provides two-way encryption.
exceptions()  : Exceptions
exceptions()  : Exceptions
The Exceptions class holds the methods that handle:
filters()  : Filters
filters()  : Filters
Filters allow you to run tasks before and/or after a controller is executed. During before filters, the request can be modified, and actions taken based on the request, while after filters can act on or modify the response itself before it is sent to the client.
format()  : Format
format()  : Format
The Format class is a convenient place to create Formatters.
get()  : object|null
Simple method to get an entry fast.
honeypot()  : Honeypot
honeypot()  : Honeypot
The Honeypot provides a secret input on forms that bots should NOT fill in, providing an additional safeguard when accepting user input.
image()  : BaseHandler
image()  : BaseHandler
Acts as a factory for ImageHandler classes and returns an instance of the handler. Used like service('image')->withFile($path)->rotate(90)->save();
incomingrequest()  : IncomingRequest
injectMock()  : void
Inject mock object for testing.
iterator()  : Iterator
iterator()  : Iterator
The Iterator class provides a simple way of looping over a function and timing the results and memory usage. Used when debugging and optimizing applications.
language()  : Language
language()  : Language
Responsible for loading the language string translations.
locator()  : FileLocatorInterface
The file locator provides utility methods for looking for non-classes within namespaced folders, as well as convenience methods for loading 'helpers', and 'libraries'.
logger()  : Logger
logger()  : Logger
The Logger class is a PSR-3 compatible Logging class that supports multiple handlers that process the actual logging.
migrations()  : MigrationRunner
migrations()  : MigrationRunner
Return the appropriate Migration runner.
negotiator()  : Negotiate
negotiator()  : Negotiate
The Negotiate class provides the content negotiation features for working the request to determine correct language, encoding, charset, and more.
override()  : void
Overrides an existing entry.
pager()  : Pager
pager()  : Pager
Return the appropriate pagination handler.
parser()  : Parser
parser()  : Parser
The Parser is a simple template parser.
redirectresponse()  : RedirectResponse
redirectresponse()  : RedirectResponse
The Redirect class provides nice way of working with redirects.
renderer()  : View
renderer()  : View
The Renderer class is the class that actually displays a file to the user.
request()  : IncomingRequest|CLIRequest
request()  : CLIRequest|IncomingRequest
Returns the current Request object.
reset()  : void
Reset shared instances and mocks for testing.
resetSingle()  : void
Resets any mock and shared instances for a single service.
response()  : ResponseInterface
response()  : ResponseInterface
The Response class models an HTTP response.
responsecache()  : ResponseCache
responsecache()  : ResponseCache
Return the ResponseCache.
router()  : Router
router()  : Router
The Router class uses a RouteCollection's array of routes, and determines the correct Controller and Method to execute.
routes()  : RouteCollection
routes()  : RouteCollection
The Routes service is a class that allows for easily building a collection of routes.
security()  : Security
security()  : Security
The Security class provides a few handy tools for keeping the site secure, most notably the CSRF protection tools.
serviceExists()  : string|null
Check if the requested service is defined and return the declaring class. Return null if not found.
session()  : Session
session()  : Session
Return the session manager.
set()  : void
Sets an entry.
siteurifactory()  : SiteURIFactory
siteurifactory()  : SiteURIFactory
The Factory for SiteURI.
superglobals()  : Superglobals
superglobals()  : Superglobals
Superglobals.
throttler()  : Throttler
throttler()  : Throttler
The Throttler class provides a simple method for implementing rate limiting in your applications.
timer()  : Timer
timer()  : Timer
The Timer class provides a simple way to Benchmark portions of your application.
toolbar()  : Toolbar
toolbar()  : Toolbar
Return the debug toolbar.
typography()  : Typography
typography()  : Typography
The Typography class provides a way to format text in semantically relevant ways.
uri()  : URI
uri()  : URI
The URI class provides a way to model and manipulate URIs.
validation()  : ValidationInterface
validation()  : ValidationInterface
The Validation class provides tools for validating input data.
viewcell()  : Cell
viewcell()  : Cell
View cells are intended to let you insert HTML into view that has been generated by any callable in the system.
buildServicesCache()  : void
getSharedInstance()  : object
Returns a shared instance of any of the class' services.

Properties

$discovered

Have we already discovered other Services?

protected static bool $discovered = false

$factories

Factory method list.

protected static array<string, callable(mixed ...$params): object> $factories = []

[key => callable]

$instances

Cache for instance of any services that have been requested as a "shared" instance.

protected static array<string, object> $instances = []

Keys should be lowercase service names.

[key => instance]

$mocks

Mock objects for testing which are returned if exist.

protected static array<string, object> $mocks = []

[key => instance]

$services

A cache of other service classes we've found.

No longer used.

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

$serviceNames

A cache of the names of services classes found.

private static array<int, string> $serviceNames = []

Methods

__callStatic()

Provides the ability to perform case-insensitive calling of service names.

public static __callStatic(string $name, array<string|int, mixed> $arguments) : object|null
Parameters
$name : string
$arguments : array<string|int, mixed>
Return values
object|null

autoloader()

The Autoloader class is the central class that handles our spl_autoload_register method, and helper methods.

public static autoloader([bool $getShared = true ]) : Autoloader
Parameters
$getShared : bool = true
Return values
Autoloader

cache()

The cache class provides a simple way to store and retrieve complex data for later.

public static cache([Cache|null $config = null ][, bool $getShared = true ]) : CacheInterface
Parameters
$config : Cache|null = null
$getShared : bool = true
Return values
CacheInterface

clirequest()

public static clirequest([App $config = null ][, mixed $getShared = true ]) : CLIRequest
Parameters
$config : App = null
$getShared : mixed = true
Return values
CLIRequest

codeigniter()

public static codeigniter([App $config = null ][, mixed $getShared = true ]) : CodeIgniter
Parameters
$config : App = null
$getShared : mixed = true
Return values
CodeIgniter

codeigniter()

CodeIgniter, the core of the framework.

public static codeigniter([App|null $config = null ][, bool $getShared = true ]) : CodeIgniter
Parameters
$config : App|null = null
$getShared : bool = true
Return values
CodeIgniter

commands()

public static commands([mixed $getShared = true ]) : Commands
Parameters
$getShared : mixed = true
Return values
Commands

commands()

The commands utility for running and working with CLI commands.

public static commands([bool $getShared = true ]) : Commands
Parameters
$getShared : bool = true
Return values
Commands

createRequest()

public static createRequest([App $config = ][, bool $isCli = false ]) : void
Parameters
$config : App =
$isCli : bool = false

csp()

Content Security Policy

public static csp([ContentSecurityPolicy|null $config = null ][, bool $getShared = true ]) : ContentSecurityPolicy
Parameters
$config : ContentSecurityPolicy|null = null
$getShared : bool = true
Return values
ContentSecurityPolicy

curlrequest()

The CURL Request class acts as a simple HTTP client for interacting with other servers, typically through APIs.

public static curlrequest([array<string|int, mixed> $options = [] ][, ResponseInterface|null $response = null ][, App|null $config = null ][, bool $getShared = true ]) : CURLRequest
Parameters
$options : array<string|int, mixed> = []
$response : ResponseInterface|null = null
$config : App|null = null
$getShared : bool = true
Return values
CURLRequest

email()

public static email([mixed $config = null ][, mixed $getShared = true ]) : Email
Parameters
$config : mixed = null
$getShared : mixed = true
Return values
Email

email()

The Email class allows you to send email via mail, sendmail, SMTP.

public static email([array<string|int, mixed>|Email|null $config = null ][, bool $getShared = true ]) : Email
Parameters
$config : array<string|int, mixed>|Email|null = null
$getShared : bool = true
Return values
Email

encrypter()

The Encryption class provides two-way encryption.

public static encrypter([Encryption|null $config = null ][, bool $getShared = false ]) : EncrypterInterface
Parameters
$config : Encryption|null = null
$getShared : bool = false
Return values
EncrypterInterface

Encryption handler

exceptions()

public static exceptions([Exceptions $config = null ][, mixed $getShared = true ]) : Exceptions
Parameters
$config : Exceptions = null
$getShared : mixed = true
Return values
Exceptions

exceptions()

The Exceptions class holds the methods that handle:

public static exceptions([Exceptions|null $config = null ][, bool $getShared = true ]) : Exceptions
  • set_exception_handler
  • set_error_handler
  • register_shutdown_function
Parameters
$config : Exceptions|null = null
$getShared : bool = true
Return values
Exceptions

filters()

public static filters([Filters $config = null ][, mixed $getShared = true ]) : Filters
Parameters
$config : Filters = null
$getShared : mixed = true
Return values
Filters

filters()

Filters allow you to run tasks before and/or after a controller is executed. During before filters, the request can be modified, and actions taken based on the request, while after filters can act on or modify the response itself before it is sent to the client.

public static filters([Filters|null $config = null ][, bool $getShared = true ]) : Filters
Parameters
$config : Filters|null = null
$getShared : bool = true
Return values
Filters

format()

public static format([Format $config = null ][, mixed $getShared = true ]) : Format
Parameters
$config : Format = null
$getShared : mixed = true
Return values
Format

format()

The Format class is a convenient place to create Formatters.

public static format([Format|null $config = null ][, bool $getShared = true ]) : Format
Parameters
$config : Format|null = null
$getShared : bool = true
Return values
Format

get()

Simple method to get an entry fast.

public static get(string $key) : object|null
Parameters
$key : string

Identifier of the entry to look for.

Return values
object|null

Entry.

honeypot()

public static honeypot([Honeypot $config = null ][, mixed $getShared = true ]) : Honeypot
Parameters
$config : Honeypot = null
$getShared : mixed = true
Return values
Honeypot

honeypot()

The Honeypot provides a secret input on forms that bots should NOT fill in, providing an additional safeguard when accepting user input.

public static honeypot([Honeypot|null $config = null ][, bool $getShared = true ]) : Honeypot
Parameters
$config : Honeypot|null = null
$getShared : bool = true
Return values
Honeypot

image()

public static image([mixed $handler = null ][, Images $config = null ][, mixed $getShared = true ]) : BaseHandler
Parameters
$handler : mixed = null
$config : Images = null
$getShared : mixed = true
Return values
BaseHandler

image()

Acts as a factory for ImageHandler classes and returns an instance of the handler. Used like service('image')->withFile($path)->rotate(90)->save();

public static image([string|null $handler = null ][, Images|null $config = null ][, bool $getShared = true ]) : BaseHandler
Parameters
$handler : string|null = null
$config : Images|null = null
$getShared : bool = true
Return values
BaseHandler

incomingrequest()

public static incomingrequest([App|null $config = null ][, bool $getShared = true ]) : IncomingRequest
Parameters
$config : App|null = null
$getShared : bool = true
Return values
IncomingRequest

injectMock()

Inject mock object for testing.

public static injectMock(string $name, object $mock) : void
Parameters
$name : string
$mock : object
Tags
testTag

only available to test code

iterator()

public static iterator([mixed $getShared = true ]) : Iterator
Parameters
$getShared : mixed = true
Return values
Iterator

iterator()

The Iterator class provides a simple way of looping over a function and timing the results and memory usage. Used when debugging and optimizing applications.

public static iterator([bool $getShared = true ]) : Iterator
Parameters
$getShared : bool = true
Return values
Iterator

language()

public static language([mixed $locale = null ][, mixed $getShared = true ]) : Language
Parameters
$locale : mixed = null
$getShared : mixed = true
Return values
Language

language()

Responsible for loading the language string translations.

public static language([string|null $locale = null ][, bool $getShared = true ]) : Language
Parameters
$locale : string|null = null
$getShared : bool = true
Return values
Language

locator()

The file locator provides utility methods for looking for non-classes within namespaced folders, as well as convenience methods for loading 'helpers', and 'libraries'.

public static locator([bool $getShared = true ]) : FileLocatorInterface
Parameters
$getShared : bool = true
Return values
FileLocatorInterface

logger()

public static logger([mixed $getShared = true ]) : Logger
Parameters
$getShared : mixed = true
Return values
Logger

logger()

The Logger class is a PSR-3 compatible Logging class that supports multiple handlers that process the actual logging.

public static logger([bool $getShared = true ]) : Logger
Parameters
$getShared : bool = true
Return values
Logger

negotiator()

The Negotiate class provides the content negotiation features for working the request to determine correct language, encoding, charset, and more.

public static negotiator([RequestInterface|null $request = null ][, bool $getShared = true ]) : Negotiate
Parameters
$request : RequestInterface|null = null
$getShared : bool = true
Return values
Negotiate

override()

Overrides an existing entry.

public static override(string $key, object $value) : void
Parameters
$key : string

Identifier of the entry.

$value : object

pager()

Return the appropriate pagination handler.

public static pager([Pager|null $config = null ][, RendererInterface|null $view = null ][, bool $getShared = true ]) : Pager
Parameters
$config : Pager|null = null
$view : RendererInterface|null = null
$getShared : bool = true
Return values
Pager

parser()

public static parser([mixed $viewPath = null ][, View $config = null ][, mixed $getShared = true ]) : Parser
Parameters
$viewPath : mixed = null
$config : View = null
$getShared : mixed = true
Return values
Parser

parser()

The Parser is a simple template parser.

public static parser([string|null $viewPath = null ][, View|null $config = null ][, bool $getShared = true ]) : Parser
Parameters
$viewPath : string|null = null
$config : View|null = null
$getShared : bool = true
Return values
Parser

redirectresponse()

The Redirect class provides nice way of working with redirects.

public static redirectresponse([App|null $config = null ][, bool $getShared = true ]) : RedirectResponse
Parameters
$config : App|null = null
$getShared : bool = true
Return values
RedirectResponse

renderer()

public static renderer([mixed $viewPath = null ][, View $config = null ][, mixed $getShared = true ]) : View
Parameters
$viewPath : mixed = null
$config : View = null
$getShared : mixed = true
Return values
View

renderer()

The Renderer class is the class that actually displays a file to the user.

public static renderer([string|null $viewPath = null ][, View|null $config = null ][, bool $getShared = true ]) : View

The default View class within CodeIgniter is intentionally simple, but this service could easily be replaced by a template engine if the user needed to.

Parameters
$viewPath : string|null = null
$config : View|null = null
$getShared : bool = true
Return values
View

request()

Returns the current Request object.

public static request([App|null $config = null ][, bool $getShared = true ]) : CLIRequest|IncomingRequest

The parameter $config and $getShared are deprecated.

createRequest() injects IncomingRequest or CLIRequest.

Parameters
$config : App|null = null
$getShared : bool = true
Return values
CLIRequest|IncomingRequest

reset()

Reset shared instances and mocks for testing.

public static reset([bool $initAutoloader = true ]) : void
Parameters
$initAutoloader : bool = true
Tags
testTag

only available to test code

resetSingle()

Resets any mock and shared instances for a single service.

public static resetSingle(string $name) : void
Parameters
$name : string
Tags
testTag

only available to test code

response()

The Response class models an HTTP response.

public static response([App|null $config = null ][, bool $getShared = true ]) : ResponseInterface
Parameters
$config : App|null = null
$getShared : bool = true
Return values
ResponseInterface

responsecache()

Return the ResponseCache.

public static responsecache([Cache|null $config = null ][, CacheInterface|null $cache = null ][, bool $getShared = true ]) : ResponseCache
Parameters
$config : Cache|null = null
$cache : CacheInterface|null = null
$getShared : bool = true
Return values
ResponseCache

routes()

The Routes service is a class that allows for easily building a collection of routes.

public static routes([bool $getShared = true ]) : RouteCollection
Parameters
$getShared : bool = true
Return values
RouteCollection

security()

public static security([App $config = null ][, mixed $getShared = true ]) : Security
Parameters
$config : App = null
$getShared : mixed = true
Return values
Security

security()

The Security class provides a few handy tools for keeping the site secure, most notably the CSRF protection tools.

public static security([Security|null $config = null ][, bool $getShared = true ]) : Security
Parameters
$config : Security|null = null
$getShared : bool = true
Return values
Security

serviceExists()

Check if the requested service is defined and return the declaring class. Return null if not found.

public static serviceExists(string $name) : string|null
Parameters
$name : string
Return values
string|null

session()

public static session([Session $config = null ][, mixed $getShared = true ]) : Session
Parameters
$config : Session = null
$getShared : mixed = true
Return values
Session

session()

Return the session manager.

public static session([Session|null $config = null ][, bool $getShared = true ]) : Session
Parameters
$config : Session|null = null
$getShared : bool = true
Return values
Session

set()

Sets an entry.

public static set(string $key, object $value) : void
Parameters
$key : string

Identifier of the entry.

$value : object

siteurifactory()

public static siteurifactory([App $config = null ][, Superglobals $superglobals = null ][, mixed $getShared = true ]) : SiteURIFactory
Parameters
$config : App = null
$superglobals : Superglobals = null
$getShared : mixed = true
Return values
SiteURIFactory

siteurifactory()

The Factory for SiteURI.

public static siteurifactory([App|null $config = null ][, Superglobals|null $superglobals = null ][, bool $getShared = true ]) : SiteURIFactory
Parameters
$config : App|null = null
$superglobals : Superglobals|null = null
$getShared : bool = true
Return values
SiteURIFactory

superglobals()

public static superglobals([array<string|int, mixed> $server = null ][, array<string|int, mixed> $get = null ][, bool $getShared = true ]) : Superglobals
Parameters
$server : array<string|int, mixed> = null
$get : array<string|int, mixed> = null
$getShared : bool = true
Return values
Superglobals

superglobals()

Superglobals.

public static superglobals([array<string|int, mixed>|null $server = null ][, array<string|int, mixed>|null $get = null ][, bool $getShared = true ]) : Superglobals
Parameters
$server : array<string|int, mixed>|null = null
$get : array<string|int, mixed>|null = null
$getShared : bool = true
Return values
Superglobals

throttler()

The Throttler class provides a simple method for implementing rate limiting in your applications.

public static throttler([bool $getShared = true ]) : Throttler
Parameters
$getShared : bool = true
Return values
Throttler

timer()

public static timer([mixed $getShared = true ]) : Timer
Parameters
$getShared : mixed = true
Return values
Timer

timer()

The Timer class provides a simple way to Benchmark portions of your application.

public static timer([bool $getShared = true ]) : Timer
Parameters
$getShared : bool = true
Return values
Timer

toolbar()

public static toolbar([Toolbar $config = null ][, mixed $getShared = true ]) : Toolbar
Parameters
$config : Toolbar = null
$getShared : mixed = true
Return values
Toolbar

toolbar()

Return the debug toolbar.

public static toolbar([Toolbar|null $config = null ][, bool $getShared = true ]) : Toolbar
Parameters
$config : Toolbar|null = null
$getShared : bool = true
Return values
Toolbar

typography()

The Typography class provides a way to format text in semantically relevant ways.

public static typography([bool $getShared = true ]) : Typography
Parameters
$getShared : bool = true
Return values
Typography

uri()

public static uri([mixed $uri = null ][, mixed $getShared = true ]) : URI
Parameters
$uri : mixed = null
$getShared : mixed = true
Return values
URI

uri()

The URI class provides a way to model and manipulate URIs.

public static uri([string|null $uri = null ][, bool $getShared = true ]) : URI
Parameters
$uri : string|null = null

The URI string

$getShared : bool = true
Return values
URI

The current URI if $uri is null.

validation()

The Validation class provides tools for validating input data.

public static validation([Validation|null $config = null ][, bool $getShared = true ]) : ValidationInterface
Parameters
$config : Validation|null = null
$getShared : bool = true
Return values
ValidationInterface

viewcell()

public static viewcell([mixed $getShared = true ]) : Cell
Parameters
$getShared : mixed = true
Return values
Cell

viewcell()

View cells are intended to let you insert HTML into view that has been generated by any callable in the system.

public static viewcell([bool $getShared = true ]) : Cell
Parameters
$getShared : bool = true
Return values
Cell

buildServicesCache()

protected static buildServicesCache() : void

getSharedInstance()

Returns a shared instance of any of the class' services.

protected static getSharedInstance(string $key, array<string|int, mixed>|bool|float|int|object|string|null ...$params) : object

$key must be a name matching a service.

Parameters
$key : string
$params : array<string|int, mixed>|bool|float|int|object|string|null
Return values
object

        
On this page

Search results