Laravel模型活动-我对它们的去向有些困惑 [英] Laravel Model Events - I'm a bit confused about where they're meant to go

查看:79
本文介绍了Laravel模型活动-我对它们的去向有些困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的看法是,一个好的Laravel应用程序应该非常受模型和事件驱动.

So the way I see it is that a good Laravel application should be very model- and event-driven.

我有一个名为Article的模型.我希望在发生以下事件时发送电子邮件警报:

I have a Model called Article. I wish to send email alerts when the following events happen:

  • 创建文章时
  • 更新文章时
  • 删除文章时

文档说我可以使用模型事件并在<App\Providers\EventServiceProvider的c1>功能.

The docs say I can use Model Events and register them within the boot() function of App\Providers\EventServiceProvider.

但这让我感到困惑,因为...

But this is confusing me because...

  • 当我添加需要诸如所有自己的模型事件的完整集合的其他模型(如CommentAuthor)时,会发生什么? EventServiceProvider的单个boot()函数会绝对庞大吗?
  • Laravel的其他"事件的目的是什么?如果实际上我的事件只会响应Model CRUD动作,为什么还需要使用它们?
  • What happens when I add further models like Comment or Author that need full sets of all their own Model Events? Will the single boot() function of EventServiceProvider just be absolutely huge?
  • What is the purpose of Laravel's 'other' Events? Why would I ever need to use them if realistically my events will only respond to Model CRUD actions?

我是Laravel的初学者,来自CodeIgniter,因此尝试将自己的头脑围绕Laravel的正确做事方式.感谢您的建议!

I am a beginner at Laravel, having come from CodeIgniter, so trying to wrap my head around the proper Laravel way of doing things. Thanks for your advice!

推荐答案

最近,我在Laravel 5项目之一中遇到了相同的问题,在该项目中,我必须记录所有模型事件.我决定使用Traits.我创建了ModelEventLogger Trait,并将其简单地用在需要记录的所有Model类中.我将根据您的需要对其进行更改,如下所示.

Recently I came to same problem in one of my Laravel 5 project, where I had to log all Model Events. I decided to use Traits. I created ModelEventLogger Trait and simply used in all Model class which needed to be logged. I am going to change it as per your need Which is given below.

<?php

namespace App\Traits;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;

/**
 * Class ModelEventThrower 
 * @package App\Traits
 *
 *  Automatically throw Add, Update, Delete events of Model.
 */
trait ModelEventThrower {

    /**
     * Automatically boot with Model, and register Events handler.
     */
    protected static function bootModelEventThrower()
    {
        foreach (static::getModelEvents() as $eventName) {
            static::$eventName(function (Model $model) use ($eventName) {
                try {
                    $reflect = new \ReflectionClass($model);
                    Event::fire(strtolower($reflect->getShortName()).'.'.$eventName, $model);
                } catch (\Exception $e) {
                    return true;
                }
            });
        }
    }

    /**
     * Set the default events to be recorded if the $recordEvents
     * property does not exist on the model.
     *
     * @return array
     */
    protected static function getModelEvents()
    {
        if (isset(static::$recordEvents)) {
            return static::$recordEvents;
        }

        return [
            'created',
            'updated',
            'deleted',
        ];
    }
} 

现在,您可以在要为其引发事件的任何Model中使用此特征.对于您的情况,在Article模型中.

Now you can use this trait in any Model you want to throw events for. In your case in Article Model.

<?php namespace App;

use App\Traits\ModelEventThrower;
use Illuminate\Database\Eloquent\Model;

class Article extends Model {

    use ModelEventThrower;

    //Just in case you want specific events to be fired for Article model
    //uncomment following line of code

   // protected static $recordEvents = ['created'];

}

现在在您的app/Providers/EventServiceProvider.php中,在boot()方法中为Article注册事件处理程序.

Now in your app/Providers/EventServiceProvider.php, in boot() method register Event Handler for Article.

 public function boot(DispatcherContract $events)
 {
     parent::boot($events);
     $events->subscribe('App\Handlers\Events\ArticleEventHandler');
 }

现在在app/Handlers/Events目录下创建类ArticleEventHandler,如下所示,

Now create Class ArticleEventHandler under app/Handlers/Events directory as below,

<?php namespace App\Handlers\Events;

use App\Article;

class ArticleEventHandler{

    /**
     * Create the event handler.
     *
     * @return \App\Handlers\Events\ArticleEventHandler
     */
    public function __construct()
    {
        //
    }

    /**
    * Handle article.created event
    */

   public function created(Article $article)
   {
      //Implement logic
   }

   /**
   * Handle article.updated event
   */

   public function updated(Article $article)
   {
      //Implement logic
   }

  /**
  * Handle article.deleted event
  */

  public function deleted(Article $article)
  {
     //Implement logic
  }

 /**
 * @param $events
 */
 public function subscribe($events)
 {
     $events->listen('article.created',
            'App\Handlers\Events\ArticleEventHandler@created');
     $events->listen('article.updated',
            'App\Handlers\Events\ArticleEventHandler@updated');
     $events->listen('article.deleted',
            'App\Handlers\Events\ArticleEventHandler@deleted');
 }

}

从不同的答案,不同的用户可以看出,处理模型事件的方法不止一种.还有一些自定义事件,可以在事件"文件夹中创建这些自定义事件,并且可以在处理程序"文件夹中对其进行处理,并且可以从不同的位置分派这些事件.希望对您有帮助.

As you can see from different answers, from different Users, there are more than 1 way of handling Model Events. There are also Custom events That can be created in Events folder and can be handled in Handler folder and can be dispatched from different places. I hope it helps.

这篇关于Laravel模型活动-我对它们的去向有些困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆