dqypcghd381390
2017-11-13 09:49
浏览 30
已采纳

在Symfony中创建帖子的索引页面

I am new to Symfony 2.7 and have limited knowledge of php.

I have a system whereby posts are created via the admin resulting in new pages.

I am trying to use these to create an index page showing all the current posts. Eventually this would probably just show title perhaps and i should be able to adjust the view but for now i'm trying to work out how do i get the posts to be got via the controller?

I've looked at the Symfony demo application but can't see how to adjust this to what i have - the annotation routing confuses me a little also as i am using YAML.

Controller

class PostController extends Controller
{

 /**
 * @param Post $post
 *
 * @return Response
 *
 * @ParamConverter("post", options={"mapping": {"name": "name"}})
 */

    public function viewAction(Post $post)
        {
    return $this->render('MyBundle:Post:view.html.twig', ['post' => $post]);
        }

}

Routing

my_results:
    path: /results/{name}
    methods: [ GET ]
    defaults: { _controller: MyBundle:Post:view }
    requirements:
        name: \w+

So far I have add the extra routing...

my_indexresults:
path: /results
methods: [ GET ]
defaults: { _controller: MyBundle:Post:index }
requirements:
    name: \w+

..and i have added to the controller...

public function indexAction()
{
    $post = $this->getDoctrine()->getRepository('MyBundle:Post')->findAll();

    return $this->render('MyBundle:Post:index.html.twig', array('post' => $post));
}

Below is the relevant parts of index.html.twig , containing what I would use a loop of the posts, if i could tie up with the controller

<!DOCTYPE html>
<html lang="en">
<head>
<title="Posts Index">
</head>
<body>
{% block content %}

{% for post in posts %}
        <div id="{{ post }}">{{ post }}</div>
{% endfor %}

{% endblock %}
</body>
</html>

..but can't work out how to pull in the posts. If it's possible to tell without looking at the entity, how could I do this please?

图片转代码服务由CSDN问答提供 功能建议

我是Symfony 2.7的新手,对php知之甚少。

我有一个系统,通过管理员创建帖子,生成新页面。

我正在尝试使用这些来创建一个显示所有当前帖子的索引页面。 最终这可能只是显示标题,我应该能够调整视图但是现在我想弄清楚如何通过控制器获取帖子?

我查看了Symfony演示应用程序,但无法看到如何根据我的情况进行调整 - 注释路由也让我感到困惑,因为我正在使用YAML。

< strong>控制器

 类PostController扩展Controller 
 {
 
 / ** 
 * @param Post $ post 
 * 
 * @  return Response 
 * 
 * @ParamConverter(“post”,options = {“mapping”:{“name”:“name”}})
 * / 
 
公共函数viewAction(Post $ post)\  n {
返回$ this-&gt; render('MyBundle:Post:view.html.twig',['post'=&gt; $ post]); 
} 
 
} 
    
 
 

路由

  my_results:
 path:/ results / {name} 
 methods:[  GET] 
默认值:{_ control:MyBundle:Post:view} 
要求:
名称:\ w + 
   
 
 

到目前为止,我添加了 额外路由...

  my_indexresults:
path:/ results 
methods:[GET] 
defaults:{_ control:MyBundle:Post:index} 
requirements:
 name  :\ w + 
   
 
 

..我已添加到控制器...

  public function indexAction(  )
 {
 $ post = $ this-&gt; getDoctrine() - &gt; getRepository('MyBundle:Post') - &gt; findAll(); 
 
返回$ this-&gt; render('MyBundle: 发布:index.html.twig',array('post'=&gt;  $ post)); 
} 
   
 
 

以下是index.html.twig的相关部分,包含我将使用的帖子循环,如果我 可以与控制器绑定

 &lt;!DOCTYPE html&gt; 
&lt; html lang =“en”&gt; 
&lt; head&gt; 
&lt; title =“帖子索引 “&gt; 
&lt; / head&gt; 
&lt; body&gt; 
 {%block content%} 
 
 {%{%post in posts%} 
&lt; div id =”{{post}}“&gt;  {{post}}&lt; / div&gt; 
 {%endfor%} 
 
 {%endblock%} 
&lt; / body&gt; 
&lt; / html&gt; 
   
  
 

..但无法弄清楚如何拉入帖子。 如果可以在不看实体的情况下判断,我该怎么办呢?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题