unchanged
Title
How to use a Widget as an Action Provider
##What is an Action Provider Widget? As written on the [API docs](http://www.yiiframework.com/doc/api/1.1/CWidget#actions-detail "actions"): 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 weneed to createcreating an action named **getData** thatissupposed to be shared among the whole project and saved with the name **getData.php** on our **protected/components/actions** folder. ~~~ [php] 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**. ~~~ [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. ~~~ [php] // 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: ~~~ [php] index.php?site/test.GetData ~~~ ###Passing Parameters to Actions As you all know, and already explained in the wiki [Actions Code Reuse with CAction](http://www.yiiframework.com/wiki/170/actions-code-reuse-with-caction/ "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](http://www.yiiframework.com/user/25057/ "Attilio") for pointing this out): ~~~ [php] // 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', 'getData'=>array( // property1 must be a public variable // on getData CAction class 'property1'=>'value1', ), ); } ~~~