CodeIgniter provides a few functions and variables that are globally defined, and are available to you at any point. These do not require loading any additional libraries or helpers.
cache([$key]) | Parameters: |
|
|---|---|
| Returns: |
Either the cache object, or the item retrieved from the cache |
| Return type: |
mixed |
If no $key is provided, will return the Cache engine instance. If a $key is provided, will return the value of $key as stored in the cache currently, or null if no value is found.
Examples:
$foo = cache('foo');
$cache = cache();
| Parameters: |
|
|---|---|
| Return type: |
|
| Returns: |
|
| Throws: |
|
Simpler way to create a new Cookie instance.
| Parameters: |
|
|---|---|
| Return type: |
|
| Returns: |
Instance of |
Fetches the global CookieStore instance held by Response.
env($key[, $default = null]) | Parameters: |
|
|---|---|
| Returns: |
The environment variable, the default value, or null. |
| Return type: |
mixed |
Used to retrieve values that have previously been set to the environment, or return a default value if it is not found. Will format boolean values to actual booleans instead of string representations.
Especially useful when used in conjunction with .env files for setting values that are specific to the environment itself, like database settings, API keys, etc.
esc($data[, $context = 'html'[, $encoding]]) | Parameters: |
|
|---|---|
| Returns: |
The escaped data. |
| Return type: |
mixed |
Escapes data for inclusion in web pages, to help prevent XSS attacks. This uses the Laminas Escaper library to handle the actual filtering of the data.
If $data is a string, then it simply escapes and returns it. If $data is an array, then it loops over it, escaping each ‘value’ of the key/value pairs.
Valid context values: html, js, css, url, attr, raw, null
helper($filename) | Parameters: |
|
|---|
Loads a helper file.
For full details, see the Helper Functions page.
lang($line[, $args[, $locale]]) | Parameters: |
|
|---|
Retrieves a locale-specific file based on an alias string.
For more information, see the Localization page.
model($name[, $getShared = true[, &$conn = null]]) | Parameters: |
|
|---|---|
| Returns: |
More simple way of getting model instances |
| Return type: |
mixed |
old($key[, $default = null[, $escape = 'html']]) | Parameters: |
|
|---|---|
| Returns: |
The value of the defined key, or the default value. |
| Return type: |
mixed |
Provides a simple way to access “old input data” from submitting a form.
Example:
// in controller, checking form submittal
if (! $model->save($user))
{
// 'withInput' is what specifies "old data"
// should be saved.
return redirect()->back()->withInput();
}
// In the view
<input type="email" name="email" value="<?= old('email') ?>">
// Or with arrays
<input type="email" name="user[email]" value="<?= old('user.email') ?>">
Note
If you are using the form helper, this feature is built-in. You only need to use this function when not using the form helper.
session([$key]) | Parameters: |
|
|---|---|
| Returns: |
An instance of the Session object if no $key, the value found in the session for $key, or null. |
| Return type: |
mixed |
Provides a convenient way to access the session class and to retrieve a stored value. For more information, see the Sessions page.
timer([$name]) | Parameters: |
|
|---|---|
| Returns: |
The Timer instance |
| Return type: |
CodeIgniterDebugTimer |
A convenience method that provides quick access to the Timer class. You can pass in the name of a benchmark point as the only parameter. This will start timing from this point, or stop timing if a timer with this name is already running.
Example:
// Get an instance
$timer = timer();
// Set timer start and stop points
timer('controller_loading'); // Will start the timer
. . .
timer('controller_loading'); // Will stop the running timer
view($name[, $data[, $options]]) | Parameters: |
|
|---|---|
| Returns: |
The output from the view. |
| Return type: |
string |
Grabs the current RendererInterface-compatible class and tells it to render the specified view. Simply provides a convenience method that can be used in Controllers, libraries, and routed closures.
Currently, only one option is available for use within the $options array, saveData which specifies that data will persistent between multiple calls to view() within the same request. By default, the data for that view is forgotten after displaying that single view file.
The $option array is provided primarily to facilitate third-party integrations with libraries like Twig.
Example:
$data = ['user' => $user];
echo view('user_profile', $data);
For more details, see the Views page.
view_cell($library[, $params = null[, $ttl = 0[, $cacheName = null]]]) | Parameters: |
|
|---|---|
| Returns: |
View cells are used within views to insert HTML chunks that are managed by other classes. |
| Return type: |
string |
For more details, see the View Cells page.
app_timezone() | Returns: | The timezone the application has been set to display dates in. |
|---|---|
| Return type: | string |
Returns the timezone the application has been set to display dates in.
csrf_token() | Returns: | The name of the current CSRF token. |
|---|---|
| Return type: | string |
Returns the name of the current CSRF token.
csrf_header() | Returns: | The name of the header for current CSRF token. |
|---|---|
| Return type: | string |
The name of the header for current CSRF token.
csrf_hash() | Returns: | The current value of the CSRF hash. |
|---|---|
| Return type: | string |
Returns the current CSRF hash value.
csrf_field() | Returns: | A string with the HTML for hidden input with all required CSRF information. |
|---|---|
| Return type: | string |
Returns a hidden input with the CSRF information already inserted:
<input type="hidden" name="{csrf_token}" value="{csrf_hash}">
csrf_meta() | Returns: | A string with the HTML for meta tag with all required CSRF information. |
|---|---|
| Return type: | string |
Returns a meta tag with the CSRF information already inserted:
<meta name="{csrf_header}" content="{csrf_hash}">
force_https($duration = 31536000[, $request = null[, $response = null]]) | Parameters: |
|
|---|
Checks to see if the page is currently being accessed via HTTPS. If it is, then nothing happens. If it is not, then the user is redirected back to the current URI but through HTTPS. Will set the HTTP Strict Transport Security header, which instructs modern browsers to automatically modify any HTTP requests to HTTPS requests for the $duration.
function_usable($function_name) | Parameters: |
|
|---|---|
| Returns: |
true if the function exists and is safe to call, false otherwise. |
| Return type: |
bool |
is_cli() | Returns: | true if the script is being executed from the command line or false otherwise. |
|---|---|
| Return type: | bool |
is_really_writable($file) | Parameters: |
|
|---|---|
| Returns: |
true if you can write to the file, false otherwise. |
| Return type: |
bool |
log_message($level, $message[, $context]) | Parameters: |
|
|---|---|
| Returns: |
true if was logged successfully or false if there was a problem logging it |
| Return type: |
bool |
Logs a message using the Log Handlers defined in app/Config/Logger.php.
Level can be one of the following values: emergency, alert, critical, error, warning, notice, info, or debug.
Context can be used to substitute values in the message string. For full details, see the Logging Information page.
redirect(string $route) | Parameters: |
|
|---|
Returns a RedirectResponse instance allowing you to easily create redirects:
// Go back to the previous page
return redirect()->back();
// Go to specific UI
return redirect()->to('/admin');
// Go to a named/reverse-routed URI
return redirect()->route('named_route');
// Keep the old input values upon redirect so they can be used by the `old()` function
return redirect()->back()->withInput();
// Set a flash message
return redirect()->back()->with('foo', 'message');
// Copies all cookies from global response instance
return redirect()->back()->withCookies();
// Copies all headers from the global response instance
return redirect()->back()->withHeaders();
When passing an argument into the function, it is treated as a named/reverse-routed route, not a relative/full URI, treating it the same as using redirect()->route():
// Go to a named/reverse-routed URI
return redirect('named_route');
remove_invisible_characters($str[, $urlEncoded = true]) | Parameters: |
|
|---|---|
| Returns: |
Sanitized string |
| Return type: |
string |
This function prevents inserting null characters between ASCII characters, like Java\0script.
Example:
remove_invisible_characters('Java\\0script');
// Returns: 'Javascript'
route_to($method[, ...$params]) | Parameters: |
|
|---|
Generates a URI relative to the domain name (not baseUrl) for you based on either a named route alias, or a controller::method combination. Will take parameters into effect, if provided.
For full details, see the URI Routing page.
service($name[, ...$params]) | Parameters: |
|
|---|---|
| Returns: |
An instance of the service class specified. |
| Return type: |
mixed |
Provides easy access to any of the Services defined in the system. This will always return a shared instance of the class, so no matter how many times this is called during a single request, only one class instance will be created.
Example:
$logger = service('logger');
$renderer = service('renderer', APPPATH.'views/');
single_service($name[, ...$params]) | Parameters: |
|
|---|---|
| Returns: |
An instance of the service class specified. |
| Return type: |
mixed |
Identical to the service() function described above, except that all calls to this function will return a new instance of the class, where service returns the same instance every time.
slash_item($item) | Parameters: |
|
|---|---|
| Returns: |
The configuration item or null if the item doesn’t exist |
| Return type: |
string|null |
Fetch a config file item with slash appended (if not empty)
stringify_attributes($attributes[, $js]) | Parameters: |
|
|---|---|
| Returns: |
String containing the attribute key/value pairs, comma-separated |
| Return type: |
string |
Helper function used to convert a string, array, or object of attributes to a string.
The following constants are always available anywhere within your application.
constant APPPATH The path to the app directory.
constant ROOTPATH The path to the project root directory. Just above APPPATH.
constant SYSTEMPATH The path to the system directory.
constant FCPATH The path to the directory that holds the front controller.
constant WRITEPATH The path to the writable directory.
constant SECOND Equals 1.
constant MINUTE Equals 60.
constant HOUR Equals 3600.
constant DAY Equals 86400.
constant WEEK Equals 604800.
constant MONTH Equals 2592000.
constant YEAR Equals 31536000.
constant DECADE Equals 315360000.
© 2014–2020 British Columbia Institute of Technology
Licensed under the MIT License.
https://codeigniter.com/user_guide/general/common_functions.html