doufu1504 2018-11-02 10:19
浏览 33

在Symfony中使用AJAX的注释和子注释

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 高缺失率数据如何选择填充方式
    • ¥50 potsgresql15备份问题
    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!