gulaandrij/views-counter-bundle

文档/实体视图计数器(Symfony)

安装: 115

依赖项: 0

建议者: 0

安全: 0

星标: 2

关注者: 2

分支: 0

公开问题: 2

类型:symfony-bundle

1.0.2 2019-12-31 08:18 UTC

README

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

设置 bundle

步骤 1:安装 ViewsCounterBundle

使用 Composer 安装 ViewsCounterBundle。

composer require gulaandrij/views-counter-bundle

在您的 AppKernel 中启用 ViewsCounterBundle

// app/AppKernel.php

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

    // ...
}

步骤 2:配置您的实体

<?php

namespace YourBundle\YourEntity;

use Lavulator\ViewsCounterBundle\Model\VisitableInterface;
use Lavulator\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
....
lavulator_views_counter:
    use_query_builder: true