Event讲解
打赏作者

dope2008

问想一下,我后台推一个消息给用户,用户在不刷新的前提下,怎么样得到提醒。不要ajax不停的访问页面

cnsecer

5.3里没有events这个文件夹了

JellyBool 回复 cnsecer

你创建 events 就有了,默认去掉了的。在 5.3 的系列也有讲到

laravel0304

05:17 秒

public function __construct(Filesystem $filesystem){
}

这里弄错了吧? 我测试的这里的构造方法不能被注入,event触发的时候会报错。在handle里也无法使用。

JellyBool 回复 laravel0304

我看了一遍视频,理论上是没有错的。你的报错是什么?

laravel0304 回复 JellyBool

理论上是没有错。但是,使用event() 触发事件的时候,如果在监听类的__construct()里传入任何参数就会报错,包括你实例中的$filesystem。 应该是框架内部在new()这个类的时候没有传参.
new之后直接调用了handle() 在handle里注入所需的参数。所以此处的析构方法可以忽略掉,handle就相当于析构了。

JellyBool 回复 laravel0304

我刚刚又去写了一遍代码,貌似还是没有什么错。不知道是我理解错你的意思还是?
1.UserSignUpListener(这是你说的监听类吧):

class UserSignUpListener
{
    public $filesystem;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct(Filesystem $filesystem)
    {
        $this->filesystem = $filesystem;
    }

    /**
     * Handle the event.
     *
     * @param  UserSignUp  $event
     * @return void
     */
    public function handle(UserSignUp $event)
    {
        var_dump($this->filesystem->get(public_path('robots.txt')));
    }

2.UserSignUp (事件):

class UserSignUp
{
    use InteractsWithSockets, SerializesModels;

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

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }

3.使用 event() 触发:

 $user = \App\User::find(8);
 event(new \App\Events\UserSignUp($user));

我这里依然没有什么错,代码还是正常执行。

所以你的代码和报错具体是什么?

laravel0304 回复 JellyBool

大哥我错了,我use的是

use League\Flysystem\Filesystem;

这个没注册,所以报错:

Target [League\Flysystem\AdapterInterface] is not instantiable while building [App\Listeners\TaskListener, League\Flysystem\Filesystem].

改成:

use Illuminate\Filesystem\Filesystem;

就OK了,注入User 也没问题。
唉,自己2B了让站长大人忙活半天,真不好意思,以后我会搞清楚了再发问。
就冲站长这态度继续支持!

JellyBool 回复 laravel0304

嗯哼,我以为是 laravel 5.3 的新版有了新的注入规则。所以还是贴上代码和报错信息快。

super86worm

请教一下,文档中看到

Event::listen('event.*', function ($eventName, array $data) {
    //
});

怎么使用*通配符去监听事件呀,google了半天也没找到这种用法的例子,谢谢。

JellyBool 回复 super86worm

判断 eventName 吧:

Event::listen('event.*', function ($eventName, array $data) {
    if ($eventName === 'your-event-name') {
    }// or switch
});
super86worm 回复 JellyBool

谢谢,懂得怎么用了,使用这种这种方式监听事件时,触发事件时候直接用就可以了,非常感谢你的教程。

$user = App\User::find(1);
event('event.user_login',$user);
JellyBool 回复 super86worm

嗯哼,对的啊。

a359611223

5.4自动创建变成这样了

<?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 UserSignUp
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}


Cyclone

按着老师您说的步骤试着写了下但是一直有这个错误。

FatalThrowableError in UserSignUp.php line 21:
Type error: Argument 1 passed to App\Events\UserSignUp::__construct() must be an instance of App\Events\User, instance of App\User given

后来发现是因为UserSignUp里面没有引入User类。。。

但是写__construct(User $user)的时候PS并没有提示。。还以为也会自动引入了

JellyBool 回复 Cyclone

命名空间没给对而已