1
私は3つのエンティティを持っています:ユーザー(FOSUserBundleの一部)、コメント、記事。Symfony/FOSUserBundle:訪問者がログインしている場合にのみ記事にコメントフォームを表示するにはどうすればよいですか?
私は、訪問者の記事のみにコメントフォームを表示したいと思います。誰もがうまく表示されていました。
私のコードは機能しませんし、非常に悪い習慣のようです。私は "変数"フォームが存在しない "を取得します。私はフォームがビューに渡されるのを止めているからです。もし私がビューにifを入れたら必要ですか?私はこれをどのように達成することができるかについての提案を感謝します!
ありがとうございます。
マイcomment.html.twig
{% extends 'base.html.twig' %}
{% block body %}
{% form_theme form 'form/fields.html.twig' %}
{% if is_granted('ROLE_USER') -%}
<div class="form-group">{{ form_start(form) }}</div>
<div class="form-group">{{ form_row(form.comment.title) }}</div>
<div class="form-group">{{ form_row(form.comment.rating) }}</div>
<div class="form-group">{{ form_row(form.comment.comment) }}</div>
<div class="form-group">{{ form_row(form.comment.user.firstName) }}</div>
<div class="form-group">{{ form_row(form.comment.user.lastName) }}</div>
<div class="form-group">{{ form_row(form.comment.user.email) }}</div>
<input class="btn btn-default" type="submit" value="Create" />
{{ form_end(form) }}
{% else %}
<p>Please log in to post a comment!</p>
{% endif %}
{% endblock %}
(記事のために)私のshow.html.twig
{% extends 'base.html.twig' %}
{% block body_id 'articlepage' %}
{% block body %}
<h1>Article: {{ article.name }}</h1>
<div class="well"><div class="media">
<div class="media-left media-top">
<img class="media-object" src="{{ article.thumbnail }}">
</div>
<div class="media-body">
<h4 class="media-heading">{{ article.name }}</h4>
<p>{{ article.description }}</p>
</div>
</div></div>
<h2>Article Comments:</h2>
{{ render(controller('AppBundle:Comment:index', {'article_id': article.id})) }}
<h2>Submit a new comment:</h2>
{{ render(controller('AppBundle:Article:comment', {'id': article.id})) }}
{% endblock %}
ArticleController.php内commentAction(ショーに埋め込まれますコントローラ。 html.twig)
public function commentAction(Request $request, Article $article)
{
$auth_checker = $this->get('security.userization_checker');
$token = $this->get('security.token_storage')->getToken();
$user = $token->getUser();
$isRoleUser = $auth_checker->isGranted('ROLE_USER');
if($isRoleUser){
$comment = new Comment();
//Build the comment form.
$commentForm = $this->createFormBuilder($comment)
->add('comment', CommentType::class, array("label" => FALSE))
->setAction($this->generateUrl('article_comment', array('id' =>$article->getId())))
->getForm();
$commentForm->handleRequest($request);
if ($commentForm->isSubmitted() && $commentForm->isValid()) {
//Update existing user or create new
$em = $this->getDoctrine()->getManager();
$comment = $commentForm->getData()->getComment();
$user = $this->getUser($comment->getUser());
//Update the existing comment or get a new one
$user = $this->getUser($comment->getUser());
$comment = $this->getComment($user, $article, $comment);
//Set the user and article for the comment.
$comment->setUser($user);
$comment->setArticle($article);
$em->persist($comment);
$em->flush($comment);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('article_show', array('id' => $article->getId()));
}
return $this->render('article/comment.html.twig', array(
'form' => $commentForm->createView(),
));
} else {
return $this->render('article/comment.html.twig');
}
}
大丈夫です。それは私が今行ったことであり、それは魅力のように機能します。ビューに「if」があるか、それを行うための従来の方法があるかどうかは分かりませんでした。お返事をありがとうございます! – user2305673