We will modify the logger to add a new method that keeps track of how many times a blog post has been viewed daily. The method will store this information in a new collection, whose schema is designed to fit this new use case. We will modify the blog.php
file to call that method after rendering the page. We will display this information in an HTML table on a different page.
1. Open the
log.php
file and add the following method in theLogger
class:public function updateVisitCounter($articleId) { $articleVistiCounterDaily = $this->_dbconnection ->getCollection('article_visit_counter_daily'); $criteria = array( 'article_id' => new MongoId($articleId), 'request_date' => new MongoDate(strtotime('today')) ); $newobj = array('$inc' => array('count' => 1)); $articleVistiCounterDaily->update($criteria, $newobj, array('upsert' => True)); }
2. Open the
blog.php
file and add the following code at the end of the file:...