久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Laravel 模型事件 - 我對它們的去向有點困惑

Laravel Model Events - I#39;m a bit confused about where they#39;re meant to go(Laravel 模型事件 - 我對它們的去向有點困惑)
本文介紹了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:

  • 何時創建文章
  • 文章更新時
  • 當文章被刪除時

文檔說我可以使用模型事件并在 <AppProvidersEventServiceProvider的code>boot()函數.

The docs say I can use Model Events and register them within the boot() function of AppProvidersEventServiceProvider.

但這讓我很困惑,因為...

But this is confusing me because...

  • 當我添加其他模型(如 CommentAuthor)時,會發生什么,這些模型需要所有自己的模型事件的完整集合?EventServiceProvider 的單個 boot() 函數會非常龐大??嗎?
  • Laravel 的其他"事件的目的是什么?如果實際上我的事件只會響應模型 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 并簡單地用于所有需要記錄的模型類.我將根據您的需要更改它,如下所示.

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 AppTraits;

use IlluminateDatabaseEloquentModel;
use IlluminateSupportFacadesEvent;

/**
 * Class ModelEventThrower 
 * @package AppTraits
 *
 *  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',
        ];
    }
} 

現在您可以在要為其拋出事件的任何模型中使用此特征.在您的情況下 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 AppTraitsModelEventThrower;
use IlluminateDatabaseEloquentModel;

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('AppHandlersEventsArticleEventHandler');
 }

現在在app/Handlers/Events目錄下創建ArticleEventHandler類,

<?php namespace AppHandlersEvents;

use AppArticle;

class ArticleEventHandler{

    /**
     * Create the event handler.
     *
     * @return AppHandlersEventsArticleEventHandler
     */
    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',
            'AppHandlersEventsArticleEventHandler@created');
     $events->listen('article.updated',
            'AppHandlersEventsArticleEventHandler@updated');
     $events->listen('article.deleted',
            'AppHandlersEventsArticleEventHandler@deleted');
 }

}

正如您從不同的答案中看到的,來自不同的用戶,處理模型事件的方法不止一種.還有自定義事件,可以在Events文件夾中創建,可以在Handler文件夾中處理,可以從不同的地方分派.希望能幫到你.

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 模型事件 - 我對它們的去向有點困惑的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Laravel Eloquent Union query(Laravel Eloquent Union 查詢)
Overwrite laravel 5 helper function(覆蓋 Laravel 5 輔助函數)
laravel querybuilder how to use like in wherein function(laravel querybuilder 如何在 where 函數中使用 like)
The Response content must be a string or object implementing __toString(), quot;booleanquot; given after move to psql(響應內容必須是實現 __toString()、“boolean和“boolean的字符串或對象.移動到 psql 后給出) - IT屋-程
Roles with laravel 5, how to allow only admin access to some root(Laravel 5 的角色,如何只允許管理員訪問某些根)
Laravel Auth - use md5 instead of the integrated Hash::make()(Laravel Auth - 使用 md5 而不是集成的 Hash::make())
主站蜘蛛池模板: 色爱综合 | 久久精品美女 | 久久久久国产一区二区三区 | 成人精品国产一区二区4080 | 操人网站 | 日韩欧美网 | 午夜影院视频在线观看 | www.888www看片 | 91精品久久久久久久久 | 99re热这里只有精品视频 | 九九色综合 | 国产视频不卡一区 | 日韩在线视频免费观看 | 青青草社区 | 波多野结衣中文视频 | 老外几下就让我高潮了 | 一区二区在线免费观看视频 | 亚洲视频一区在线观看 | 亚洲激情在线观看 | 99久久精品国产一区二区三区 | 久久国产激情视频 | 欧美精品免费观看二区 | 成人三级视频在线观看 | 国产午夜精品一区二区三区嫩草 | 一色一黄视频 | 久久精品国产久精国产 | 精品一区av| 国产夜恋视频在线观看 | 午夜成人免费视频 | 亚洲va欧美va天堂v国产综合 | 日韩欧美三级电影 | 911网站大全在线观看 | 日日爱av | 中文字幕一区二区三区在线视频 | 国产在线观看av | 日本三级网址 | 日本公妇乱淫xxxⅹ 国产在线不卡 | 黄色片亚洲 | 欧美一级欧美三级在线观看 | 久久国产精品视频 | 日韩网站在线 |