Laravel Octane 事件监听器未触发

发布于 2025-01-10 19:50:28 字数 2053 浏览 4 评论 0原文

我正在使用 laravel sail 在 swoole 上运行 laravel Octane。

我对我的 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 optimization、重新加载辛烷、停止辛烷并再次启动。甚至停止 docker 容器并重新启动它们。什么都不起作用。

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

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

I'm using laravel sail to run laravel octane on swoole.

I made a change to my 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);
    }
}

Here is my listener:

<?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]);
    }
}

No matter what I try this event listener is not being fired. I've tried clearing the cache, composer dump-autoload, php artisan optimize, reloading octane, stopping octane and starting it again. Even stopping the docker containers and starting them again. Nothing works.

It logs the list of listeners (including the CreateUserTeam listener) and it will fire the Email notification but not the other listener.

If anyone has any insight... please. I am at wits-end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2025-01-17 19:50:28

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

像你一样添加了我的监听器:

    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' => '[email protected]',
            '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);
    }
}

并在帆内运行我的测试:

sail output

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

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

I created same situation with you,

Added my listener like you did:

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

Used Laravel's registration test for it:

<?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' => '[email protected]',
            'password' => 'password',
            'password_confirmation' => 'password',
        ]);

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

Here is my test listener

<?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);
    }
}

And run my test inside the sail:

sail output

As you can see it runs handle function inside TestListener.php

Can you remove you other code parts inside handle function and try like this? I think you may have some error in your code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文