模板渲染过程中抛出异常

Lal*_*han 4 symfony-2.1

我正在使用 symfony2.16 中产品的 id 更新值,也使用 MngoDBbundle

但我收到这样的错误

在第 1 行的 AcmeStoreBundle:Default:index.html.twig 中,在渲染模板期间抛出了异常(““acme_store_update”路由缺少一些必需参数(“id”)。)。

这是我的控制器,请检查更新操作

<?php

namespace Acme\StoreBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Acme\StoreBundle\Document\Product;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;


class DefaultController extends Controller
{
    /**
     * @Route("/hello/{name}")
     * @Template()
     */
    public function indexAction($name)
    {
        return array('name' => $name);
    }

// ...

    /**
     * @Route("/create", name="acme_store_create")
     * @Template()
     */
    public function createAction(Request $request)
    {
        $product = new Product();
        //$product->setName('apple');
        //$product->setPrice('19.99');
        $form = $this->createFormBuilder($product)
            ->add('name', 'text')
            ->add('price', 'text')
            ->getForm();
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {
                $req = $request->request->get('form');
                $product->setName($req['name']);
                $product->setPrice($req['price']);
                $dm = $this->get('doctrine.odm.mongodb.document_manager');
                $dm->persist($product);
                $dm->flush();
                return new Response('Name ='.$product->getName().', price ='.$product->getPrice());
            }
         }
            return $this->render('AcmeStoreBundle:Default:index.html.twig', array(
                'form' => $form->createView(),
            ));
    }
    // ...
    /**
     * @Route("/show/{id}", name="acme_store_show")
     * @Template()
     */
    public function showAction($id)
    {
        $product = $this->get('doctrine.odm.mongodb.document_manager')
            ->getRepository('AcmeStoreBundle:Product')
            ->find($id);

        if (!$product) {
            throw $this->createNotFoundException('No product found for id '.$id);
        }
        return new Response('Name ='.$product->getName().', price ='.$product->getPrice());
        // do something, like pass the $product object into a template
    }
    // ...
    /**
     * @Route("/update/{id}", name="acme_store_update")
     * @Template()
     */
    public function updateAction(Request $request,$id)
    {
        $dm = $this->get('doctrine.odm.mongodb.document_manager');
        $product = $dm->getRepository('AcmeStoreBundle:Product')->find($id);
        $form = $this->createFormBuilder($product)
            ->add('name', 'text')
            ->add('price', 'text')
            ->getForm();
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {
                $req = $request->request->get('form');
                $product->setName($req['name']);
                $product->setPrice($req['price']);
                $dm->flush();
                return new Response('Name ='.$product->getName().', price ='.$product->getPrice());
            }
        }
            return $this->render('AcmeStoreBundle:Default:index.html.twig', array(
                'form' => $form->createView(),
            ));

    }
            // ...

}
Run Code Online (Sandbox Code Playgroud)

我的 index.html.twig 是

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

    <input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)

如何使用 id 更新值请帮助我

Gin*_*tro 5

在 updateAction 中添加$id到您的模板变量中:

return $this->render('AcmeStoreBundle:Default:index.html.twig', array(
    'form' => $form->createView(),
    'product_id' => $id,
));
Run Code Online (Sandbox Code Playgroud)

并在您的模板中将product_id参数添加到path函数中:

<form action="{{ path('acme_store_update', {id: product_id}) }}" method="post" {{ form_enctype(form) }}>
    ....
Run Code Online (Sandbox Code Playgroud)

http://symfony.com/doc/current/book/routing.html