doutao1939 2015-09-07 14:47
浏览 28
已采纳

Laravel 5.1重用事件

I have setup an event in Laravel 5.1 so that when a user is created, this event creates a user role (by creating a new row in the role_user table in the database).

I now want to setup a new event for when a user gets updated called UserUpdated. However when this new event is called, I want to reuse the listener I created for the UserCreated event which is called AssignRole.

I have copied the listener below:

<?php

namespace SimplyTimesheets\Listeners\User;

use App\Events\User\UserCreated;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Repos\User\UserRepoInterface;
use App\Repos\User\RoleRepoInterface;

class AssignRole
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct(UserRepoInterface $user, RoleRepoInterface $role)
    {
        $this->user = $user;
        $this->role = $role;
    }

    /**
     * Handle the event.
     *
     * @param  UserCreated  $event
     * @return void
     */
    public function handle(UserCreated $event)
    {
        $user = $this->user->findUserById($event->user->id);
        $role = $this->role->findRoleById($event->request->role_id);

        return $user->roles()->save($role, ['cust_id' => $event->user->cust_id]);
    }
}

How can I reuse AssignRole in my new event handler, UserUpdated?

  • 写回答

1条回答 默认 最新

  • drbmhd9583 2015-09-07 15:07
    关注

    Don't typehint the UserCreated event type. You lose a little bit of control, but it'll help you achieve what you want.

    <?php
    
    namespace SimplyTimesheets\Listeners\User;
    
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use App\Repos\User\UserRepoInterface;
    use App\Repos\User\RoleRepoInterface;
    
    class AssignRole
    {
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct(UserRepoInterface $user, RoleRepoInterface $role)
    {
        $this->user = $user;
        $this->role = $role;
    }
    
    /**
     * Handle the event.
     *
     * @param  $event
     * @return void
     */
    public function handle($event)
    {
        $user = $this->user->findUserById($event->user->id);
        $role = $this->role->findRoleById($event->request->role_id);
    
        return $user->roles()->save($role, ['cust_id' => $event->user->cust_id]);
    }
    }
    

    Then, in your EventServiceProvider, you can assign the same listener to multiple events:

       'App\Events\User\UserCreated' => [
            'App\Handlers\Events\AssignRole@handle',
        ],
        'App\Events\User\UserUpdated' => [
            'App\Handlers\Events\AssignRole@handle',
        ],
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料