经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
laravel 使用事件系统统计浏览量的实现
来源:jb51  时间:2019/10/17 9:04:41  对本文有异议

最近有一个商城项目中有统计商品点击量和艺术家访问量的需求,但又不想改动太多原来的代码,而点击与访问这两个动作是有明确触发点的,正好可以用laravel中的事件系统来做,在点击和访问对应的函数中产生这俩事件,监视器获取到之后,再将记录保存到数据库中,并更新计数。

1、在 app\Providers\EventServiceProvider

中注册监听器:

  1. /**
  2. * The event listener mappings for the application.
  3. *
  4. * @var array
  5. */
  6. protected $listen = [
  7. ......
  8. 'App\Events\Statistics' => [
  9. 'App\Listeners\BehavioralStatistics',
  10. ],
  11. ......
  12. ];

2、执行

  1. php artisan event:generate

生成事件类与监听类

3、定义事件

  1. <?php
  2.  
  3. namespace App\Events;
  4.  
  5. use Illuminate\Broadcasting\Channel;
  6. use Illuminate\Queue\SerializesModels;
  7. use Illuminate\Broadcasting\PrivateChannel;
  8. use Illuminate\Broadcasting\PresenceChannel;
  9. use Illuminate\Foundation\Events\Dispatchable;
  10. use Illuminate\Broadcasting\InteractsWithSockets;
  11. use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
  12.  
  13. class Statistics
  14. {
  15. use Dispatchable, InteractsWithSockets, SerializesModels;
  16.  
  17. public $user;
  18. public $obj;
  19.  
  20. /**
  21. * Create a new event instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct($user,$obj)
  26. {
  27. $this->user = $user;
  28. $this->obj = $obj;
  29. }
  30.  
  31. /**
  32. * Get the channels the event should broadcast on.
  33. *
  34. * @return \Illuminate\Broadcasting\Channel|array
  35. */
  36. public function broadcastOn()
  37. {
  38. return new PrivateChannel('channel-name');
  39. }
  40. }

4、定义监听器:

  1. <?php
  2.  
  3. namespace App\Listeners;
  4.  
  5. use App\Events\Statistics;
  6. use App\System\StaticsView;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Support\Facades\Log;
  10.  
  11. class BehavioralStatistics
  12. {
  13. /**
  14. * Create the event listener.
  15. *
  16. * @return void
  17. */
  18. public function __construct()
  19. {
  20. //
  21. }
  22.  
  23. /**
  24. * Handle the event.
  25. *
  26. * @param Statistics $event
  27. * @return void
  28. */
  29. public function handle(Statistics $event)
  30. {
  31. $obj_class = get_class($event->obj);
  32. $statics_view = new StaticsView;
  33.  
  34. switch($obj_class){
  35. case "App\\User":
  36. $statics_view->statics_type = 'user';
  37.  
  38. break;
  39. case "App\\Production":
  40. $statics_view->statics_type = 'production';
  41.  
  42. break;
  43. }
  44.  
  45. $statics_view->ip = request()->getClientIp();;
  46. $statics_view->time_local = 0;
  47. $statics_view->statics_id = $event->obj->id;
  48. $statics_view->save();
  49. }
  50. }

5、触发事件:

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

以上这篇laravel 使用事件系统统计浏览量的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号