waController

Processing of HTTP requests without mandatory response

Contents...

As a rule, controllers based on class waController are not supposed to return anything in response to received HTTP request and rather usually perform some hidden actions; e.g., update database contents, save data to files, send requests to remote resources. However, when necessary, such a controller is capable of sending any response; e.g., using echo or other similar methods.

If you need to return HTML code or a JSON string in response to an HTTP request, then it might be more convenient use a controller based on classes waViewAction, waViewActions, waViewController, waJsonController, waJsonActions.

See explanation of HTTP request routing in backend and in frontend.

Main request processing logic must be described in public method execute().

Controllers & actions

A product may have both a controller and an action class with the same combination of module_id and action_id. In this case a controller has the priority over an action for processing an HTTP request. An action in this case can be called only from within a controller when necessary.

Below is shown an example of such a controller/action couple:

Methods

public function appSettings ($name, $default = '')

Returns app settings values.

Parameters

Example

$app_settings = $this->appSettings();

public function configPath ($name, $custom = false)

Returns path to app's configuration file.

Parameters

Example

$custom_workflow_config = include $this->configPath('workflow.php', true);

public function getApp ()

Returns controller app ID.

Example

$app_id = $this->getApp();

public function getConfig ()

Returns current instance of class (waSystemConfig) responsible for managing system configuration.

Example

$current_domain = $this->getConfig()->getDomain();

public function getPluginRoot ()

Returns path to plugin source files directory, if the controller is part of an app plugin.

Example

$plugin_dir = $this->getPluginRoot();

public function getResponse ()

Returns current instance of class responsible for preparing response to HTTP requests.

Example

$this->getResponse()->redirect($url);

public function getRights ($name = null, $assoc = true)

Returns information on authorized backend user's access rights.

Parameters

Example

$can_edit = $this->getRights('edit');

public function getStorage ()

Returns current instance of class responsible for managing PHP sessions.

Example

$my_session_var = $this->getStorage()->get('my_session_var');

public function getUser ()

Returns current instance of class responsible for managing authorized backend users.

Example

$user = $this->getUser();

public function getUserId ()

Returns authorized backend user ID.

Example

$user_id = $this->getUserId();

public function logAction ($action, $params = null, $subject_contact_id = null, $contact_id = null)

Logs information on a user action to system storage.

Parameters

Example

$this->logAction(
    'page_edit',
    array(
        'id'   => 12,
        'name' => 'About us'
    ),
    null,
    $this->getUserId()
);

protected function preExecute ()

Performs arbitrary additional actions before main controller logic. This method must be implemented by the developer, if necessary.

public function redirect ($params = array(), $code = null)

Redirects a user to specified URL.

Parameters

Example

// URL as a string
$this->redirect('https://'.$this->getConfig()->getDomain(), 301);

// URL as an array of GET parameters
$this->redirect(array(
    'module' => 'pages',
    'action' => 'edit',
    'id'     => $page_id,
));

public function storage ($name)

Returns, updates, or clears values of PHP session variables.

Parameters

Example

//getting variable value
$this->storage('my_var');
    
//clearing variable value
$this->storage('my_var', null);

//updating variable value
$this->storage('my_var', $new_value);