使用事件触发
打赏作者

Gavin1024

执行 php artisan event:generate 并没有生成对应的Listeners下的SendEmailRegister文件 然后这个错误是怎么意思呢
···
[Symfony\Component\Console\Exception\LogicException]
An option with shortcut “q” already exists.
···

JellyBool 回复 Gavin1024

可能是有点小小的变化,你把你的 events 配置贴出来看看,还有你执行的 artisan 命令,报错的具体信息等

Gavin1024 回复 JellyBool

这是执行的命令
还有这是event的provider
同样的行创建的 ** make:listener --event=UserRegitsered SendWelcomeEmail**也一样

JellyBool 回复 Gavin1024

你的 laravel 版本具体是多少?在这之前有没有注册过其他的自定义的命令?这是其他的 artisan 命令也有问题还是这样 event 命令出现问题?命令不是应该这样么?

make:listener SendWelcomeEmail --event=UserRegitsered
Gavin1024 回复 JellyBool

我用的5.3的 并没有注册其他自定义的命令 其他命令都没有这个问题 我试了下发现5.2是可以的 这个有点奇怪 我再去找找

JellyBool 回复 Gavin1024

5.3 在我这里是没有问题的。。。你重新创建一个新的 5.3 版本项目试试。确定一下是不是框架的问题

Gavin1024 回复 JellyBool

好的 我试试

824286145
<?php

namespace App;
//use App\Events\User;
use App\Events\UserRegistered;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public static function register(array $attributes)
    {
        $user = static::create($attributes);
        event(new UserRegistered($user));
        return $user;
    }
}

824286145
ErrorException in UserRegistered.php line 18: Argument 1 passed to App\Events\UserRegistered::__construct() must be an instance of App\Events\User, instance of App\User given, called in /Users/fanqiang/PHP/www/laravel/laravist/user-signup/app/User.php on line 31 and defined
824286145
<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

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

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return [];
    }
}

JellyBool 回复 824286145

命名空间没对吧,在 UserRegistered 声明一下 use App\User;

824286145 回复 JellyBool

可以了,谢谢

824286145

怎么 发表一次 出来这么多?

mostwin

laravel的事件触发 和 队列在处理视频中需求,效果不是一样的吗?都是解耦,有什么其他的不同吗?

JellyBool 回复 mostwin

哎,要是说解耦的目的的话,哪个不是呢

f4cklangzi

把一些逻辑放到Model里面然后Model就变得很臃肿了…

f4cklangzi

为什么在监听器中使用Mail要注入进来而不是直接Mail:send()呢?