Laravel普通的事件&窃听器

原创
小哥 3年前 (2022-11-21) 阅读数 11 #大杂烩

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Events\Register;
use DB;

class DemoController extends Controller
{
public function register(Request $request)
{
DB::table(users)->where(id,1)->update([name=>lisi]);

event(new Register(1));
return 33;
}

}

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Event;
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,
    ],
    App\Events\OrderShipped => [
    App\Listeners\SendShipmentNotification,
    ],
    // 用户注册后的事件
    App\Events\Register => [
    // 发送广告邮件
    App\Listeners\SendAdMail,
    // 发送短信
    App\Listeners\SendSms,
    // 发送帮助信息
    App\Listeners\SendHelpInformation,

],
];

/**

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

//
}
}

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class Register
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $uid;

/**

  • Create a new event instance.
  • @return void
    */
    public function __construct($uid)
    {
    $this->uid = $uid;
    }

/**

  • Get the channels the event should broadcast on.
  • @return \Illuminate\Broadcasting\Channel|array
    */
    public function broadcastOn()
    {
    return new PrivateChannel(channel-name);
    }
    }
版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除