2013-01-15 19 views
2

我使用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,請幫我

回答

3
在updateAction

更新增值$id到您的模板變量:

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

並在您的模板中添加product_id參數去的path功能:

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

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

3

在我的情況下,錯誤不是在錯誤輸出模板文件,但在一根樹枝文件,它被延長。在我使用的樹枝文件中:

path(app.request.attributes.get('_route')) 

..嘗試獲取URL路徑。由於未提供必需的參數,因此創建了一個錯誤。

我用正確的方式來獲得的網址:

{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }} 

..和錯誤走了。

+0

這也給我修好了!在訪問/ blog/{id}樣式路線時,正在調用路徑(app.request.attributes.get('_ route'))在佈局中的某個位置。 –

0

在錯誤輸入ID而非憑證,讓這個錯誤發生 (異常被模板的渲染過程中引發的,其路由文件(以下簡稱「acme_store_update路線有一些缺少強制參數(id)。」)在AcmeStoreBundle:默認值:在第1行)

爲如index.html.twig:使用.yml文件 ibw_job_edit:

pattern: /{id}/edit 
defaults: { _controller: "IbwJobeetBundle:Job:edit" } 

,而不是這個u必須寫 ibw_job_edit:

pattern: /{token}/edit 
    defaults: { _controller: "IbwJobeetBundle:Job:edit" }