Symfony2 - 建立一个博客档案 [英] Symfony2 - Setting up a blog archive

查看:23
本文介绍了Symfony2 - 建立一个博客档案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为博客站点设置博客存档,用户点击日期并显示相应的帖子.(见图)我知道我需要检索我所有的博客文章并按日期排序,但之后的步骤对我来说是模糊的.获取这些数据,然后按月/年对其进行排序并将其传递给模板是我遇到问题的部分.

I've been working on trying to setup a blog archive for a blog site where the use clicks on a date and the corresponding posts appear. (see image) I understand I need to retrieve all my blog posts and sort by date, but the steps after that are foggy to me. Taking that data then sorting it by month/year and passing it to a template is the part I am having trouble with.

有人可以说明我做错了什么或提供一个简单的工作示例吗?

Can someone shed some light on what I am doing wrong or provide a simple working example?

到目前为止我所拥有的:

What I have thus far:

    public function archiveAction()
    {
        $em = $this->getDoctrine()->getManager();

//        $query = $em->getRepository('AcmeProjectBundle:Blog')
//            ->findAll();

        $blogs = $em->getRepository('AcmeProjectBundle:Blog')
            ->getLatestBlogs();

        if (!$blogs) {
            throw $this->createNotFoundException('Unable to find blog posts');
        }

        foreach ($blogs as $post) {
            $year = $post->getCreated()->format('Y');
            $month = $post->getCreated()->format('F');
            $blogPosts[$year][$month][] = $post;
        }

//        exit(\Doctrine\Common\Util\Debug::dump($month));

        return $this->render('AcmeProjectBundle:Default:archive.html.twig', array(
            'blogPosts' => $blogPosts,
        ));
    }

推荐答案

你想告诉你的 archiveAction 哪个月被实际点击,所以你需要一个或多个参数给它:http://symfony.com/doc/current/book/controller.html#route-parameters-as-controller-arguments(我会为我的参数做类似/archive/{year}/{month}/的事情,但这取决于你.)然后当有人去你 myblog.com/archive/2014/04,他们会看到那些帖子.

You want to tell your archiveAction which month was actually clicked, so you need to one or more parameters to it: http://symfony.com/doc/current/book/controller.html#route-parameters-as-controller-arguments (I would do something like /archive/{year}/{month}/ for my parameters, but it's up to you.) Then when someone goes you myblog.com/archive/2014/04, they would see those posts.

接下来,您要显示该月的帖子.为此,您需要使用 Doctrine 查询构建器.这是关于它的一个 SO 答案,但您可以搜索更多与查询日期有关的内容.选择学说 2 中日期之间的条目

Next, you want to show the posts for that month. For this you'll need to use the Doctrine Query builder. Here's one SO answer on it, but you can search around for some more that pertain to querying for dates. Select entries between dates in doctrine 2

这篇关于Symfony2 - 建立一个博客档案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆