我是"移植"我的旧网站代码进入了新的Symfony2.6。我有一个Controller来渲染表单并在博客中创建帖子。然后,一旦它有效,它就会在第二个控制器中被提交(POST方法)。当我调用路由来初始化控制器时,出于某种原因我忽略了它,它也调用了ShowAction($ slug),它失败了,因为它还没有创建帖子,因此它没有任何$ slug参数。为什么要调用这个showAction?尽管我输入了url才能显示表单。
以下是启动失败的行的错误日志:
1. in src/Blog/BlogBundle/Services/PostManager.php at line 80
2. at PostManager ->findBySlug ('create_post') in src/Blog/BlogBundle/Controller PostController.php at line 55
3. at PostController ->ShowAction ('create_post')
4. at call_user_func_array (array(object(PostController), 'ShowAction'), array('create_post')) in app/bootstrap.php.cache at line 3020
我不想调用ShowAction。
这是控制器代码:
/**
* Show a Post
*
* @param string $slug
*
* @throws NotFoundHttpException
* @return array
*
* @Route("/{slug}")
* @Template()
*/
public function ShowAction($slug)
{
$post = $this->getPostManager()->findBySlug($slug);
$form_comment = $this->createForm(new CommentType());
return array(
'post' => $post,
'form_comment' => $form_comment->createView()
);
}
/**
* Displays a form to create a new Post entity.
*
* @Route("/new_post", name="_blog_backend_post_new")
* @Template()
*/
public function newAction()
{
$form = $this->createForm(new PostType(), new PostModel(new Post()));
return array(
'form' => $form->createView(),
);
}
/**
* Creates a new Post entity.
*
* @Route("/create_post", name="_blog_backend_post_create")
* @Method("POST")
* @Template("BlogBundle:Backend/Post:new.html.twig")
*/
public function createAction()
{
$request = $this->getRequest();
$em = $this->getDoctrine()->getManager();
$form = $this->createForm(new PostType(), new PostModel(new Post()));
$formHandler = new PostHandler($form, $request, new Post(), $em);
if ($formHandler->process()) {
return $this->redirect($this->generateUrl('_blog_backend_post'));
}
return array(
'form' => $form->createView(),
);
}
我不知道它是否必要,但这里是涉及的模板:
* @Template(" BlogBundle:Backend / Post:new.html.twig"):
{% extends "::base.html.twig" %}
{% block content %}
<form class="well" action="{{ url('_blog_backend_post_create') }}" method="post" {{ form_enctype(form) }}>
{% include 'BlogBundle:Backend/Post:edit.form.html.twig' with {'form': form } %}
</form>
{% endblock %}
BlogBundle:后端/后:edit.form.html.twig:
{% if form_errors(form) %}
<div class="alert alert-block alert-error fade in form-errors">
{{ form_errors(form) }}
</div>
{% endif %}
<p>
<label for="">Title</label>
{% if form_errors(form.title) %}
<div class="alert alert-block alert-error fade in form-errors">
{{ form_errors(form.title) }}
</div>
{% endif %}
{{ form_widget(form.title, { 'attr': {'class': 'w100'} }) }}
</p>
<p>
<label for="">Body</label>
{% if form_errors(form.body) %}
<div class="alert alert-block alert-error fade in form-errors">
{{ form_errors(form.body) }}
</div>
{% endif %}
{{ form_widget(form.body, { 'attr': {'class': 'w100'} }) }}
</p>
<p>
<button class="btn" type="submit">Save</button>
<a class="btn" href="{{ url('_blog_backend_post') }}">Cancel</a>
</p>
{{ form_rest(form) }}
我缺少什么,我该如何纠正?先谢谢你。
答案 0 :(得分:2)
路由器匹配可能的第一条路线,在这种情况下&#34; / {slug}&#34; slug =&#34; create_post&#34;。
至少有两种解决方案,第一种是最简单的,第二种是我推荐的解决方案:
将showAction剪切/粘贴到文件的底部,在这种情况下,路由器首先将/ create_post与createAction匹配
您可以将其排除在外:@Route("/{slug}", requirements={"slug" = "^(?<!create_post).+"})
答案 1 :(得分:1)
问题是您要发布到/ create_post,而/ create_post又匹配路由/ {slug}。 slug被设置为'create_post'。使用Symfony 2路由器,第一个匹配获胜,因此调用showAction方法。
考虑将ShowAction路径设置为:'/ show / {slug}'。
或者您可以将ShowAction移动到控制器文件的底部。这有点危险,因为您可能会忘记并稍后添加其他操作。但无论哪种方式都有效。