• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    laravel 使用事件系统统计浏览量的实现

    最近有一个商城项目中有统计商品点击量和艺术家访问量的需求,但又不想改动太多原来的代码,而点击与访问这两个动作是有明确触发点的,正好可以用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 使用事件系统统计浏览量的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

    您可能感兴趣的文章:
    • 利用Laravel事件系统如何实现登录日志的记录详解
    • laravel withCount 统计关联数量的方法
    • Laravel统计一段时间间隔的数据方法
    上一篇:关于laravel 子查询 & join的使用
    下一篇:解决Laravel无法使用COOKIE和SESSION的问题
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    laravel 使用事件系统统计浏览量的实现 laravel,使用,事件,系统,统计,