【问题标题】:Laravel/Lumen | Failing to dispatch eventLaravel/流明 |未能分发事件
【发布时间】:2018-10-30 11:17:44
【问题描述】:

这是我第一次在 Laravel/Lumen 中使用事件。

我实际上正在使用 Lumen,并且我试图在新用户注册时发送一个 Mailable 实例以便在后台发送电子邮件。

我相信我设置正确,但我不断收到此错误...

类型错误:传递给 Illuminate\Mail\Mailable::queue() 的参数 1 必须实现接口 Illuminate\Contracts\Queue\Factory,给定 Illuminate\Queue\DatabaseQueue 的实例

我实际上无法在错误消息本身中看到问题的来源,例如没有行号。

但是,这是我的代码...

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!');
      }
}

【问题讨论】:

    标签: php laravel queue lumen


    【解决方案1】:

    我遇到了同样的问题。 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-&gt;register(\App\Providers\MailServiceProvider::class);

    【讨论】:

    • $app->make('queue');将工作,将其添加到您的 app.php
    猜你喜欢
    • 2015-04-13
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 2021-07-21
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多