How to use a Widget as an Action Provider

  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
  5. Access Rules

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?r=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',
           ),
        ),
    );
}

Access Rules

Also, if you have implemented the accessRules method inside the controller, then you'll need to make sure that you allow the action provider in the accessRules method, otherwise you will receive 404 error when trying to view the page. Something like this will do the trick:

public function accessRules()
    {
        return array(
            array('allow',
                'actions' => array('test.getData'),
                'users' => array('*'),
            ),
            array('deny', 
                'users' => array('*'),
            ),
        );
    }

24 0
41 followers
Viewed: 61 143 times
Version: 1.1
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