# Standard library

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.

Exists

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.

Config

You can get the configuration constants through the following functions:

getServer

Returns the current server.

getDB

Returns the current database name.

getDBMS

Returns the current database management system.

getDbUser

Returns the current database username.

getDbPass

Returns the current database username password.

getLanguage

Returns the current language being used by Wolff.

getDirectory

Returns the project root path.

getProjectDirectory

Returns the project root path relative to the server root.

getSystemDirectory

Returns the system folder path.

getAppDirectory

Returns the app folder path.

getPublicDirectory

Returns the public folder path.

getExtensionDirectory

Returns the extensions folder path.

getCacheDirectory

Returns the cache folder path.

getPageTitle

Returns the current page title.

getMainPage

Returns the current main page.

wolffVersion

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'

Path

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.

Others

echod

Echo a variable and then die (for debugging purposes).

echod('Lorem ipsum dolor sit amet');

println

Echo a variable and a new line.

println('Lorem ipsum dolor sit amet');

printr

Print an array in a nice looking way

$array = array('laravel', 'codeigniter', 'wolff', 'yii');
printr($array);

printrd

Print an array in a nice looking way and then die

$array = array('laravel', 'codeigniter', 'wolff', 'yii');
printrd($array);

dumpd

Var dump a variable and then die (for debugging purposes).

$str = 'Hello world';
dumpd($str);

dumpAll

Var dump all the current variables.

isJson

Returns true if the given string is a json.

$json = '{name: "John", age: 21, city: "New York"}';
isJson($json);
//Returns true

toArray

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);

arrayToCsv

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');

getLocalUrl

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.

getClientIP

Returns the current client IP.

getClientIP();
//In localhost it will return ::1

getUserAgent

Returns the HTTP User Agent information (equivalent to $_SERVER['HTTP_USER_AGENT']).

getServerRoot

Returns the server root path (equivalent to $_SERVER['DOCUMENT_ROOT']).

inCli

Returns true if the current code is running in a Command Line Interface, false otherwise.

getCurrentPage

Returns the current url relative to the project root directory.

getCurrentPage();
//If the current url is localhost/wolff/homepage it will return 'homepage'.

getPureCurrentPage

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'.

getBenchmark

Returns the time between the page load start and the current time in seconds as float.

Adding functions to the Standard library

You can add your own functions to the standard library this way.

  1. Create a php file with the following structure in the system/definitions folder:
<?php

namespace {
    function example() {
        //Code
    }
}
  1. Add all the functions you want.

  2. After that add the following line to your composer.json file inside the autoload > files array

"system/definitions/yourfilename.php",
  1. Remember to run composer dump-autoload for the changes to take effect.

And you are done, now your functions are available inside all your Wolff project.