Well.. i have this action in ArticleController:
/**
* @Route("/articles/{category}/{id}", name="article")
*/
public function getArticle($category, $id)
{
$articleRepo = $this->getDoctrine()->getRepository(Article::class);
$article = $articleRepo->find($id);
$categoryRepo = $this->getDoctrine()->getRepository(ArticleCategory::class);
$categories = $categoryRepo->findAll();
$categoryId = $categoryRepo->getCategoryIdByCode($category);
$lastArticles = $articleRepo->getLimitArticles($categoryId, 3);
$nextArticle = $articleRepo->getNextArticle($categoryId, $id);
$previousArticle = $articleRepo->getPreviousArticle($categoryId, $id);
if (is_null($nextArticle)) {
$nextArticle = $articleRepo->getRandomArticle($categoryId, $id);
}
if (is_null($previousArticle)) {
$previousArticle = $articleRepo->getRandomArticle($categoryId, $id);
}
$commentForm = $this->createForm(CommentFormType::class);
$answerForm = $this->createForm(CommentFormType::class);
$commentRepo = $this->getDoctrine()->getRepository(Comment::class);
$comments = $commentRepo->getComments($id);
foreach ($comments as $comment) {
$subcomments = $commentRepo->getSubComments($comment->getId());
$comment->subComments = $subcomments;
}
return $this->render('articles/article.html.twig', [
'article' => $article,
'categories' => $categories,
'lastArticles' => $lastArticles,
'nextArticle' => $nextArticle,
'previousArticle' => $previousArticle,
'commentForm' => $commentForm->createView(),
'answerForm' => $answerForm->createView(),
'comments' => $comments
]);
}
Everything is OK, it returns view file with needed data. But now i want to change it to works dynamically with AJAX. But i'm not sure how to do it.
First variant: To change this method to return all data to ajax call (JSON) and to fill elements with jquery. And to call method on page load and when someone add new comment.
Second variant: To create another method (first method still will render view, not JSON Response) which get only posts and to call it with AJAX when someone add new comment, without to send AJAX on page load, only if someone add post. (but here i will have same logic in two methods). Then to add new div for last added element to others div elements.
Third variant: ? If someone can help me ?
I dont want some to write code for me, i want someone to told me which is the right way to do it in Symfony.