Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a custom Yii Component

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?

like image 486
KyleVan Avatar asked Aug 23 '11 14:08

KyleVan


People also ask

What are Yii components?

Yii applications are built upon components which are objects written to a specification. A component is an instance of CComponent or its derived class. Using a component mainly involves accessing its properties and raising/handling its events. The base class CComponent specifies how to define properties and events.

Is Yii hard to learn?

Yii has a steep learning curve, making it somewhat harder for beginners to get started. It also requires greater attention during development as one mistake in code can cause issues in the entire application.

What is difference between Yii and Yii2?

Conceptually Yii1 and Yii2 are quite similar, however Yii2 runs on newer PHP versions and utilizes namespaces, traits etc. Yii2 has also support for dependency injection through $container singleton available after initializing the framework.


2 Answers

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.

like image 153
ddinchev Avatar answered Sep 17 '22 03:09

ddinchev


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.

like image 27
ldg Avatar answered Sep 20 '22 03:09

ldg