How to use a Widget as an Action Provider

You are viewing revision #6 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.

« previous (#5)next (#7) »

  1. What is an Action Provider Widget?
  2. Step 1: Create the Action Class
  3. Step 2: Configure the Widget
  4. Step 3: Configure the Controller

What is an Action Provider Widget?

As written on the API docs: An Action Provider Widget is a widget that has declared several actions by using its 'public static function actions()'. After, a Controller can easily import all its declared actions on its 'public function actions()'.

Step 1: Create the Action Class

For the sake of the article we need to create an action named getData that is supposed to be shared among the whole project and saved with the name getData.php on our protected/components/actions folder.

class getData extends CAction{
    public function run(){
        echo 'HELLO WORLD';
    }
}

Step 2: Configure the Widget

To transform a Widget into an action provider is quite easy (once you know of course). The only thing we need to do is to set the static function actions(). As you will see on the following code, we name the action as GetData and that is the action that will be called in our route. We are going to save the following widget in our protected/components/ folder with the name testProvider.php.

class testProvider extends CWidget{
    public static function actions(){
        return array(
           // naming the action and pointing to the location
           // where the external action class is
           'GetData'=>'application.components.actions.getData',
        );
    }
}

Step 3: Configure the Controller

Finally we set our controller’s actions() function to point to our actions provider.

// This function is in this example
// on SiteController
public function actions()
{
   return array(
      // test. is the prefix we are going to use on our URL
      // for all actions within the actionProvider class
      // we point to the location where the provider
      // is.
      'test.'=>'application.components.testProvider',
    );
}

All done, now we can call all the actionProvider actions as controllerID/actionPrefix.actionID. Here is the example:

index.php?site/test.GetData
Passing Parameters to Actions

As you all know, and already explained in the wiki Actions Code Reuse with CAction, Actions are classes that extend from CAction, that is, a CComponent. If we wish to pass parameters to the actions that are within the provider, we have to do it this way when configuring your controller (step 3) (Thanks to Attilio for pointing this out):

// This function is in this example
// on SiteController
public function actions()
{
   return array(
      // test. is the prefix we are going to use on our URL
      // for all actions within the actionProvider class
      // we point to the location where the provider
      // is.
      'test.'=>array(
         'class'=>'application.components.testProvider',
         'getData'=>array(
            // property1 must be a public variable
            // on getData CAction class
            'property1'=>'value1',
           ),
        ),
    );
}
24 0
41 followers
Viewed: 61 114 times
Version: Unknown (update)
Category: How-tos
Written by: Antonio Ramirez
Last updated by: stinkytofu
Created on: Feb 18, 2011
Last updated: 9 years ago
Update Article

Revisions

View all history

Related Articles