Beside the custom libraries you can use and create in Wolff, there’s the standard library which is used by Wolff itself, but you can use it too.
These functions are accessible from anywhere inside Wolff just like the native PHP functions.
Returns true if the given array is associative, false otherwise.
Take in mind that in the context of the function, an associative array is an array with non-numeric keys.
$arr = [
'name' => 'Margaret',
'age' => 63
];
isAssoc($arr);
That should return true
.
isAssoc([ 'Margaret', 'Thomas', 'Edward' ]);
That should return false
.
Returns the specified key from the given array. If the given key doesn't exists it will simply return null
.
$arr = [
'name' => 'Margaret',
'age' => 63
];
val($arr, 'name');
That should return the string Margaret
.
The key accepts dot notation:
val($arr, 'user.name');
That should be the equivalent to $arr['user']['name']
.
Returns the average value of the given numbers array.
average([ 2.5, 5.46, 4, 9 ]);
That should return 5.24
.
Echo a variable and then die (for debugging purposes).
echod('Lorem ipsum dolor sit amet');
Print an array in a nice looking way
$array = ['laravel', 'codeigniter', 'wolff', 'yii'];
printr($array);
This function can take any number of parameters.
printr($array, $array2, $array3...);
Print an array in a nice looking way and then die
$array = ['laravel', 'codeigniter', 'wolff', 'yii'];
printrd($array);
This function can take any number of parameters.
printrd($array, $array2, $array3...);
Var dump a variable and then die (for debugging purposes).
$str = 'Hello world';
dumpd($str);
Var dump all the current variables.
dumpAll();
Returns true if the given parameter complies with an integer.
isInt('1');
isInt(1);
Both of the calls showed above will return true.
Returns true if the given parameter complies with an integer.
isFloat('1.5');
isFloat(1.5);
Both of the calls showed above will return true.
Returns true if the given parameter complies with an boolean.
isBool(true);
isBool('1');
Both of the calls showed above will return true.
Only the numeric values 1 and 0, and the strings 'true', 'false', '1' and '0' are counted as boolean.
Returns true if the given string is a json.
$json = '{name: "John", age: 21, city: "New York"}';
isJson($json);
That will return true.
Returns the given variable as a associative array.
Useful when it is necessary to turn a multidimensional json or std object to an array.
$json = '{name: "John", age: 21, city: "New York"}';
toArray($json);
Convert an array result into a csv file and then downloads it.
$array = [
0 => [
'name' => 'John Doe',
'email' => '[email protected]'
],
1 => [
'name' => 'Jane Doe',
'email' => '[email protected]'
],
];
//The first parameter is the array, the second is the desired file name (without extension).
arrayToCsv($array, 'filename');
Returns the given string as a local url. Useful for redirections.
Example:
url('home/sayHello');
If the project is located at http://localhost/wolff
, the function will return http://localhost/wolff/home/sayHello
.
If the project is located at https://www.getWolff.com
, the function will return https://www.getWolff.com/home/sayHello
.
Returns the current client IP.
getClientIP();
//In localhost it will return ::1
Returns the HTTP User Agent information (equivalent to $_SERVER['HTTP_USER_AGENT']
).
getUserAgent();
Returns the server root path (equivalent to $_SERVER['DOCUMENT_ROOT']
).
getServerRoot();
Returns true if the current request is running in localhost.
local();
Returns true if the current code is running in a Command Line Interface, false otherwise.
inCli();
Returns the current url relative to the project root directory.
getCurrentPage();
//If the current url is localhost/wolff/homepage it will return 'homepage'.
Returns the complete current url without parameters.
getPureCurrentPage();
//If the current url is http://example.com/homepage?id=2 it will return 'http://example.com/homepage'.
Returns the time between the page load start and the current time in seconds as float.
getBenchmark();
You can get the configuration constants through the following functions.
Keep in mind that the paths returned by the config.php
functions are relative to the server root, except for the getPublicDir
and getProjectDir
functions.
The functions related to directories can take a string as a parameter which will be concatenated to the directory returned. Example:
getPublic('home.png');
//In this case it will return 'localhost/wolff/public/home.png'
Returns the specified key value from the CONFIG
array. If the given key doesn't exists it will simply return null
.
config('root_dir');
That is the equivalent to CONFIG['root_dir']
.
The key accepts dot notation:
config('db.password');
That is the equivalent to CONFIG['db']['password']
.
Returns the current server.
getServer();
Returns the current database name.
getDB();
Returns the current database management system.
getDBMS();
Returns the current database username.
getDbUser();
Returns the current database username password.
getDbPass();
Returns the current language being used by Wolff.
getLanguage();
Returns the project root path.
getDir();
Returns the project root path relative to the server root.
getProjectDir();
Returns the system folder path.
getSystemDir();
Returns the app folder path.
getAppDir();
Returns the public folder path.
getPublic();
Returns the cache folder path.
getCacheDir();
Returns the current page title.
config('title');
Returns the current main page.
getMainPage();
Returns the current version of Wolff.
wolffVersion();
//In this case it should return 2.0
You can add your own functions to the standard library this way.
system/definitions
folder:<?php
namespace {
function example() {
//Code
}
}
Add all the functions you want.
After that add the following line to your composer.json file inside the autoload > files array
"system/definitions/yourfilename.php",
composer dump-autoload
for the changes to take effect.And you are done, now your functions are available inside all your Wolff project.