laravel 使用事件系統(tǒng)統(tǒng)計瀏覽量的實現(xiàn)
最近有一個商城項目中有統(tǒng)計商品點擊量和藝術(shù)家訪問量的需求,但又不想改動太多原來的代碼,而點擊與訪問這兩個動作是有明確觸發(fā)點的,正好可以用laravel中的事件系統(tǒng)來做,在點擊和訪問對應(yīng)的函數(shù)中產(chǎn)生這倆事件,監(jiān)視器獲取到之后,再將記錄保存到數(shù)據(jù)庫中,并更新計數(shù)。
1、在 app\Providers\EventServiceProvider
中注冊監(jiān)聽器:
/** * 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)聽類
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)聽器:
<?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)計瀏覽量的實現(xiàn)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
PHP中__autoload和Smarty沖突的簡單解決方法
這篇文章主要介紹了PHP中__autoload和Smarty沖突的簡單解決方法,通過spl_autoload_register注冊__autoload函數(shù)的方法來解決__autoload函數(shù)再在Smarty無效的問題,需要的朋友可以參考下2016-04-04
ThinkPHP寫數(shù)組插入與獲取最新插入數(shù)據(jù)ID實例
這篇文章主要介紹了ThinkPHP寫數(shù)組插入與獲取最新插入數(shù)據(jù)ID的方法,實例講述了ThinkPHP基于數(shù)組操作數(shù)據(jù)庫的方法,具有一定的參考借鑒價值,需要的朋友可以參考下2014-11-11
Laravel統(tǒng)一封裝接口返回狀態(tài)實例講解
這篇文章主要介紹了Laravel統(tǒng)一封裝接口返回狀態(tài)實例講解,封裝接口返回狀態(tài)有利于前后端分離項目的合作開發(fā),有正好需要的同學(xué)可以研究下2021-03-03
php curl抓取網(wǎng)頁的介紹和推廣及使用CURL抓取淘寶頁面集成方法
抓取網(wǎng)頁內(nèi)容,分析網(wǎng)頁數(shù)據(jù)經(jīng)常使用php curl,簡潔易用,本篇文章通過代碼實例給大家講解 php curl抓取網(wǎng)頁的介紹和推廣及使用CURL抓取淘寶頁面集成方法,需要的朋友參考下2015-11-11
CentOS7.0下安裝PHP5.6.30服務(wù)的教程詳解
這篇文章主要介紹了CentOS7.0下安裝PHP5.6.30服務(wù)的教程,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09

