The MkCommentBundle allows you to create comment form, add and manage comments on your content.
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 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 :)
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 :
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
{
//....
}
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:'
]);
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);
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 %}
Use the comment helper to get the comments list in your view
{{ helper_comments_list(comments) }}
{{ comments.count }}
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'}) }}
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 :)
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;
}
//...
}
The MkCommentBundle can work with Akismet. To use it, it's very simple.
mykees_comment:
akismet:
api_key: YOUR API KEY
website: YOUR WEBSITE
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 %}
$model_name and $model_id are optional
$this->get('mykees.comment.query.manager')->findOneComment($id, $model_name=null,$model_id=null)
$model parameter must an instance of IsCommentable interface.
$this->get('mykees.comment.query.manager')->findOneCommentByReferer($id, $model)
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)
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)
$this->get('mykees.comment.query.manager')->findAllComments(array $criteria=[],$orderBy=null,$limit=null,$offset=null)
$this->get('mykees.comment.query.manager')->deleteByCommentIds($ids)
$this->get('mykees.comment.query.manager')->deleteComment($id)