Writing Actions

About class file names

Actions for Mivec Framework must be ended with the extension .action.class.php. Also, the class name must be the same as the file name. For example, we want to write a MyAction, the file name will have to be MyAction.action.class.php.1)

Adding required files

Then, we need to add this statement at the beginning:

require_once MIVEC_HOME . "action/WebAction.class.php";

We don’t need to care about what the value of the constant MIVEC_HOME is. It is defined at MivecServlet instead.

If you need additional required files, remember that the path of the file is relative to MivecServlet.php.

Action class declaration

Next, make the new action extends the WebAction class.

class RegisterUserAction extends WebAction

WebAction is the Action implementation specific for web applications, which add a pair of <span> tag around error messages when addError is called. See Validating User Input for details.

You may define an optional constructor for the class, fill it up with the necessary logic that, for instance, provide data for validation when the request parameters are injected, or just start up a database connection.

Define setter methods

Next, is to define the parameters this Action class can receive. Generally, Controller will identify methods that begin with “set” and call the method, using the value of the parameter named after “set”.

For example, when Controller sees a setUserID() method on the action, it will call setUserID() using the request parameter named “userID”.2)

For example:

function setUserID($userID)
{
    $this->userID = $userID;
}

Perform logic before actual command execution: prepare()

If you want to perform logic after request parameters are mapped to the Action, you may override the prepare() method. This method will be called before any command execution, so this method is good for performing common logic for different commands on the same action, but also requiring request parameters, e.g. retrieving a list of product categories.

function prepare()
{
    $this->categories = CategoryManager::getCategories($this->visible);
}

Define Action commands

By default, Controller will call the execute() method on the Action, which simply returns “success” for ViewFactory lookup the corresponding views from ActionMap.

However, in most cases this is not what we want. We can simply override this method, adding necessary business logic for processing data made available from Controller. For example:

function execute()
{
    UserManager::createNewUser($this->userID, $this->password);
    return SUCCESS;
}

Remember to return a string value for Controller to look for the page to forward/ redirect after the execution, via the ActionMap. Three default constants are defined:

Constant name Value
SUCCESS “success”
ERROR “error”
INPUT “input”

They should be enough for most cases.

Apart from the default execute() method, you may also define any other methods, and using the URL to map to that method. See Configuring Actions to View Mapping for details. For example,

function confirm()
{
    //Shows confirmation page, so just let the parameter passthrough...
    return SUCCESS;
}

Define values to be exposed to views

After the specifed command had been executed, Controller will retrieve the values available from the Action via its getter methods (i.e. the methods begin with “get”) and inject them into View for rendering.

The name of the parameter available to views is the string after the “get”. In other words, for the method “getRegistrationNumber()”, “registrationNumber” will be made available to View.

function getRegistrationNumber()
{
    return $this->registrationNumber;
}

Final output

Putting all above together, we will have an Action implementation like this:

<?php
 
require_once MIVEC_HOME . "action/WebAction.class.php";
 
class RegisterUserAction extends WebAction
{
    var $userID;
    var $password;
    var $registrationNumber;
 
    function setUserID($userID)
    {
         $this->userID = $userID;
    }
 
    function setPassword($password)
    {
         $this->password = $password;
    }
 
    function getRegistrationNumber()
    {
         return $this->registrationNumber;
    }
 
    function execute()
    {
         $this->registrationNumber = UserManager::createUser($this->userID, $this->password);
         return SUCCESS;
    }
 
    function confirm()
    {
        //Shows confirmation page, so just let the parameter passthrough...
        return SUCCESS;
    }
}
 
?>

« Prev: Framework Configuration Next : Validating User Input »

1) There’s an exception of reusing Action classes on different Action names, however. See Configuring Actions to View Mapping for details.
 
  devguide/writing_actions.txt · Last modified: 2005/04/22 06:10
 
Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki

SourceForge.net Logo