我正在嘗試計算用戶在登錄期間訪問了多少頁。Symfony2 count訪問過的網站每個用戶
因此,我創建了一個Service
,它正在監聽Kernel::TERMINATE
事件,每當用戶轉到我係統內的其他站點時應增加計數器。
class ActivityListener implements EventSubscriberInterface {
private $doctrine;
private $session;
public function __construct(Registry $doctrine, Session $session){
$this->doctrine = $doctrine;
$this->session = $session;
}
public static function getSubscribedEvents(){
return array(
KernelEvents::TERMINATE => 'increasePageCount'
);
}
public function increasePageCount(){
$em = $this->doctrine->getManager();
$log = $em->getRepository('Bundle:UserLog')->findBy(array('sessionId' => $this->session->getId()));
if($log){
$log = $log[0];
$log->increasePageCounter(); //Function in the entity that increases the count
$em->persist($log);
$em->flush();
}
}
}
該代碼將被調用,這是好的,但每頁換10次,這顯然是很多方法!
我也嘗試了Kernel::REQUEST
事件,但結果相同。
我可以怎樣才能讓每個新頁面請求只增加一次PageCounter? (例如,用戶從賬單概覽開始到某個賬單的細節)