Component initialization


I am little confused in the following scenario… I will be glad if somebody help me make it the right way…:

I have a Yii web application. :) It has one controller and several actions. Actions are defined in classes and the controller actually contains only one public member variable $actions which I use to define them in the controllerMap in the application config file.

I need a convenient way to initialize these actions. There is no init method like in the widgets… And I can't use the constructor of the action class, because on construction time, the properties of the action are not set yet and basically no initialization could be done then, and even more - the constructor should be parametrized…

I need a way to call the init method right after Yii::createComponent creates the instance and set all the properties of the created object.

I've read in the reference, that I can customize the core classes in the Yii class If I wish, so I try to did it with the createComponent class - I've added a call to newly created object's init method (if there is one)… But I couldn't inherit the createComponent from the YiiBase because it should accept variable parameters length… I had to just copy and paste the implementation, and I am pretty sure that this is not good decision…

So I need a better solution now - all I need is a way to initialize all created components, including actions after their properties have been set…

Thanks in advance… :)

When you declare actions in actions() method, you can provide initial property values to an action, and Yii will automatically set the properties right after the action is created. If this is not enough, you can put the initialization code at the beginning of its run() method.

Thanks for the fast reply, but that is not enough in this case:

I want to aggregate another component in the action, by specifying a provider array in the action configuration. I want it to be created before run method, but I can't initialize it if it is not created. That's why I need a method, that will create its instance before the run method is called…

I want to specify aggregated component class in the action configuration, because this way it is powerfull and gives me the freedom to change this aggregated component class on the next use of this action without change the action at all… (Let's say that this aggregated component is somekind of implementation of the Strategy desgin pattern)

(For me this is simmilar to Unity application block in Enterprise Library - well not the same, but simmilar enough)…

not sure if i understand you correctly, but you may try the following:

class MyAction extends CAction


    public $components=array();

    public function run()


          foreach($this->components as &$component)


          // your action main code here

          // you can access the components via $this->components[$id]



In your controller, you may need to configure the 'components' property of the action like you do with application components.