symfony在呈现模板期间抛出异常

时间:2015-07-08 03:37:09

标签: php symfony propel

我是Symfony的新手,并尝试使用Acme / LibraryBundle显示表单。

但是出现了错误

"在呈现模板期间抛出异常("无法生成指定路线的URL" book_new"因为此类路线不存在。&#34 ;)在AcmeLibraryBundle:Book:new.html.twig at line 3。"

我的表格

<form action="{{ path('book_new') }}" method="post" {{ form_enctype(form) }}>
    {{ form_widget(form) }}

    <input type="submit" />
</form>

我的控制器:

namespace Acme\LibraryBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Acme\DemoBundle\Model\Book;

use Acme\LibraryBundle\Form\Type\BookType;

class BookController extends Controller
{
    public function newAction()
    {

        $book = new Book();
        $form = $this->createForm(new BookType(), $book);

        /*$request = $this->getRequest();

        if ('POST' === $request->getMethod()) {
            $form->handleRequest($request);

            if ($form->isValid()) {
                $book->save();

                return $this->redirect($this->generateUrl('book_success'));
            }
        }*/

        return $this->render('AcmeLibraryBundle:Book:new.html.twig', array(

                'form' => $form->createView(),

        ));

    }
}

1 个答案:

答案 0 :(得分:0)

由于我的routing.yml中不存在book_new,因此我将其更改为acme_library_homepage。

acme_library_homepage:
path:     /test
defaults: { _controller: AcmeLibraryBundle:Book:new }

在:

<form action="{{ path('book_new') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}

<input type="submit" />

<form action="{{ path('acme_library_homepage') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}

<input type="submit" />