このバンドルを使用します https://github.com/FriendsOfSymfony/FOSCommentBundle/blob/master/Resources/doc/index.md
ただし、非同期ではなく、最初のコメントをhtmlで表示する必要があります。そのほぼ完了私はコントローラーを作成して ThreadController を拡張します
限定投稿を取得する機能を追加
public function getThreadCommentsWithLimitAction(Request $request, $id, $limit=false)
{
$displayDepth = $request->query->get('displayDepth');
$sorter = $request->query->get('sorter');
$thread = $this->container->get('fos_comment.manager.thread')->findThreadById($id);
// We're now sure it is no duplicate id, so create the thread
if (null === $thread) {
// Decode the permalink for cleaner storage (it is encoded on the client side)
$permalink = urldecode($request->query->get('permalink'));
$thread = $this->container->get('fos_comment.manager.thread')
->createThread();
$thread->setId($id);
$thread->setPermalink($permalink);
// Validate the entity
$validator = $this->get('validator');
$errors = $validator->validate($thread, 'NewThread');
if (count($errors) > 0) {
$view = View::create()
->setStatusCode(Codes::HTTP_BAD_REQUEST)
->setData(array('errors' => $errors))
->setTemplate(new TemplateReference('FOSCommentBundle', 'Thread', 'errors'));
return $this->getViewHandler()->handle($view);
}
// Add the thread
$this->container->get('fos_comment.manager.thread')->saveThread($thread);
}
$viewMode = $request->query->get('view', 'tree');
switch ($viewMode) {
case self::VIEW_FLAT:
$comments = $this->container->get('fos_comment.manager.comment')->findCommentsByThread($thread, $displayDepth, $sorter);
// We need nodes for the api to return a consistent response, not an array of comments
$comments = array_map(function($comment) {
return array('comment' => $comment, 'children' => array());
},
$comments
);
break;
case self::VIEW_TREE:
default:
$comments = $this->container->get('fos_comment.manager.comment')->findCommentTreeByThread($thread, $sorter, $displayDepth);
break;
}
$view = View::create()
->setData(array(
'comments' => $comments,
'displayDepth' => $displayDepth,
'limit' => $limit,
'sorter' => 'date',
'thread' => $thread,
'view' => $viewMode,
))
->setTemplate(new TemplateReference('FOSCommentBundle', 'Thread', 'comments'));
// Register a special handler for RSS. Only available on this route.
if ('rss' === $request->getRequestFormat()) {
$templatingHandler = function($handler, $view, $request) {
$view->setTemplate(new TemplateReference('FOSCommentBundle', 'Thread', 'thread_xml_feed'));
return new Response($handler->renderTemplate($view, 'rss'), Codes::HTTP_OK, $view->getHeaders());
};
$this->get('fos_rest.view_handler')->registerHandler('rss', $templatingHandler);
}
return $this->getViewHandler()->handle($view);
}
テンプレートの comments.html.twig をオーバーライドします
{% set depth = depth|default(0) %}
{% set view = view|default('tree') %}
{% if depth == 0 %}
{% if fos_comment_can_comment_thread(thread) %}
{% render url("fos_comment_new_thread_comments", {"id": thread.id}) %}
{% endif %}
{% if fos_comment_can_edit_thread(thread) %}
<div class="fos_comment_thread_commentable">
<button data-url="{{ url('fos_comment_edit_thread_commentable', {'id': thread.id, 'value': not thread.commentable}) }}" class="fos_comment_thread_commentable_action">
{{ (thread.commentable ? 'fos_comment_thread_close' : 'fos_comment_thread_open') | trans({}, 'FOSCommentBundle') }}
</button>
</div>
{% endif %}
{% set count = thread.numComments %}
<h3>{% transchoice count with {'%count%': count} from "FOSCommentBundle" %}fos_comment_thread_comment_count{% endtranschoice %}</h3>
{% endif %}
{% if limit is defined and limit>0%}
{% for commentinfo in comments|slice(0, limit) %}
{% include "FOSCommentBundle:Thread:comment.html.twig" with { "children": commentinfo.children, "comment": commentinfo.comment, "depth": depth, "view": view } %}
{% endfor %}
{% if comments|length > limit %}
<p><a href="{{ path('place_one_commentsAll',{
threadId: comments.0.comment.thread.id
}) }}">zobacz wszystkie wypowiedzi ({{ comments|length }})</a></p>
{% endif %}
{% else %}
{% for commentinfo in comments %}
{% include "FOSCommentBundle:Thread:comment.html.twig" with { "children": commentinfo.children, "comment": commentinfo.comment, "depth": depth, "view": view } %}
{% endfor %}
{% endif %}
私は今のところ小枝の制限がまだ終わっていないことを知っていますが、jsアクションに尋ねる必要があります
コメントはコアレットに表示されますが、アクション (返信、投票の追加) は機能しません。
fos https://github.com/FriendsOfSymfony/FOSCommentBundle/blob/master/Resources/assets/js/comments.jsから手動で comments.js を含めます。
と手動でセットアップ
<script type="text/javascript">
var fos_comment_thread_id = 'place{{ place.id }}';
var fos_comment_thread_api_base_url = '{{ path('fos_comment_get_threads') }}';
</script>
jsアクションを初期化するにはどうすればよいですか?
私は試しますFOS_COMMENT.initializeListeners();
VM229:2 Uncaught ReferenceError: FOS_COMMENT is not defined(…)
js が読み込まれます。
このように初期化すると
<script type="text/javascript">
var fos_comment_thread_id = 'place{{ place.id }}';
var fos_comment_thread_api_base_url = '{{ path('fos_comment_get_threads') }}';
</script>
<script src="{{ asset('bundles/app/js/comments.js') }}"></script>
このエラーが発生します
Uncaught TypeError: FOS_COMMENT.thread_container.trigger is not a function