Help with Configuration Parameters for Custom Component

I'm creating a custom component that requires an custom parameter.

I've created the entry in main.php as follows:

	// application components

	'components' => array(

		'myComp' => array(

			'class' => 'CMyComponent',

			'url' => 'http://wsdlurl',

			'key' => 'xxxxxxx',



I've created my class extending CComponent.

My question is, what do I have to do to access those two config params (url and key)?

Are they created automatically? Do I need to create getters and setters for each? What are the relationships between member variable names and the parameter names if any?

I've been looking through the code and doc and I'm just lost.

Any help would be much appreciated.

You just need to declare 'url' and 'key' public member variables in your component class, and they will be filled with the supplied values. You may also define such properties using getter/setter.

Thanks, but that doesn't seem to work. Here's my class…


class CMyComponent extends CComponent


	public $key = '';

	public $url = '';	

	public function init()


		//	Call daddy...




	public function getKey()


		return( $this->key );



	public function getUrl()


		return( $this->url );



	public function setKey( $sKey )


		$this->key = $sKey;



	public function setUrl( $sUrl )


		$this->url = $sUrl;




In addition, my init() function doesn't ever seem to be called… Maybe that's the problem?

  1. You should extend CApplicationComponent.

  2. By default, the component will be created/initialized only when you call Yii::app()->myComp for the first time. If you want it to be created all the time, you should list its ID 'myComp' in the 'preload' property of app.

Qiang, thanks! I got it working with the getters and setters.

Just so I'm clear…

If I provide public members that match the parameter names in main.php, they will be automatically set. If the member names do not match, then I must provide getters and setters? And by match I mean same case, same name (i.e. 'key' in params file matches public $key in php file). Or must I always provide a getter and setter for a member variable/config parm?

Is that correct?

Yes, if you provide public members, you don't need getter or setter. The underlying for this configuration is very simple. It does the following:


where $name refers to the configuration array keys, and $value the corresponding array values.