婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > laravel 使用事件系統(tǒng)統(tǒng)計(jì)瀏覽量的實(shí)現(xiàn)

laravel 使用事件系統(tǒng)統(tǒng)計(jì)瀏覽量的實(shí)現(xiàn)

熱門標(biāo)簽:格陵蘭島地圖標(biāo)注 天津營(yíng)銷電話機(jī)器人加盟代理 南寧crm外呼系統(tǒng)平臺(tái) 事業(yè)單位如何百度地圖標(biāo)注 福泉電話機(jī)器人 太原極信防封電銷卡 熱線電話機(jī)器人 地圖標(biāo)注入哪個(gè)科目 電銷招聘機(jī)器人

最近有一個(gè)商城項(xiàng)目中有統(tǒng)計(jì)商品點(diǎn)擊量和藝術(shù)家訪問(wèn)量的需求,但又不想改動(dòng)太多原來(lái)的代碼,而點(diǎn)擊與訪問(wèn)這兩個(gè)動(dòng)作是有明確觸發(fā)點(diǎn)的,正好可以用laravel中的事件系統(tǒng)來(lái)做,在點(diǎn)擊和訪問(wèn)對(duì)應(yīng)的函數(shù)中產(chǎn)生這倆事件,監(jiān)視器獲取到之后,再將記錄保存到數(shù)據(jù)庫(kù)中,并更新計(jì)數(shù)。

1、在 app\Providers\EventServiceProvider

中注冊(cè)監(jiān)聽(tīng)器:

 /**
  * The event listener mappings for the application.
  *
  * @var array
  */
 protected $listen = [
  ......
  'App\Events\Statistics' => [
   'App\Listeners\BehavioralStatistics',
  ],
  ......
 ];

2、執(zhí)行

php artisan event:generate

生成事件類與監(jiān)聽(tīng)類

3、定義事件

?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class Statistics
{
 use Dispatchable, InteractsWithSockets, SerializesModels;

 public $user;
 public $obj;

 /**
  * Create a new event instance.
  *
  * @return void
  */
 public function __construct($user,$obj)
 {
  $this->user = $user;
  $this->obj = $obj;
 }

 /**
  * Get the channels the event should broadcast on.
  *
  * @return \Illuminate\Broadcasting\Channel|array
  */
 public function broadcastOn()
 {
  return new PrivateChannel('channel-name');
 }
}

4、定義監(jiān)聽(tīng)器:

?php

namespace App\Listeners;

use App\Events\Statistics;
use App\System\StaticsView;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;

class BehavioralStatistics
{
 /**
  * Create the event listener.
  *
  * @return void
  */
 public function __construct()
 {
  //
 }

 /**
  * Handle the event.
  *
  * @param Statistics $event
  * @return void
  */
 public function handle(Statistics $event)
 {
  $obj_class = get_class($event->obj);
  $statics_view = new StaticsView;

  switch($obj_class){
   case "App\\User":
    $statics_view->statics_type = 'user';

    break;
   case "App\\Production":
    $statics_view->statics_type = 'production';

    break;
  }

  $statics_view->ip = request()->getClientIp();;
  $statics_view->time_local = 0;
  $statics_view->statics_id = $event->obj->id;
  $statics_view->save();
 }
}

5、觸發(fā)事件:

event(new Statistics(user, user,user,production));

以上這篇laravel 使用事件系統(tǒng)統(tǒng)計(jì)瀏覽量的實(shí)現(xiàn)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 利用Laravel事件系統(tǒng)如何實(shí)現(xiàn)登錄日志的記錄詳解
  • laravel withCount 統(tǒng)計(jì)關(guān)聯(lián)數(shù)量的方法
  • Laravel統(tǒng)計(jì)一段時(shí)間間隔的數(shù)據(jù)方法

標(biāo)簽:阿克蘇 郴州 金華 通化 香港 佳木斯 寶雞 自貢

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《laravel 使用事件系統(tǒng)統(tǒng)計(jì)瀏覽量的實(shí)現(xiàn)》,本文關(guān)鍵詞  laravel,使用,事件,系統(tǒng),統(tǒng)計(jì),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《laravel 使用事件系統(tǒng)統(tǒng)計(jì)瀏覽量的實(shí)現(xiàn)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于laravel 使用事件系統(tǒng)統(tǒng)計(jì)瀏覽量的實(shí)現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 汶上县| 崇阳县| 四会市| 普兰店市| 茂名市| 甘洛县| 景宁| 陇西县| 大足县| 道真| 余干县| 平陆县| 黄骅市| 廊坊市| 都匀市| 榆树市| 祁东县| 桃源县| 沙洋县| 三原县| 莫力| 正定县| 建阳市| 南投县| 阿图什市| 秦安县| 封开县| 马龙县| 延吉市| 阳西县| 柏乡县| 呼图壁县| 于都县| 象山县| 福泉市| 合肥市| 曲水县| 上林县| 呼伦贝尔市| 宣汉县| 云霄县|