Actions & controllers

Contents...

Creating action classes

Action class files reside in application subdirectory wa-apps/{APP_ID}/lib/actions/. It is advisable to create a separate subdirectory for each module and to place files according to rule wa-apps/{APP_ID}/lib/actions/{MODULE}/{fileName}.php.

Actions can be classified into the following two types by the logic distribution principle in classes:

Multiple actions

If web pages generated by several actions have similar structure and simple design, it is convenient to store such actions in a single place. This approach to organizing actions is used in many popular frameworks including Zend Framework.

For this action organizing type class waViewActions is used. The class of a multiple action should extend this base class in the source code of an application. The names of methods implementing individual actions must end with the Action suffix.

Example of a multiple action class:

<?php

class guestbookBackendActions extends waViewActions
{

    // Default action
    public function defaultAction()
    {
        ...
    }
  
    // delete action
    public function deleteAction()
    {
        ...
    }

}

Single actions

If the generation logic of a certain web page is complex and requires being separated into several parts, it is advisable to arrange such an action as a separate class. This will allow you to avoid creating very large classes of type waViewActions.

Single action classes should extend system class waViewAction. The method implementing the particular action must be named execute.

Example of a single action class:

<?php

class myappOrderShowAction extends waViewAction
{

    public function execute()
    {
        ...
    }

}

Implementation of the action logic

The general operating logic of most simple actions works according to the following scheme: retrieve data from the model and pass them to the template engine to be displayed in the user browser.

Passing data to the template engine

When generating the HTML code of the resulting web page, the template engine processes the data received from the action. Data should be passed to the template engine using the following syntax:

$this->view->assign('var', $var);

Example of an action class showing how data received from the model are passed to the template file:

<?php

class guestbookFrontendAction extends waViewAction
{

    public function execute()
    {
        // Create a model instance to retrieve data from the DB
        $model = new guestbookModel();
    
        // Retrieve guestbook messages from the DB
        $records = $model->order('datetime DESC')->fetchAll();
    
        // Pass the retrieved data to the template file
        $this->view->assign('records', $records);
    }

}

Creating controller classes

Sometimes the default logic of a controller cannot provide the required functionality for an application. This is true for cases when the exact template file name (along with the related actions) of the generated web page depends on several conditions or when a user request does not result in generation of HTML code (e.g., redirection to another URL is performed). For such specific purposes a custom controller can be created.

A controller can be created using one of two base classes: waController and waViewController.

If no web page is generated as a result of the request processing, then the corresponding controller class should extend base class waController; otherwise base class waViewController should be used. The method implementing the request the request processing logic must be named execute.

Implementation of the controller logic

The most widely used operating logic of a controller without generating HTML code is to redirect a user to another URL after the requested actions have been executed. This scheme is often used, when a request is sent to update (delete, save, or modify) some portion of data. In this case the browser sends a POST request to the server which processes the received data and then redirects the user to another page where the updated data are displayed.

Redirection to another URL

Browser redirection to a different URL is carried out in controller source code using the following syntax:

$this->redirect('some_url');

Example of a controller class with redirection:

<?php

class someappRecordSaveController extends waController
{

    public function execute()
    {
        ...
        $this->redirect('some_url');
    }

}

Calling actions

An action is called within a controller using the following syntax:

$this->executeAction($action);

Example of a controller with generation of a new web page:

<?php

class someappRecordDoController extends waViewController
{

    public function execute()
    {
        if (waRequest::getMethod() == 'post') {
          $this->executeAction(new someappRecordPostAction());
        } else {
          $this->executeAction(new someappRecordGetAction());
        }
    }

}