简体   繁体   中英

symfony An exception has been thrown during the rendering of a template

I am new in Symfony and trying to display the form using Acme/LibraryBundle.

but there was an Error

"An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "book_new" as such route does not exist.") in AcmeLibraryBundle:Book:new.html.twig at line 3."

My Form

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

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

My Controller:

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(),

        ));

    }
}

Since the book_new does not exist in my routing.yml, I changed it to acme_library_homepage.

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

Before:

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

<input type="submit" />

After

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

<input type="submit" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM