我使用symfony2.16產品也使用MngoDBbundle一個例外模板
的ID更新的價值,但我得到的錯誤是這樣
例外的渲染過程中被拋出模板的渲染過程中已經被拋出(以下簡稱「 ‘acme_store_update’路線有一些缺少強制參數 (」 ID「)。」)在AcmeStoreBundle:默認:index.html.twig在1號線
這是我的控制器請更新動作
<?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(),
));
}
// ...
}
我index.html.twig是
<form action="{{ path('acme_store_update') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<input type="submit" />
</form>
如何使用ID,請幫我
這也給我修好了!在訪問/ blog/{id}樣式路線時,正在調用路徑(app.request.attributes.get('_ route'))在佈局中的某個位置。 –