Loading a custom Yii Component

KyleVan picture KyleVan · Aug 23, 2011 · Viewed 12.2k times · Source

I am trying to use a custom class I have created to send out mail so I can keep the controller files thin. I created my custom class and put it in the components folder. I then added:

'sendMail' => array(
    'class'=>'application.components.SendMail.',
), 

underneath main components in my main config file.

This should allow me to access the class directly correct? I try using:

Yii::app()->SendMail->MailerConfirmation();

and:

Yii:app()->MailerConfirmation();

and all I end up with is errors.

Can anyone tell me how I include a custom component? Maybe I am doing this all wrong?

Answer

ddinchev picture ddinchev · Aug 23, 2011

First it should be:

'sendMail' => array(
    'class'=>'application.components.SendMail',
), 

Notice the absence of dot in the end "SendMail" instead of "SendMail.". Also, this configuration expects that you have php file SendMail.php, in protected/components directory that is a class with name "SendMail" and that this component extends CApplicationComponent. The component id will be with lower first letter, eg Yii::app()->sendMail and this will return instance of "SendMail" class. I do not know what MailerConfirmation is, but if this is a method of SendMail object, then you should access it like Yii::app()->sendMail->MailerConfirmation()

If this doesn't help, then please post some code and post the errors you are getting.