discutea/rating-bundle

源自 damianociarla/rating-bundle ! 此Bundle为Symfony4应用程序提供评分系统功能

v1.2 2015-02-19 09:10 UTC

This package is auto-updated.

Last update: 2024-09-24 23:36:30 UTC


README

DiscuteaRatingBundle增加了对Symfony2中评分系统的支持。特性包括

  • 您可以使用一行代码为任何页面添加投票。
  • 您可以将其与任何用户管理系统(例如FOSUserBundle)集成
  • 您可以设置不同的角色以访问投票
  • 该bundle使用一个样式表文件,没有javascript文件

1) 安装

A) 下载并安装DiscuteaRatingBundle

要安装DiscuteaRatingBundle,运行以下命令

bash $ php composer.phar require damianociarla/rating-bundle

B) 启用bundle

在kernel中启用所需的bundles

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
    	// ...
    	new Discutea\RatingBundle\DiscuteaRatingBundle(),
	);
}

2) 创建您的投票和评分类

在此第一个版本中,DiscuteaRatingBundle只支持Doctrine ORM。但是,您必须提供具体的投票和评分类。您必须扩展bundle提供的抽象实体,并创建适当的映射。

评分

<?php
// src/MyProject/MyBundle/Entity/Rating.php

namespace MyProject\MyBundle\Entity;

use Discutea\RatingBundle\Entity\Rating as BaseRating;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
 */
class Rating extends BaseRating
{
    /**
     * @ORM\Id
     * @ORM\Column(type="string")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="MyProject\MyBundle\Entity\Vote", mappedBy="rating")
     */
    protected $votes;
}

投票

<?php
// src/MyProject/MyBundle/Entity/Vote.php

namespace MyProject\MyBundle\Entity;

use Discutea\RatingBundle\Entity\Vote as BaseVote;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
 */
class Vote extends BaseVote
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="MyProject\MyBundle\Entity\Rating", inversedBy="votes")
     * @ORM\JoinColumn(name="rating_id", referencedColumnName="id")
     */
    protected $rating;

    /**
     * @ORM\ManyToOne(targetEntity="MyProject\UserBundle\Entity\User")
     */
    protected $voter;
}

3) 配置您的应用程序

# app/config/config.yml

discutea_rating:
    db_driver: orm
    base_path_to_redirect: / # when the permalink is not configured
    max_value: 5 # maximum value for the vote (stars displayed)
    model:
        rating: MyProject\MyBundle\Entity\Rating
        vote: MyProject\MyBundle\Entity\Vote

4) 导入DiscuteaRatingBundle路由

导入bundle路由

discutea_rating:
    resource: "@DiscuteaRatingBundle/Resources/config/routing.xml"
	prefix:   /

5) 在模板中导入样式表

要导入样式表,请运行以下命令

bash $ php app/console assets:install

并在模板中包含样式表

<link rel="stylesheet" href="{{ asset('bundles/discutearating/css/rating.css') }}" />

5.1) 通过ajax启用投票

要通过ajax投票,您必须在加载jQuery库后包含以下脚本

<script src="{{ asset('bundles/discutearating/js/rating.js') }}"></script>

6) 显示评分并启用投票

您只能对您所在的页面进行投票。您不能在浏览不同页面时对页面进行投票。但是,您可以在任何页面上显示页面的评分(只读模式)

显示评分

您可以使用星级显示评分,而不需要启用投票

{% include 'DiscuteaRatingBundle:Rating:rating.html.twig' with {'id' : 'YOUR_UNIQUE_ID'} %}

如果您有一个项目列表并希望显示每个项目的评分,这将很有用。

启用投票

要在页面上启用投票,请使用以下twig代码

{% include 'DiscuteaRatingBundle:Rating:control.html.twig' with {'id' : 'YOUR_UNIQUE_ID'} %}

如果您需要更改特定页面的默认用户角色,请添加role参数

{% include 'DiscuteaRatingBundle:Rating:control.html.twig' with {'id' : 'YOUR_UNIQUE_ID', 'role' : 'ROLE_USER'} %}

如果您需要更改永久链接,请添加permalink参数,否则它将存储当前路由

{% include 'DiscuteaRatingBundle:Rating:control.html.twig' with {'id' : 'YOUR_UNIQUE_ID', 'permalink' : url('YOUR_ROUTE_ID')} %}