FOSCommentBundle功能包:与FOSUserBundle集成

Step 6: Integration with FOSUserBundle

By default, comments are made anonymously.FOSUserBundle authentication can be used to sign the comments.

缺省情况下,评论是匿名的。FOSUserBundle 认证可以用来标识评论。

A) Setup FOSUserBundle(安装FOSUserBundle)

First you have to setup FOSUserBundle. Check the instructions.

首先您需要安装 FOSUserBundle。请参阅说明

B) Extend the Comment class(扩展评论类)

In order to add an author to a comment, the Comment class should implement the SignedCommentInterface and add a field to your mapping.

为了为评论添加作者,Comment类需要实现SignedCommentInterface接口,并添加一个字段到您的映射。


For example in the ORM:

例如,在ORM中:

<?php
// src/MyProject/MyBundle/Entity/Comment.php
namespace MyProject\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\CommentBundle\Entity\Comment as BaseComment;
use FOS\CommentBundle\Model\SignedCommentInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
 * @ORM\Entity
 */
class Comment extends BaseComment implements SignedCommentInterface
{
    // .. fields
    /**
     * Author of the comment
     *
     * @ORM\ManyToOne(targetEntity="MyProject\MyBundle\Entity\User")
     * @var User
     */
    protected $author;
    public function setAuthor(UserInterface $author)
    {
        $this->author = $author;
    }
    public function getAuthor()
    {
        return $this->author;
    }
    public function getAuthorName()
    {
        if (null === $this->getAuthor()) {
            return 'Anonymous';
        }
        return $this->getAuthor()->getUsername();
    }
}

That is it!

Return to the index.

返回到指南索引页


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值