Usage of CWidget as action provider ?

Hi @all,

How can I use the action method in the my own Widget class (extend CWidget) ?

currently I have this code:

in widget class

public static function actions()


return array(






And in my controller class:

 public function actions()


  return array(







So I can use the Action of my Widget with the url “index.php?r=MyController/pro2.mymethod

Is that correct?

But how can I pass (not static) values from my Widget to the Action?

The Action has the MyController as controller object and not the Widget.

But I wanted to pass values from my Widget to the CAction object.

I knew that I can pass values via Url via GET but I thought there must be another way.

Or should I use session values for the widget and read them out in the action again to pass my data?

Is it also possible to have action methods in the Widget, so that a function of the widget is called and not the 'run' method, e.g. to return some json data.

hope its clearly what I want.

regards Horizons

Do you mean Yii::app()->controller->action?

Yii::app()->controller->action is the Caction class object which is used from my widget.

What I wanted was to create some widget, which gets some properties e.g. also a model in the view of the controller.

Something like below:



       'attribute' => "CompanyID",





  $ACSelect= $this->createWidget('application.extensions.acselect.ACSelect',$ac_select_options);


This widget should display data which should be requested via a ajax call.

Then I wanted to get the ajax request outside of my controller class.

And thought it would be best fit into the widget class.

Therefore I created a CAction class which is used in my widget which should get the requested information of the model and return it via json.

My only wish is to be able to set the parameters or properties in the view and not in the action method of my controller class.

e.g. to be able to pass 'ac_search_columns'=>array('Company'), from my widget to the CAction class which I have created.

Not sure if I fully understood you. I guess you mainly want to pass dynamic parameters from a widget (in the previous request) to a widget action (in the current request). If that is the case, I think you don't have much choice because they are in two different requests, and you should resort to some persistent data storage, such as GET, session or DB.

Thats indeed what I wanted.

Sadly there is no other way then Get or Session data.

Hmm if i wanted to pass data to the action object i also must pass a session name of the widget via get to have the possibility to read out the session value of my widget (widget could be used multiple times on a form).

Am i right?

Is the no possibility to call action methods of the widget object? (e.g. ajax call to the widget)

I mean whats the point of the widget class as an action provider if the actions has no connect to the widget ?

Maybe I am also doing something wrong?

Could you post a simple demo of a action used in a widget which acts as an action provider?

If you are using POST to call the action, you may consider CHtml::statefulForm() (check the hangman demo for its usage). That's another way of persistent data storage.

I don't have a demo for widget action at the moment…

Sadly my Action should only return some values of a query via json on a ajax request.

I wanted it to be independent as possible from a controller and modifications to it.

Cause the widget should have most of the code for displaying the widget and its data, depending on the properties set on creation of the widget.

In the Api there is this sentence:

'When a widget uses several actions, you can declare these actions using this method. The widget will then become an action provider, and the actions can be easily imported into a controller. '


So the Actions are really only imported into the Controller and are not called via the widget object :(

You can also use POST together with AJAX, I think.

I´m back again trying to solve my issue.

Sure I can use "POST" instead of "GET" for ajax.

But with a action used from a widget the url is completely different from the url when i am showing a form through the controller

e.g. index.php?r=MyController/update&id=11

and the url of the action of the widget would be something like this


Also if I want to store data via a stateful form it would mean that I must submit the form once.

But If I open the form and then use a ajax through my widget no form post has been done.

Guess my only possibility is to use sessions, to store the values.

You have written in some posts below "I guess you mainly want to pass dynamic parameters from a widget"

What would be if the parameters would not be dynamic?

Could I pass "static parameters" from the widget to the Widget Action which i define in the rendered view?

Guess I will now try the session Possibility.