Skip to content
Barry O'Donovan edited this page Nov 19, 2012 · 1 revision

OSS_Controller_Aciton is a simple extension of Zend_Controller_Action which introduces the ability to add in traits which add new functionality.

Some of the basic things OSS_Controller_Action does is:

  • sets the protected variable $_options and the Zend_Registry element options containing the array of options from application.ini;
  • sets the Redirector helper so that it exits immediately after a _redirect(). Note there is also a defined function in this class called redirectAndEnsureDie() which also ensures this;
  • if the action name starts cli, it ensures that the action is called from a CLI context and not a web context;
  • finally it initialises the traits.

You would not extend OSS_Controller_Action directly in your own actions but rather create a library (similar to OSS-Framework) for your own application and in this create a XXX_Controller_Action class that extends OSS_Controller_Action and that would in turn be extended by your actions. You do this so you can introduce the traits you want.

Traits

Traits were introduced with PHP 5.4 allowing for reusable code blocks outside of the standard OOP inheritance model. They are incredibly useful as you will see here. Let's start with an example trait called OSS_Controller_Action_Trait_Logger (see OSS_Logger) which introduces a getLogger() function allowing any action to log messages by:

$this->getLogger()->err( 'There has been a big error! );

A base template for a trait looks like:

trait OSS_Controller_Action_Trait_Logger
{
    /**
     * The trait's initialisation method.
     *
     * This function is called from the Action's contructor and it passes those
     * same variables used for construction to the traits' init methods.
     *
     * @param object $request See Parent class constructor
     * @param object $response See Parent class constructor
     * @param object $invokeArgs See Parent class constructor
     */
    public function OSS_Controller_Action_Trait_Logger_Init( $request, $response, $invokeArgs )
    {
        $this->traitSetInitialised( 'OSS_Controller_Action_Trait_Logger' );
    }
       
    // add properties and members that this trait will add to OSS_Contoller_Action 
}

In your XXX_Controller_Action, you would then use this trait by:

use OSS_Controller_Action_Trait_Logger;

The constructor of OSS_Controller_Action will iterate over all declared traits in turn calling their ``OSS_Controller_ActionTrait_XXX_Init()function (and this function should always calltraitSetInitialised()` at the end just before returning.

Other trait functions available from OSS_Controller_Action are:

You can see a full list of available traits here.

Clone this wiki locally