Laravel事件和侦听器

时间:2020-08-10 07:49:10

标签: laravel laravel-7 laravel-events

这是我使用时的事件类

class EventServiceProvider extends ServiceProvider
    {
        /**
         * The event listener mappings for the application.
         *
         * @var array
         */
        protected $listen = [
            Registered::class    => [
                SendEmailVerificationNotification::class,
            ],
            CategoryEvent::class => [
                CategoryCreatedListener::class,
            ]
        ];

当我运行以下命令时

php artisan event:generate

如果我将名称空间放在事件和监听器之前,它将在服务提供商目录中同时创建事件和监听器

App\Events\CategoryEvent::class

它仍然在服务提供商目录中创建 App \ Event和App \ Event ,以在App命名空间中创建事件和侦听器目录。

enter image description here

2 个答案:

答案 0 :(得分:0)

Laravel似乎没有为事件生成提供自定义路径支持。因此,最好的选择是在所需目录中手动创建它,然后将其自己插入EventServiceProvider中。这是一些手工工作,但不需要花费一分钟以上的时间。

类似的问题已经解决了:How do I create Events/Listeners with artisan command at sub-directory level?

答案 1 :(得分:0)

听起来像是命名间隔问题。您可能要确保这些类是别名:

use App\Events\Registered;
use App\Listeners\SendEmailVerificationNotification;
...

否则Registered::class将是App\Providers\Registered