Laravel/Lumen |发送事件失败
Laravel/Lumen | Failing to dispatch event
这是我第一次使用 Laravel/Lumen 中的事件。
我实际上正在使用 Lumen,并且我试图在新用户注册时分派一个 Mailable 实例,以便在后台发送电子邮件。
我相信我已经设置好了,但是我一直收到这个错误...
Type error: Argument 1 passed to Illuminate\Mail\Mailable::queue() must implement interface Illuminate\Contracts\Queue\Factory, instance of Illuminate\Queue\DatabaseQueue given
我实际上无法在错误消息本身中看到问题出在哪里,例如没有行号。
但是,这是我的代码...
AuthenticationContoller.php
$this->dispatch(new NewUser($user));
NewUser.php
<?php
namespace App\Mail;
use App\Models\User;
use Illuminate\Mail\Mailable;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class NewUser extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('test')->to('test@test.com', 'Test')
->from('test@test.com', 'test')->replyTo('test@test.com', 'test')
->subject('Welcome to the blog!');
}
}
我运行遇到了同样的问题。 Lumen 和 Illuminate/Mailer 似乎不能很好地协同工作。
但是我在 a Github thread 中找到了一个非常简单的修复方法。
基本上,您只需在 app/Providers 目录中创建一个新的服务提供商。
MailServiceprovider.php
<?php
namespace App\Providers;
use Illuminate\Mail\Mailer;
use Illuminate\Mail\MailServiceProvider as BaseProvider;
class MailServiceProvider extends BaseProvider
{
/**
* Register the Illuminate mailer instance.
*
* @return void
*/
protected function registerIlluminateMailer()
{
$this->app->singleton('mailer', function ($app) {
$config = $app->make('config')->get('mail');
// Once we have create the mailer instance, we will set a container instance
// on the mailer. This allows us to resolve mailer classes via containers
// for maximum testability on said classes instead of passing Closures.
$mailer = new Mailer(
$app['view'], $app['swift.mailer'], $app['events']
);
// The trick
$mailer->setQueue($app['queue']);
// Next we will set all of the global addresses on this mailer, which allows
// for easy unification of all "from" addresses as well as easy debugging
// of sent messages since they get be sent into a single email address.
foreach (['from', 'reply_to', 'to'] as $type) {
$this->setGlobalAddress($mailer, $config, $type);
}
return $mailer;
});
$this->app->configure('mail');
$this->app->alias('mailer', \Illuminate\Contracts\Mail\Mailer::class);
}
}
然后您只需在 bootstrap/app.php
中注册此服务提供商,而不是通过添加以下行的默认服务提供商:
$app->register(\App\Providers\MailServiceProvider::class);
这是我第一次使用 Laravel/Lumen 中的事件。
我实际上正在使用 Lumen,并且我试图在新用户注册时分派一个 Mailable 实例,以便在后台发送电子邮件。
我相信我已经设置好了,但是我一直收到这个错误...
Type error: Argument 1 passed to Illuminate\Mail\Mailable::queue() must implement interface Illuminate\Contracts\Queue\Factory, instance of Illuminate\Queue\DatabaseQueue given
我实际上无法在错误消息本身中看到问题出在哪里,例如没有行号。
但是,这是我的代码...
AuthenticationContoller.php
$this->dispatch(new NewUser($user));
NewUser.php
<?php
namespace App\Mail;
use App\Models\User;
use Illuminate\Mail\Mailable;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class NewUser extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('test')->to('test@test.com', 'Test')
->from('test@test.com', 'test')->replyTo('test@test.com', 'test')
->subject('Welcome to the blog!');
}
}
我运行遇到了同样的问题。 Lumen 和 Illuminate/Mailer 似乎不能很好地协同工作。
但是我在 a Github thread 中找到了一个非常简单的修复方法。
基本上,您只需在 app/Providers 目录中创建一个新的服务提供商。
MailServiceprovider.php
<?php
namespace App\Providers;
use Illuminate\Mail\Mailer;
use Illuminate\Mail\MailServiceProvider as BaseProvider;
class MailServiceProvider extends BaseProvider
{
/**
* Register the Illuminate mailer instance.
*
* @return void
*/
protected function registerIlluminateMailer()
{
$this->app->singleton('mailer', function ($app) {
$config = $app->make('config')->get('mail');
// Once we have create the mailer instance, we will set a container instance
// on the mailer. This allows us to resolve mailer classes via containers
// for maximum testability on said classes instead of passing Closures.
$mailer = new Mailer(
$app['view'], $app['swift.mailer'], $app['events']
);
// The trick
$mailer->setQueue($app['queue']);
// Next we will set all of the global addresses on this mailer, which allows
// for easy unification of all "from" addresses as well as easy debugging
// of sent messages since they get be sent into a single email address.
foreach (['from', 'reply_to', 'to'] as $type) {
$this->setGlobalAddress($mailer, $config, $type);
}
return $mailer;
});
$this->app->configure('mail');
$this->app->alias('mailer', \Illuminate\Contracts\Mail\Mailer::class);
}
}
然后您只需在 bootstrap/app.php
中注册此服务提供商,而不是通过添加以下行的默认服务提供商:
$app->register(\App\Providers\MailServiceProvider::class);