1. Go to this page and download the library: Download krowek/view-counter-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
krowek / view-counter-bundle example snippets
...
$bundles = array(
...
new Tchoulom\ViewCounterBundle\TchoulomViewCounterBundle(),
...
);
...
use Tchoulom\ViewCounterBundle\Model\ViewCountable;
...
class Article implements ViewCountable
{
...
}
use Tchoulom\ViewCounterBundle\Model\ViewCountable;
use Entity\ViewCounter;
use Doctrine\Common\Collections\ArrayCollection;
...
class Article implements ViewCountable
{
...
/**
* @var integer
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Entity\ViewCounter", mappedBy="article")
*/
protected $viewCounters;
/**
* @ORM\Column(name="views", type="integer", nullable=true)
*/
protected $views = 0;
/**
* Constructor
*/
public function __construct()
{
$this->viewCounters = new ArrayCollection();
}
/**
* Gets id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Sets $views
*
* @param integer $views
*
* @return $this
*/
public function setViews($views)
{
$this->views = $views;
return $this;
}
/**
* Gets $views
*
* @return integer
*/
public function getViews()
{
return $this->views;
}
/**
* Get $viewCounters
*
* @return Collection
*/
public function getViewCounters()
{
return $this->viewCounters;
}
/**
* Add $viewCounter
*
* @param ViewCounter $viewCounter
*
* @return $this
*/
public function addViewCounter(ViewCounter $viewCounter)
{
$this->viewCounters[] = $viewCounter;
return $this;
}
/**
* Remove $viewCounter
*
* @param ViewCounter $viewCounter
*/
public function removeViewCounter(ViewCounter $viewCounter)
{
$this->viewCounters->removeElement($viewCounter);
}
...
}
use Tchoulom\ViewCounterBundle\Entity\ViewCounter as BaseViewCounter;
/**
* ViewCounter.
*
* @ORM\Table(name="view_counter")
* @ORM\Entity()
*/
class ViewCounter extends BaseViewCounter
{
...
}
use App\Entity\ViewCounter;
use Tchoulom\ViewCounterBundle\Counter\ViewCounter as Counter;
...
// For Symfony 4 or 5, inject the ViewCounter service
/**
* @var Counter
*/
protected $viewcounter;
/**
* @param Counter $viewCounter
*/
public function __construct(Counter $viewCounter)
{
$this->viewcounter = $viewCounter;
}
/**
* Reads an existing article
*
* @Route("/read/{id}", name="read_article")
* @ParamConverter("article", options={"mapping": {"id": "id"}})
* @Method({"GET", "POST"})
*/
public function readAction(Request $request, Article $article)
{
// Viewcounter
$viewcounter = $this->get('tchoulom.viewcounter')->getViewCounter($article);
// For Symfony 4 or 5
$viewcounter = $this->viewcounter->getViewCounter($article);
$em = $this->getDoctrine()->getEntityManager();
if ($this->viewcounter->isNewView($viewcounter)) {
$views = $this->viewcounter->getViews($article);
$viewcounter->setIp($request->getClientIp());
$viewcounter->setArticle($article);
$viewcounter->setViewDate(new \DateTime('now'));
$article->setViews($views);
$em->persist($viewcounter);
$em->persist($article);
$em->flush();
...
}
}
...
...
use Tchoulom\ViewCounterBundle\Counter\ViewCounter as Counter;
// For Symfony 4 or 5, inject the ViewCounter service
/**
* @var Counter
*/
protected $viewcounter;
/**
* @param Counter $viewCounter
*/
public function __construct(Counter $viewCounter)
{
$this->viewcounter = $viewCounter;
}
/**
* Reads an existing article
*
* @Route("/read/{id}", name="read_article")
* @ParamConverter("article", options={"mapping": {"id": "id"}})
* @Method({"GET", "POST"})
*/
public function readAction(Request $request, Article $article)
{
// Saves the view
$page = $this->get('tchoulom.viewcounter')->saveView($article);
// For Symfony 4 or 5
$page = $this->viewcounter->saveView($article);
...
}
namespace App\Service;
use GeoIp2\Database\Reader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Tchoulom\ViewCounterBundle\Adapter\Geolocator\GeolocatorInterface;
/**
* Class Geolocator
*
* This service must implements the "GeolocationInterface".
*/
class Geolocator implements GeolocatorInterface
{
/**
* @var Request
*/
protected $request;
/**
* @var Reader
*/
protected $reader;
/**
* Geolocator constructor.
*
* @param RequestStack $requestStack
* @param Reader $reader
*/
public function __construct(RequestStack $requestStack, Reader $reader)
{
$this->request = $requestStack->getCurrentRequest();
$this->reader = $reader;
}
/**
* Gets the record.
*
* @return \GeoIp2\Model\City|mixed
* @throws \GeoIp2\Exception\AddressNotFoundException
* @throws \MaxMind\Db\Reader\InvalidDatabaseException
*/
public function getRecord()
{
$clientIp = $this->request->getClientIp();
return $this->reader->city($clientIp);
}
/**
* Gets the continent.
*
* @return string
* @throws \GeoIp2\Exception\AddressNotFoundException
* @throws \MaxMind\Db\Reader\InvalidDatabaseException
*/
public function getContinent(): string
{
return $this->getRecord()->continent->name;
}
/**
* Gets the country.
*
* @return string
* @throws \GeoIp2\Exception\AddressNotFoundException
* @throws \MaxMind\Db\Reader\InvalidDatabaseException
*/
public function getCountry(): string
{
return $this->getRecord()->country->name;
}
/**
* Gets the region.
*
* @return string
* @throws \GeoIp2\Exception\AddressNotFoundException
* @throws \MaxMind\Db\Reader\InvalidDatabaseException
*/
public function getRegion(): string
{
return $this->getRecord()->subdivisions[0]->names['en'];
}
/**
* Gets the city.
*
* @return string
* @throws \GeoIp2\Exception\AddressNotFoundException
* @throws \MaxMind\Db\Reader\InvalidDatabaseException
*/
public function getCity(): string
{
return $this->getRecord()->city->name;
}
}
use Tchoulom\ViewCounterBundle\Finder\StatsFinder:
/**
* @var StatsFinder
*/
protected $statsFinder;
/**
* @param StatsFinder $statsFinder
*/
public function __construct(StatsFinder $statsFinder)
{
$this->statsFinder = $statsFinder;
}
// The "statsFinder" service
$statsFinder = $this->get('tchoulom.viewcounter.stats_finder');
// Get all statistical data
$contents = $statsFinder->loadContents();
// Finds statistics by page
// Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Page
$page = $statsFinder->findByPage($article);
// Finds statistics by year (year number: 2019)
// Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Year
$year = $statsFinder->findByYear($article, 2019);
// Finds statistics by month (month number: 1)
// Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Month
$month = $statsFinder->findByMonth($article, 2019, 1);
// Finds statistics by week (week number: 3)
// Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Week
$week = $statsFinder->findByWeek($article, 2019, 1, 3);
// Finds statistics by day (name of the day: 'thursday')
// Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Day
$day = $statsFinder->findByDay($article, 2019, 1, 3, 'thursday');
// Finds statistics by hour (time name: 'h17' => between 17:00 and 17:59)
// Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Hour
$hour = $statsFinder->findByHour($article, 2019, 1, 3, 'thursday', 'h17');
// Finds statistics by minute (the name of the minute: 'm49' => in the 49th minute)
// Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Minute
$minute = $statsFinder->findByMinute($article, 2019, 1, 3, 'thursday', 'h17', 'm49');
// Finds statistics by second (the name of the second: 's19' => in the 19th second)
// Returns an instance of Tchoulom\ViewCounterBundle\Statistics\Second
$second = $statsFinder->findBySecond($article, 2019, 1, 3, 'thursday', 'h17', 'm49', 's19');
// Get the yearly statistics
$yearlyStats = $statsFinder->getYearlyStats($article);
// Get the hourly statistics for Thursday of the week number 33 in august 2019
$hourlyStats = $statsFinder->getHourlyStats($article, 2019, 8, 33, 'Thursday');
// Get the statistics per minute on Saturday of the week number 33 in august 2019 at 15h ('h15')
$statsPerMinute = $this->get('tchoulom.viewcounter.stats_finder')->getStatsPerMinute($article, 2019, 8, 33, 'Saturday', 'h15');
// Get the statistics per second on Saturday of the week number 33 in august 2019 at 15H49
$statsPerSecond = $this->get('tchoulom.viewcounter.stats_finder')->getStatsPerSecond($article, 2019, 8, 33, 'Saturday', 'h15', 'm49');