format_textdirection_l_to_r Introducing

The MkCommentBundle allows you to create comment form, add and manage comments on your content.

Features :
  • Easy to configure and used
  • You can add comment, and you can reply to them.You can adjust like you want the depth of responses.
  • The bundle can protect your comments to spam. It use Akismet library.
  • Possibility to post a comment normally or by Ajax system

format_textdirection_l_to_r Configuration

Install :

Download MkCommentBundle with the command line :

    $ php composer.phar require mykees/symfony2-comment-bundle "1.0.*@dev"

Enable the bundle :

<?php
public function registerBundle
{
    $bundles = array(
        // ...
        new Mykees\CommentBundle\MykeesCommentBundle(),
    );
}

Add route in your app/config/routing.yml :

mykees_comment:
    resource: "@MykeesCommentBundle/Resources/config/routing.yml"
    prefix:   /comment

mykees_admin_comment:
    resource: "@MykeesCommentBundle/Resources/config/routing_admin.yml"
    prefix:   /admin/comment
Create your Comment classe :

Create your own Comment class, and this class must extend by the Mykees\CommentBundle\Entity\Comment class that will provide variables getters, setters and mapping required.

<?php
// src/YourProject/YourBundle/Entity/Comment.php

namespace YourProject\YourBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Mykees\CommentBundle\Entity\Comment as CommentParent;

/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Comment extends CommentParent
{
    /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;


    /**
    * Get id
    *
    * @return integer
    */
    public function getId()
    {
        return $this->id;
    }
}

Enable the configuration in your app/config/config.yml :

mykees_comment:
    comment_class: YourProject\YourBundle\Entity\Comment

That's it for minimal configuration :)

format_textdirection_l_to_r Usage

To retrieve the comment form for your content, you need to have an object with an id. By example if you have a controller that retrieves an article and an entity Article, you can do like that :

Make you entity Commentable :

Implement the IsCommentable Interface to your entity:

<?php
namespace YourProject\YourBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Mykees\CommentBundle\Interfaces\IsCommentable;

/**
* Article
*
* @ORM\Table()
* @ORM\Entity()
*/
class Article implements IsCommentable
{
    //....
}
Create the comment form

To create a comment form in your controller, you need to use the following service :

$form = $this->get('mykees.comment.manager')->createForm($article);

If you want to add a label to your fields, you can add an array as optional second parameter that will contain these labels :

$form = $this->get('mykees.comment.manager')->createForm($article,[
    'username'=>'Nom:',
    'email'=>'Email:'
]);
Get the comments list :

To get a the comments list linked to an entity in your controller, you need to use the following service :

$comments = $this->get('mykees.comment.query.manager')->findComments($article);

format_textdirection_l_to_r View and Style

Form helper :

Use the form helper to get the form in your view

{{ helper_comment_form(form) }}

Add base css in your layout for stylize the main form element

{% block stylesheets %}
    <link rel="stylesheet" href="{{ asset('bundles/mykeescomment/css/comment.css') }}">
{% endblock %}

If you activate the responses to comments by adjusting the depth, you need to add the following js file to your template for stylize some form elements

{% block javascripts %}
<script type="text/javascript" src="{{ asset('bundles/mykeescomment/js/comment.js') }}"></script>
{% endblock %}
Comments list helper :

Use the comment helper to get the comments list in your view

{{ helper_comments_list(comments) }}
Number of comments :
{{ comments.count }}

format_textdirection_l_to_r Manage Comments

Editing a comment :

To edit comments, you must to create your own editing process.

1) Create your routing : /!\ (Don't change "comment_id" name parameter) /!\

my_own_admin_comment_edit:
    path: /comment/edit/{comment_id}
    defaults: { _controller: YourOwnBundle:YourOwnController:youOwnEditFunction }
    requirements:
        comment_id: \d+

2) Create your function edit proccess in your controller.

3) Add to Comments list helper "true" as second parameter and on third parameter an array that will contain the name of your edit route:

{{ helper_comments_list(comments,true,{edit_routing:'my_own_admin_comment_edit'}) }}

format_textdirection_l_to_r Ajax

If you prefer to post comments by Ajax, you must to add true as second parameter of the helper comment form:

{{ helper_comment_form(form,true) }}

That's it :)

format_textdirection_l_to_r Options

Use with FOSUserBundle :

If you use the FOSUserBundle in your project and want to use it with MkCommentBundle, your Comment classe must implement HasUserInterface Interface, and add $user variable with Getter and Setter

<?php

namespace YourProject\YourBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\UserInterface;
use Mykees\CommentBundle\Entity\Comment as CommentParent;
use Mykees\CommentBundle\Interfaces\HasUserInterface;

/**
* Comment
*
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Comment extends CommentParent implements HasUserInterface
{

    //...

    /**
    *
    * @ORM\ManyToOne(targetEntity="YourProject\YourBundle\Entity\User")
    * @var User
    */
    private $user;

    //...

    public function getUser()
    {
        return $this->user;
    }

    public function setUser(UserInterface $user)
    {
        $this->user = $user;
        return $this;
    }

    //...
}
Akismet :

The MkCommentBundle can work with Akismet. To use it, it's very simple.

  • Create an account.
  • Add to app/config/config.yml the following lines :
mykees_comment:
    akismet:
        api_key: YOUR API KEY
        website: YOUR WEBSITE
Activate responses :

You can activate the reponses by adjusting the depth in your app/config/config.yml.

mykees_comment:
    //...
    depth: 1

Note*

If you activate the responses, don't forget to add js file in your view:

{% block javascripts %}
<script type="text/javascript" src="{{ asset('bundles/mykeescomment/js/comment.js') }}"></script>
{% endblock %}

format_textdirection_l_to_r Useful functions

- Find one comment :

$model_name and $model_id are optional

$this->get('mykees.comment.query.manager')->findOneComment($id, $model_name=null,$model_id=null)
- Find one comment by model referer :

$model parameter must an instance of IsCommentable interface.

$this->get('mykees.comment.query.manager')->findOneCommentByReferer($id, $model)
- Find all comments for an entity :

The $get_by_id parameter allows to have the id of comment as index in array. it's an optionnal parameter

$this->get('mykees.comment.query.manager')->findComments(IsCommentable $model, $get_by_id=false)
- Find all comments for an entity by model name and model id :

The $get_by_id parameter allows to have the id of comment as index in array. it's an optionnal parameter

$this->get('mykees.comment.query.manager')->findCommentsByModelAndId($model_name, $model_id, $get_by_id=false)
- Retrieve all comments :
$this->get('mykees.comment.query.manager')->findAllComments(array $criteria=[],$orderBy=null,$limit=null,$offset=null)
- Delete comments by ids :
$this->get('mykees.comment.query.manager')->deleteByCommentIds($ids)
- Delete a comment by id :
$this->get('mykees.comment.query.manager')->deleteComment($id)