【发布时间】:2014-05-27 19:17:16
【问题描述】:
我正在尝试创建一个基于 symfony 的论坛。在索引页面上,我需要向用户展示他们可以选择进入的部分。但是每个部分都有类别,所有这些都需要显示在一个视图中。它应该看起来像这样。
- 第 1 节
- 类别1
- 类别2
- 第 2 节
- 类别3
等等
我正在关注 symblog.co.uk 教程,并尝试根据他们的blogs/comments example 来做,有一个简单的问题,他们在 show action 中定义 cmets,每个博客都有 $cmets 变量,我需要我的类别可以从 $sections 变量访问。 对于每个部分,用户必须能够阅读类别并添加新类别
这是我的文件的样子。
索引操作,我所写的一切都发生的基本视图
{% block body %}
IndexAction of Page Controller
<form action="{{ path("EpiForumBundle_section_create") }}" method="post" {{ form_enctype(form) }} class="section">
{{ form_errors(form) }}
{{ form_row(form.name) }}
{{ form_rest(form) }}
<input type="submit" value="Submit" />
</form>
<table>
<th>date created
<th>name
{% for section in sections %}
<tr>
<td>
<div class="date"><time datetime="{{ section.created|date('c') }}">{{ section.created|date('l, F j, Y') }}</time></div>
</td>
<td>
<p><span class="highlight">{{ section.name }}</span></p>
</td>
</tr>
{% else %}
<p>There are no sections for this forum</p>
{% endfor %}
</table>
{% endblock %}
页面控制器
<?php
namespace Epi\ForumBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Epi\ForumBundle\Entity\Section;
use Epi\ForumBundle\Form\SectionType;
class PageController extends Controller
{
public function indexAction()
{
$section = new Section();
$form = $this->createForm(new SectionType(), $section);
$em = $this->getDoctrine()
->getEntityManager();
$sections = $em->getRepository('EpiForumBundle:Section')
->getLatestSections();
return $this->render('EpiForumBundle:Page:index.html.twig', array('sections' => $sections, 'form' => $form->createView()));
}
}
部分控制器
<?php
namespace Epi\ForumBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Epi\ForumBundle\Entity\Section;
use Epi\ForumBundle\Form\SectionType;
class SectionController extends Controller
{
public function createAction()
{
$section = new Section();
$form = $this->createForm(new SectionType(), $section);
$em = $this->getDoctrine()->getManager();
$request = $this->getRequest();
if($request->getMethod() == 'POST'){
$form->bind($this->getRequest());/*or $form->handleRequest($request); depends on symfony version */
$em->persist($section);
$em->flush();
return $this->redirect("/");
}
return $this->redirect("/");
}
}
部分类型:
<?php
namespace Epi\ForumBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class SectionType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name');
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Epi\ForumBundle\Entity\Section'
));
}
/**
* @return string
*/
public function getName()
{
return 'epi_forumbundle_section';
}
}
那么现在我应该把这些表格放在哪里?应该如何编写这些表格以了解 section_id?
【问题讨论】: