【问题标题】:Loading a custom Yii Component加载自定义 Yii 组件
【发布时间】:2011-08-23 14:45:08
【问题描述】:

我正在尝试使用我创建的自定义类来发送邮件,这样我就可以保持控制器文件的精简。我创建了我的自定义类并将其放在 components 文件夹中。然后我补充说:

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

在我的主配置文件中的主要组件下方。

这应该允许我直接访问该类正确吗?我尝试使用:

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

和:

Yii:app()->MailerConfirmation();

我最终得到的只是错误。

谁能告诉我如何包含自定义组件?也许我做错了?

【问题讨论】:

    标签: php yii swiftmailer


    【解决方案1】:

    首先应该是:

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

    请注意末尾“SendMail”而不是“SendMail.”中没有点。此外,此配置要求您在 protected/components 目录中有 php 文件 SendMail.php,该目录是一个名为“SendMail”的类,并且该组件扩展了 CApplicationComponent。组件 ID 的首字母较低,例如Yii::app()->sendMail,这将返回“SendMail”类的实例。我不知道 MailerConfirmation 是什么,但如果这是 SendMail 对象的方法,那么你应该像Yii::app()->sendMail->MailerConfirmation()一样访问它

    如果这没有帮助,请发布一些代码并发布您遇到的错误。

    【讨论】:

      【解决方案2】:

      请注意,如果您不打算在配置文件中设置任何组件属性或使用其他 CApplicationComponent 功能并且您的配置文件包含默认值:

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

      你可以把你的 SendMail.php 类放在 components 目录中,它会通过调用它来自动加载:

      $mailer = new SendMail();
      

      然后通过以下方式调用您的方法:

      $mailer->MailerConfirmation();
      

      如果您确实想使用CApplicationComponent,您可能需要查看herehere 以获取一些示例以及Yii 教程。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-18
        • 1970-01-01
        • 1970-01-01
        • 2019-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-05
        相关资源
        最近更新 更多