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.
If you want to know if an element exists in Wolff, you can use the exists methods which return true if that element exists, or false otherwise.
As an example, if you want to know if the subfolder/home controller exists you can use this:
controllerExists('subfolder/home');
The same can be applied to views and much more. Using the functionExists
, viewExists
and languageExists
.
You can get the configuration constants through the following functions:
Returns the current server.
Returns the current database name.
Returns the current database management system.
Returns the current database username.
Returns the current database username password.
Returns the current language being used by Wolff.
Returns the project root path.
Returns the project root path relative to the server root.
Returns the system folder path.
Returns the app folder path.
Returns the public folder path.
Returns the extensions folder path.
Returns the cache folder path.
Returns the current page title.
Returns the current main page.
Returns the current version of Wolff.
wolffVersion();
//In this case it should return 0.9.1
Keep in mind that the paths returned by the config.php
functions are relative to the server root, except for the getPublicDirectory
and getProjectDirectory
functions.
The functions related to directories can take a string as a parameter which will be concatenated to the directory returned. Example:
getPublicDirectory('home.png');
//In this case it will return 'localhost/wolff/public/home.png'
You can get the complete path of an element (relative to the server root folder) calling the path methods which return a string with the respective path.
As an example, if you want to know the subfolder/home controller path you can use this:
getControllerPath('subfolder/home');
The same can be applied to views and much more. Using the getControllerPath
, getViewPath
and getLanguagePath
.
Echo a variable and then die (for debugging purposes).
echod('Lorem ipsum dolor sit amet');
Echo a variable and a new line.
println('Lorem ipsum dolor sit amet');
Print an array in a nice looking way
$array = array('laravel', 'codeigniter', 'wolff', 'yii');
printr($array);
Print an array in a nice looking way and then die
$array = array('laravel', 'codeigniter', 'wolff', 'yii');
printrd($array);
Var dump a variable and then die (for debugging purposes).
$str = 'Hello world';
dumpd($str);
Var dump all the current variables.
Returns true if the given string is a json.
$json = '{name: "John", age: 21, city: "New York"}';
isJson($json);
//Returns 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 = array(
0 => array(
'name' => 'John Doe',
'email' => '[email protected]'
),
1 => array(
'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:
getLocalUrl('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']
).
Returns the server root path (equivalent to $_SERVER['DOCUMENT_ROOT']
).
Returns true if the current code is running in a Command Line Interface, false otherwise.
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.
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.