1

我正在使用 laravel 帆在 swoole 上运行 laravel 辛烷值。

我对 EventServiceProvider 进行了更改:

<?php

namespace App\Providers;

use App\Events\SubscriptionCreated;
use App\Listeners\CreateUserTeam;
use App\Listeners\SendSubscriptionReceipt;
use App\Listeners\UpdateTeamFeatures;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
            CreateUserTeam::class
        ],
        SubscriptionCreated::class => [
            SendSubscriptionReceipt::class,
            UpdateTeamFeatures::class
        ]
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        \Log::debug("HERE", $this->listen);
    }
}

这是我的听众:

<?php

namespace App\Listeners;

use App\Models\Team;
use Illuminate\Auth\Events\Registered;

class CreateUserTeam
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        \Log::debug("HIT");
    }

    /**
     * Handle the event.
     *
     * @param  Registered  $event
     * @return void
     */
    public function handle(Registered $event)
    {
        $user = $event->user;
        $team = Team::create([
            'manager_id' => $user->id,
        ]);
        $team->createAsStripeCustomer();
        $user->update(['team_id' => $team->id]);
    }
}

无论我尝试什么,这个事件监听器都不会被解雇。我试过清除缓存、composer dump-autoload、php artisan optimize、重新加载辛烷值、停止辛烷值并重新启动它。甚至停止 docker 容器并重新启动它们。没有任何效果。

它记录侦听器列表(包括 CreateUserTeam 侦听器),它会触发电子邮件通知,但不会触发其他侦听器。

如果有人有任何见解......请。我束手无策

4

1 回答 1

0

我和你创造了同样的情况,

像你一样添加我的听众:

    protected $listen = [
    Registered::class => [
        SendEmailVerificationNotification::class,
        TestListener::class,
    ],
];

使用 Laravel 的注册测试:

<?php

namespace Tests\Feature\Auth;

use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class RegistrationTest extends TestCase
{
    use RefreshDatabase;

    public function test_registration_screen_can_be_rendered()
    {
        $response = $this->get('/register');

        $response->assertStatus(200);
    }

    public function test_new_users_can_register()
    {
        $response = $this->post('/register', [
            'name' => 'Test User',
            'email' => 'test@example.com',
            'password' => 'password',
            'password_confirmation' => 'password',
        ]);

        $this->assertAuthenticated();
        $response->assertRedirect(RouteServiceProvider::HOME);
    }
}

这是我的测试听众

<?php

namespace App\Listeners;

use Illuminate\Auth\Events\Registered;

class TestListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        \Log::debug("HIT");
        dump("test");
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(Registered $event)
    {
        $user = $event->user;

        dump($user->id);
    }
}

并在帆内运行我的测试:

帆输出

如您所见,它在 TestListener.php 中运行句柄函数

您可以删除句柄函数中的其他代码部分并尝试这样吗?我认为您的代码中可能有一些错误

于 2022-03-01T05:22:01.280 回答