Laravel覆盖一个包的事件

huangapple go评论60阅读模式
英文:

Laravel override a package event

问题

是否可以覆盖一个由包提供的事件?

例如

通过Composer安装到vendor目录的包"laravel/cashier-mollie"有一个事件laravel\cashier-mollie\src\Events\OrderPaymentFailed

我想要做的是当这个事件被触发时更新我的数据库中的特定字段。

有什么想法吗?

英文:

Is it possible to override a package provided event?

For example

Package "laravel/cashier-mollie" which is installed via composer into the vendor dir has an event laravel\cashier-mollie\src\Events\OrderPaymentFailed

What I want to do is update a specific field in my database when this event is fired.

Any ideas?

答案1

得分: 3

你可以在 app/Providers/EventServiceProvider.php 中为事件添加自定义监听器。

英文:

You can add your own Listener for the event in app/Providers/EventServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use Laravel\Cashier\Events\OrderPaymentFailed;
use App\Listeners\OrderPaymentsFailedListener;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        OrderPaymentFailed::class => [
            // Your custom listener
            OrderPaymentsFailedListener::class,
        ],
        
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        //
    }
}

huangapple
  • 本文由 发表于 2020年1月6日 23:56:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定