层智翰·卡里士坎/视图计数器包

此包已被废弃且不再维护。未建议替代包。

Symfony 文档/实体视图计数器

v1.0.0 2017-03-28 20:26 UTC

This package is not auto-updated.

Last update: 2023-12-15 16:28:06 UTC


README

ViewsCounter 用于增加文档/实体的视图计数。

设置包

步骤 1: 安装 ViewsCounterBundle

使用 Composer 安装 ViewsCounterBundle。

composer require cengizhancaliskan/views-counter-bundle

在 AppKernel 中启用 ViewsCounterBundle

// app/AppKernel.php

public function registerBundles()
{
    $bundles = [
        // ...
        new Cengizhan\ViewsCounterBundle\CengizhanViewsCounterBundle(),
    ];

    // ...
}

步骤 2: 配置你的实体

<?php

namespace YourBundle\YourEntity;

use Cengizhan\ViewsCounterBundle\Model\VisitableInterface;
use Cengizhan\ViewsCounterBundle\Traits\VisitableEntityTrait;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class Article implements VisitableInterface
{
    use VisitableEntityTrait;

    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @ORM\Column(name="title", type="string")
     */
    protected $title;

    public function getId()
    {
        return $this->id;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function getTitle()
    {
        return $this->title;
    }

}

用法

<?php

....
$this->get('views_counter.views_counter')->count($article);
....

如何配置

如果你可以使用查询构建器(建议用于缓存实体)

# config.yml
....
cengizhan_views_counter:
    use_query_builder: true