当我尝试使用表单创建新实体时,我收到 NotFoundHttpException 错误。
这是用于创建表单和实体的代码 - CategoryController:
/**
* Displays a form to create a new Category entity.
*
* @Route("/new", name="category_new")
* @Method({"GET"})
*/
public function newAction(Request $request)
{
$entity = new Category();
$form = $this->createCreateForm($entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Creates a new Category entity.
*
* @Route("/", name="category_create")
* @Method("POST")
* @Template("AdminBundle:CategoryPanel:new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new Category();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('category_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Creates a form to create a Category entity.
*
* @param Category $entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Category $entity, ServiceCategory $parentCategory = null)
{
$form = $this->createForm(CategoryType::class, $entity, array(
'action' => $this->generateUrl('category_create'),
'method' => 'POST',
'parentCategory' => $parentCategory
));
$form->add('submit', SubmitType::class, array(
'label' => 'Create',
'attr' => array(
'class' => "btn btn-primary"
)
));
return $form;
}
CategoryType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TextType::class, array('label' => 'Category name'));
$parentCategory = $options["parentCategory"];
if($parentCategory != null){
$builder->add('parent', 'entity', array(
'class' => "CoreBundle:ServiceCategory",
'choices' => array($parentCategory)
));
}else{
$builder->add('parent', 'entity', array(
'class' => "CoreBundle:ServiceCategory",
'query_builder' => function(ServiceCategoryRepository $cp){
$qb = $cp->createQueryBuilder('c');
return $qb;
},
));
}
}
为什么这个代码在我只是试图创建它时正在寻找实体?
更新
new.html.twig
{% extends 'AdminBundle:AdminPanel:base.html.twig' %}
{% block body -%}
<h1>Category creation</h1>
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.parent) }}
<ul class="record_actions">
<li style="display: inline-block">
{{ form_widget(form.submit) }}
</li>
<li style="display: inline-block">
<a href="{{ path('category_panel_index') }}">
<button type="button" class="btn btn-primary">
Back to the list
</button>
</a>
</li>
</ul>
{{ form_end(form) }}
{%endblock%}
答案 0 :(得分:1)
这可能是多个路由之间的冲突,就像我的情况一样。
你可能有一些其他路径可能在其他控制器中有类似的路径(使用动态变量)使<>/new
指向其他地方。
请在var_dump
控制器中执行newAction
,检查执行是否正确。