Working with application

Running an application in CLI mode

Application runtime schema in CLI mode

The application object run() method is responsible for the initialization step of the main add-on. It is called once by framework loader at startup.

Hooks are functions that are called by the framework before the application code starts to run. This lets you execute some global initializations as, e.g., define database connection values (path, login, password...)

An example of how to write code working with CLI.

$this->lib->cli->out('Welcome to toKernel', 'red'); $this->lib->cli->out("\n"); // or TK_NL constant $this->lib->cli->out('Please enter your name:', 'green'); $name = $this->lib->cli->in(); $this->lib->cli->out('Hello ' . $name, 'yellow');

Application object methods for CLI mode

All these methods can be called through $this->app

string action()

Returns the name of the method (without any prefix) that will be invoked within the main addon.
For example, if the entered command is:

/usr/bin/php /var/www/html/index.php --language ru --addon migration --action check_db


$cli_action = $this->app->action();

will set $cli_action to 'check_db'.

string alias()

Returns the alias name if defined or an empty string otherwise

$alias = $this->app->alias();

For example, if we edit /application/config/aliases.ini and add the alias definition

[do_migrate] id_addon=migration action=check_db template=

and then run the application as:

/usr/bin/php /var/www/html/index.php --language ru --do_migrate

the result will be the same as if we had entered the longer command above

array allowed_languages([ string $lp])

Returns an array containing the application's allowed languages for CLI mode as defined in the /application/config/tokernel.ini configration file's section: [CLI] when called without parameters. Each array element will have the language prefix as key and the language name as value.

// Get all allowed languages. $allowed_languages = $this->app->allowed_languages();

When called with a language prefix parameter, it will return the language name.

// Get language by prefix $en = $this->app->allowed_languages('en');

The result will be 'English'

string id_addon()

Returns the name of the main addon
For example, after entering:

/usr/bin/php /var/www/html/index.php --addon migration --action check_db $addon = $this->app->id_addon();

will set $addon to 'migration'

string language([string $item = NULL])

Return value from translation file

// Return message translated 'Enter title' $message = $this->app->language('enter_title'); // Return language prefix 'ru' if url is : $lp = $this->app->language();

See Language management Language class library

mixed params([string $item = NULL])

If given an argument, this method will return the value of the parameter whose key is the argument if it exists. If the key is not defined, this method return an empty string.

When called without arguments, this method will return an array containing all the parameters passed in the command
For example, if the command is:

/usr/bin/php /var/www/html/index.php --language ru --addon migration --action check_db --exclude_table users --last_id 98 $id = $this->app->params('last_id'); // Return 98 $params = $this->app->params(); // Return the array ('exclude_table' => 'users', 'last_id' => '98')

int params_count()

Returns the count of parameters in the command line

$pc = $this->app->params_count();

void set_language(string $lp)

Sets the application current language by prefix.


After this call, all invocations of the language() method will use the ru.ini translation files.

See also: CLI class library CLI with interactive mode Aliasing for URL and CLI arguments Managing aliases for URL and CLI arguments