Loading a custom Yii Component
Asked Answered
P

2

7

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?

Pollster answered 23/8, 2011 at 14:45 Comment(0)
L
12

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.

Laruelarum answered 23/8, 2011 at 15:30 Comment(0)
N
2

Note that if you are not going to set any component properties in the config file or use other CApplicationComponent features and your config file includes the default:

'import'=>array(
    'application.models.*',
    'application.components.*',
),

You can put your SendMail.php class in the components directory and it will autoload by calling it via:

$mailer = new SendMail();

then call your methods via:

$mailer->MailerConfirmation();

if you do want to use CApplicationComponent, you may want to look here or here for a couple examples, as well as the Yii tutorials.

Nickolenicks answered 23/8, 2011 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.