Documentation

Working with application

Running an application in HTTP mode

Application runtime schema in HTTP 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...)

Application object methods for HTTP 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 requested URL is:

http://example.com/test/show_form/some_item/some_value

executing:

$action = $this->app->action(); will set $action to 'show_form'.

Methods in addon classes are accessible from an HTTP request only if they are defined with special prefixes.

// 'action_' prefix for standard HTTP requests public function action_show_form() { /* ... */ } // 'action_ax_' prefix for Ajax requests public function action_ax_show_form() { /* ... */ }

Any other method defined in an addon class can't be accessed via an external request and can therefore be considered as private to the application.

string alias()

Returns the alias name if defined or an empty srinng otherwise

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

array allowed_languages([ string $lp])

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

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 app_url()

Returns the url of the application instance including the path where it resides. For example, if the web server document root is located in /var/www/html and the application directory is located in /var/www/html/application/

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

will set $app_url to 'http://example.com/application/'

bool cached_output()

Returns true if the html page content could be retrieved from the cache and was loaded in the output buffer.

$is_cached = $this->app->cached_output();

void error_404([string $message = NULL ,][string $file = NULL, ][int $line = NULL)

Redirects to the "Error 404 - Page not found" page.

In this case, application will send actual 404 header status.

$this->app->error_404('Invalid ID', __FILE__, __LINE__); $this->app->error_404('Article not found');

string get_template()

Returns the name of the template for the main addon

$template = $this->app->get_template();

string id_addon()

Returns the name of the main addon

In the example used above:

http://example.com/test/show_form/some_item/some_value

$addon = $this->app->id_addon();

will set $addon to 'test'

bool is_ajax_request()

Returns true if the application is handling an Ajax request

if($this->app->is_ajax_request()) { /* ... */ }

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 : http://example.com/ru/test/show_form/some_item/some_value $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 request URL
For example, if the request URL is:

http://example.com/test/show_form/some_item/some_value/id/88/type/public // Return 88 $id = $this->app->params('id'); /* Return the array ('some_item' => 'some_value', 'id' => '88', 'type' => 'public') in the Assoc parameter mode */ $params = $this->app->params();

Be careful to set the correct parameter parsing mode in the /application/config/tokernel.ini configuration file's [HTTP] section (see url lib documentation)

int params_count()

Returns the count of parameters in the request URL

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

void redirect(string $url)

Redirects to the page whose url is passed in the argument

$this->app->redirect($some_url);

mixed request_method()

Returns the HTTP request method (usually 'GET' or 'POST')

$rm = $this->app->request_method();

void set_header(string $header [, bool $replace = true])

Sets an HTTP response header. Since the framework buffers all output, you don't have to take particular precautions not to produce any output before calling this method.

The optional replace parameter indicates whether the header should replace a previous similar header.

$this->app->set_header('Content-type: application/pdf'); $this->app->set_header('Content-type: application/pdf', false); // Do not replace the header

void set_language(string $lp)

Sets the application current language by prefix. $this->app->set_language('ru');

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

void set_template(string $template)

Changes the template to be used for the page display.

$this->app->set_template('some_addon_name.some_action_name');

This method can be used to override the default template, if you want to use the same template for several addons or actions.

By default, the framework uses a template whose name is 'addon_name.action_name.tpl.php'

For example, if the addon name is 'test' and the action is 'show_form' the default template will have the name 'test.show_form.tpl.php'.

Templates are located in:

/application/templates/ /framework/templates/

with the application templates overriding the framework ones.

string theme_name()

Returns the name of the application defined theme. Themes gives the posibility to use several different styles in project as is customary in many CMS applications.

$current_theme = $this->app->theme_name();

The themes name are defined in '/application/config/tokernel.ini', section: [HTTP]
Themes are located in '/application/themes/'

string theme_path()

Returns the path of the application defined theme.

// Returns /var/www/html/application/themes/red_zone/ $theme_path = $this->app->theme_path(); // Creating variable with value /var/www/html/application/themes/red_zone/images/button.jpg $image_file = $this->app->theme_path() . 'images' . TK_DS . 'button.jpg';

string theme_url()

Returns the url of the application defined theme.

// Returns http://example.com/application/themes/red_zone/ $theme_url = $this->app->theme_url(); // Creating variable with value http://example.com/application/themes/red_zone/css/style.css $css_file = $this->app->theme_url() . 'css/style.jpg';

string tk_url()

Returns the framework url if it is accessible.

This can be useful if you want to share a javascript library between several application instances. You would then put the library in the framework/js/ directory.

In this case, calling this function from any application instance will return the actual library url.

// Get framework url http://example.com/framework/ $framework_url = $this->app->tk_url(); // Create variable with value: http://example.com/framework/js/mylib.js $js_lib_url = $this->app->tk_url() . 'js/mylib.js';

This assumes the framework is located in the /framework directory under your web document root directory. Refer to the setup guide for other setups