当前位置:首页 > CMS教程 > 其它CMS > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2022-01-06 10:36:08 浏览: 评论:0 

今天小编就为大家分享一篇laravel 使用事件系统统计浏览量的实现,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧。

最近有一个商城项目中有统计商品点击量和艺术家访问量的需求,但又不想改动太多原来的代码,而点击与访问这两个动作是有明确触发点的,正好可以用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、执行

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、触发事件:

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

Tags: laravel事件系统 laravel浏览量

分享到: