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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化