【问题标题】:Require command in Yii PHPYii PHP 中的 Require 命令
【发布时间】:2013-08-16 08:19:27
【问题描述】:

我正在使用 Yii 框架进行 Web 开发。

我正在尝试制作电子邮件 cron 工作,为此我正在使用 PHPMailer。

这是我的 config/console.php

> return array(
>     'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
>     'name'=>'My Console Application',
> 
>     // preloading 'log' component
>     'preload'=>array('log'),
>     'import'=>array(
>             'application.modules.*',
>             'application.extensions.*',
>             'application.components.*',
>             'application.models.*',
>         ), ....

我的command/testCommand.php

class UptimeCheckerCommand extends CConsoleCommand{
    public function run($args) { 
.... 
$warning->send(); 
....

我的组件/Warning.php

....
require("/protected/extensions/PHPMailer/class.phpmailer.php");
....

错误报告:

PHP Error[2]: require(/protected/extensions/PHPMailer/class.phpmailer.php): fail
ed to open stream: No such file or directory

我已经使用控制器对组件进行了测试,它工作得非常好。该错误仅在我尝试使用 yiic test 命令访问它时发生。

任何帮助将不胜感激

【问题讨论】:

  • 将require语句改成require("./protected/extensi...")会不会出现同样的错误?

标签: php yii phpmailer


【解决方案1】:
'import'=>array(
   ...
   'application.extensions.PHPMailer.*',
   ...
)

并将文件 class.phpmailer.php 重命名为 PHPMailer.php

导入目录不会导入其任何子目录。

【讨论】:

    【解决方案2】:

    试试这样:

    $mailer = Yii::import('application.extensions.phpmailer'); 
    

    然后使用 $mailer发送电子邮件

    【讨论】:

      【解决方案3】:
      Yii::import('application.extensions.phpmailer'); 
      

      那么你可以这样创建一个对象:

      $mail = new PHPMailer();
      

      【讨论】:

        【解决方案4】:

        感谢您的快速回复。

        我已经尝试了上面的一些建议。

        Andrey Shatilov 的建议没有奏效。

        对于 usman allamma​​zraara 的建议进行更改:

        require("/protected/extensions/PHPMailer/class.phpmailer.php");

        Yii::import('application.extensions.PHPMailer.class.phpmailer.php');

        也不能工作,因为文件名中有点(class.phpmailer.php)

        因此,yii 认为我在 class 文件夹中请求 phpmailer.php 而不是 class.phpmailer.php

        有效的代码:

        require("../protected/extensions/PHPMailer/class.phpmailer.php");

        不知道为什么会这样,因为我在 /protected/command/testCommand.php 中的 testCommand.php 和 /protected/extensions/PHPMailer/ 中的我的 phpmailer 文件,但它有效。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-08-28
          • 2021-12-18
          • 1970-01-01
          • 1970-01-01
          • 2021-01-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多