Symfony2 - 设置博客归档 [英] Symfony2 - Setting up a blog archive

查看:169
本文介绍了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?

我到目前为止:

    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.

接下来,您要显示该月份的帖子。为此,您需要使用教义查询构建器。这里有一个SO答案,但您可以搜索一些与查询日期有关的更多信息。 在doctrine 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天全站免登陆