2016-02-07 18 views
0

我正在使用Laravel 5.0并试图设置一个命令来排队我的邮件服务中的作业。在服务中使用命令

我的问题是,我不知道如何将命令调度程序注入我的邮件服务。

我的结构:

  • 服务文件夹
    • Mailer.php(包含抽象类梅勒
    • UserMailer.php(包含类UserMailer扩展了梅勒类)

事情我已经尝试:

我试图使用DispatchesCommands的UserMailer类中和梅勒类中的特质。我也尝试将\ Illuminate \ Contracts \ Bus \ Dispatcher注入到UserMailer类的构造器中。

在所有三种情况下,我得到一个错误 “类服务\邮寄包装\ SendEmail未找到”

Mailer.php:

abstract class Mailer 
{ 
    public function emailTo($view, $mData) 
    { 

    Mail::queue($view, $mData, function($message) use ($mData) 
    { 
     //code here - not relevant 

     } 
    }); 
} 
} 

UserMailer.php:

use Illuminate\Contracts\Bus\Dispatcher as Dispatcher; 

Class UserMailer extends Mailer 
{ 

    protected $bus; 

    function __construct(Dispatcher $bus) 
    { 
    $this->bus = $bus; 

    } 

    public function sendIndividualEmail($members) 
    { 

     // code here not relevant... 

    $this->bus->dispatch(new SendEmail($members)); 

    return true; 
    } 

如何从我的UserMailer类中访问命令总线? TIA

回答

1

你应该导入完全合格的命名空间路径为SendEmail类,例如:

use Illuminate\Contracts\Bus\Dispatcher as Dispatcher; 

use Namespace\To\SendEmail; //<-- Add this "use" statement with correct namespace 

Class UserMailer extends Mailer 
{ 
    //... 
} 

因此,您可以在类中使用new SendEmail($members)。在您的代码中,PHP正在寻找Services\Mailers命名空间/文件夹中的SendEmail

+0

谢谢,我很感激。这正是我所错过的。 – retrograde

+0

不客气:-) –