October includes a variety of "helper" PHP functions. Many of these functions are used internally by October itself, however, you are free to use them in your own plugins and applications if you find them useful.
<style> .collection-method-list > p { column-count: 3; -moz-column-count: 3; -webkit-column-count: 3; column-gap: 2em; -moz-column-gap: 2em; -webkit-column-gap: 2em; } .collection-method-list a { display: block; } </style>The array_add
function adds a given key / value pair to the array if the given key doesn't already exist in the array:
$array = array_add(['name' => 'Desk'], 'price', 100);
// ['name' => 'Desk', 'price' => 100]
The array_divide
function returns two arrays, one containing the keys, and the other containing the values of the original array:
list($keys, $values) = array_divide(['name' => 'Desk']);
// $keys: ['name']
// $values: ['Desk']
The array_dot
function flattens a multi-dimensional array into a single level array that uses "dot" notation to indicate depth:
$array = array_dot(['foo' => ['bar' => 'baz']]);
// ['foo.bar' => 'baz'];
The array_undot
function is the counter-part to the array_dot
method. It will convert a dot-notated array into a standard associative array:
$array = array_undot([
'foo.bar' => 'baz'
]);
// [
// 'foo' => [
// 'bar' => 'baz'
// ]
// ]
The array_except
method removes the given key / value pairs from the array:
$array = ['name' => 'Desk', 'price' => 100];
$array = array_except($array, ['price']);
// ['name' => 'Desk']
The array_first
method returns the first element of an array passing a given truth test:
$array = [100, 200, 300];
$value = array_first($array, function ($key, $value) {
return $value >= 150;
});
// 200
A default value may also be passed as the third parameter to the method. This value will be returned if no value passes the truth test:
$value = array_first($array, $callback, $default);
The array_flatten
method will flatten a multi-dimensional array into a single level.
$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];
$array = array_flatten($array);
// ['Joe', 'PHP', 'Ruby'];
The array_forget
method removes a given key / value pair from a deeply nested array using "dot" notation:
$array = ['products' => ['desk' => ['price' => 100]]];
array_forget($array, 'products.desk');
// ['products' => []]
The array_get
method retrieves a value from a deeply nested array using "dot" notation:
$array = ['products' => ['desk' => ['price' => 100]]];
$value = array_get($array, 'products.desk');
// ['price' => 100]
The array_get
function also accepts a default value, which will be returned if the specific key is not found:
$value = array_get($array, 'names.john', 'default');
The array_only
method will return only the specified key / value pairs from the given array:
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
$array = array_only($array, ['name', 'price']);
// ['name' => 'Desk', 'price' => 100]
The array_pluck
method will pluck a list of the given key / value pairs from the array:
$array = [
['developer' => ['name' => 'Brian']],
['developer' => ['name' => 'Stewie']]
];
$array = array_pluck($array, 'developer.name');
// ['Brian', 'Stewie'];
The array_pull
method returns and removes a key / value pair from the array:
$array = ['name' => 'Desk', 'price' => 100];
$name = array_pull($array, 'name');
// $name: Desk
// $array: ['price' => 100]
The array_set
method sets a value within a deeply nested array using "dot" notation:
$array = ['products' => ['desk' => ['price' => 100]]];
array_set($array, 'products.desk.price', 200);
// ['products' => ['desk' => ['price' => 200]]]
The array_sort
method sorts the array by the results of the given Closure:
$array = [
['name' => 'Desk'],
['name' => 'Chair'],
];
$array = array_values(array_sort($array, function ($value) {
return $value['name'];
}));
/*
[
['name' => 'Chair'],
['name' => 'Desk'],
]
*/
The array_sort_recursive
function recursively sorts the array using the sort
function:
$array = [
[
'Brian',
'Shannon',
'Alec',
],
[
'PHP',
'Ruby',
'JavaScript',
],
];
$array = array_sort_recursive($array);
/*
[
[
'Alec',
'Brian',
'Shannon',
],
[
'JavaScript',
'PHP',
'Ruby',
]
];
*/
The array_where
function filters the array using the given Closure:
$array = [100, '200', 300, '400', 500];
$array = array_where($array, function ($value, $key) {
return is_string($value);
});
// [1 => 200, 3 => 400]
The head
function simply returns the first element in the given array:
$array = [100, 200, 300];
$first = head($array);
// 100
The last
function returns the last element in the given array:
$array = [100, 200, 300];
$last = last($array);
// 300
Path prefix symbols can be used to create a dynamic path. For example, a path beginning with ~/
will create a path relative to the application:
list: ~/plugins/acme/pay/models/invoiceitem/columns.yaml
These symbols are supported for creating dynamic paths:
Symbol | Description |
---|---|
$ |
Relative to the plugins directory |
~ |
Relative to the application directory |
The app_path
function returns the fully qualified path to the app
directory:
$path = app_path();
You may also use the app_path
function to generate a fully qualified path to a given file relative to the application directory:
$path = app_path('Http/Controllers/Controller.php');
The base_path
function returns the fully qualified path to the project root:
$path = base_path();
You may also use the base_path
function to generate a fully qualified path to a given file relative to the application directory:
$path = base_path('vendor/bin');
The config_path
function returns the fully qualified path to the application configuration directory:
$path = config_path();
You may also use the config_path
function to generate a fully qualified path to a given file relative to the config directory:
$path = config_path('dev/cms.php');
The database_path
function returns the fully qualified path to the application's database directory:
$path = database_path();
The plugins_path
function returns the fully qualified path to the application plugin directory:
$path = plugins_path();
You may also use the plugins_path
function to generate a fully qualified path to a given file relative to the plugins directory:
$path = plugins_path('author/plugin/routes.php');
The public_path
function returns the fully qualified path to the public
directory:
$path = public_path();
The storage_path
function returns the fully qualified path to the storage
directory:
$path = storage_path();
You may also use the storage_path
function to generate a fully qualified path to a given file relative to the storage directory:
$path = storage_path('app/file.txt');
The temp_path
function returns the fully qualified path to a writable directory for temporary files:
$path = temp_path();
You may also use the temp_path
function to generate a fully qualified path to a given file relative to the temp directory:
$path = temp_path('app/file.txt');
The themes_path
function returns the fully qualified path to the themes
directory:
$path = themes_path();
You may also use the themes_path
function to generate a fully qualified path to a given file relative to the themes directory:
$path = themes_path('mytheme/file.txt');
The cache_path
function returns the fully qualified path to the application cache directory:
$path = cache_path();
You may also use the cache_path
function to generate a fully qualified path to a given file relative to the cache directory:
$path = cache_path('cms/cachefile.json');
The camel_case
function converts the given string to camelCase
:
$camel = camel_case('foo_bar');
// fooBar
The class_basename
returns the class name of the given class with the class' namespace removed:
$class = class_basename('Foo\Bar\Baz');
// Baz
The e
function runs htmlentities
over the given string:
echo e('<html>foo</html>');
// <html>foo</html>
The ends_with
function determines if the given string ends with the given value:
$value = ends_with('This is my name', 'name');
// true
The snake_case
function converts the given string to snake_case
:
$snake = snake_case('fooBar');
// foo_bar
The str_limit
function limits the number of characters in a string. The function accepts a string as its first argument and the maximum number of resulting characters as its second argument:
$value = str_limit('The CMS platform that gets back to basics.', 6);
// The CMS...
The starts_with
function determines if the given string begins with the given value:
$value = starts_with('The cow goes moo', 'The');
// true
The str_contains
function determines if the given string contains the given value:
$value = str_contains('The bird goes tweet', 'bird');
// true
The str_finish
function adds a single instance of the given value to a string:
$string = str_finish('this/string', '/');
// this/string/
The str_is
function determines if a given string matches a given pattern. Asterisks may be used to indicate wildcards:
$value = str_is('foo*', 'foobar');
// true
$value = str_is('baz*', 'foobar');
// false
The str_plural
function converts a string to its plural form. This function currently only supports the English language:
$plural = str_plural('car');
// cars
$plural = str_plural('child');
// children
The str_random
function generates a random string of the specified length:
$string = str_random(40);
The str_singular
function converts a string to its singular form. This function currently only supports the English language:
$singular = str_singular('cars');
// car
The str_slug
function generates a URL friendly "slug" from the given string:
$title = str_slug("October CMS", "-");
// october-cms
The studly_case
function converts the given string to StudlyCase
:
$value = studly_case('foo_bar');
// FooBar
The trans
function translates the given language line using your localization files:
echo trans('validation.required'):
The trans_choice
function translates the given language line with inflection:
$value = trans_choice('foo.bar', $count);
Generate a URL for an asset using the current scheme of the request (HTTP or HTTPS):
$url = asset('img/photo.jpg');
The config
function gets the value of a configuration variable. The configuration values may be accessed using "dot" syntax, which includes the name of the file and the option you wish to access. A default value may be specified and is returned if the configuration option does not exist:
$value = config('app.timezone');
$value = config('app.timezone', $default);
The config
helper may also be used to set configuration variables at runtime by passing an array of key / value pairs:
config(['app.debug' => true]);
The dd
function dumps the given variable and ends execution of the script:
dd($value);
The env
function gets the value of an environment variable or returns a default value:
$env = env('APP_ENV');
// Return a default value if the variable doesn't exist...
$env = env('APP_ENV', 'production');
The get
function obtains an input item from the request, restricted to GET variables only:
$value = get('key', $default = null)
The input
function obtains an input item from the request:
$value = input('key', $default = null)
The post
function obtains an input item from the request, restricted to POST variables only:
$value = post('key', $default = null)
The redirect
function return an instance of the redirector to do redirect responses:
return redirect('/home');
The request
function returns the current request instance:
$referer = request()->header('referer');
The response
function creates a response instance or obtains an instance of the response factory:
return response('Hello World', 200, $headers);
return response()->json(['foo' => 'bar'], 200, $headers);
The route
function generates a URL for the given named route:
$url = route('routeName');
If the route accepts parameters, you may pass them as the second argument to the method:
$url = route('routeName', ['id' => 1]);
Generate a URL for an asset using HTTPS:
echo secure_asset('foo/bar.zip', $title, $attributes = []);
The trace_log
function writes a trace message to the log file.
trace_log('This code has passed...');
The function supports passing exceptions, arrays and objects:
trace_log($exception);
trace_log($array);
trace_log($object);
You may also pass multiple arguments to trace multiple messages:
trace_log($value1, $value2, $exception, '...');
The trace_sql
function enables database logging and begins to monitor all SQL output.
trace_sql();
Db::table('users')->count();
// select count(*) as aggregate from users
The url
function generates a fully qualified URL to the given path:
echo url('user/profile');
echo url('user/profile', [1]);