溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在laravel中使用事件系統統計瀏覽量

發布時間:2021-02-17 12:54:54 來源:億速云 閱讀:405 作者:Leah 欄目:開發技術

這篇文章給大家介紹如何在laravel中使用事件系統統計瀏覽量,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

1、在 app\Providers\EventServiceProvider

中注冊監聽器:

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

2、執行

php artisan event:generate

生成事件類與監聽類

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、定義監聽器:

<?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、觸發事件:

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

關于如何在laravel中使用事件系統統計瀏覽量就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女