2014-04-02 33 views
0

這是佈線後的路由信息​​:如何在Symfony中傳遞參數?

cacic_uorg_type_excluir: 
pattern: /uorg/type/excluir/{idUorgType} 
defaults: { _controller: CacicCommonBundle:UorgType:excluir, idUorgType: null} 
requirements: 
    idUorgType: \d+ 

這是我的控制器

public function excluirAction($idUorg, Request $request) 
{ 
    if (! $request->isXmlHttpRequest()) 
     throw $this->createNotFoundException('page not found'); 

    $uorgType = $this->getDoctrine()->getRepository('CacicCommonBundle:TipoUorg')->find($request->get('id')); 
    if (! $uorgType) 
     throw $this->createNotFoundException('UORG nor found'); 

    $em = $this->getDoctrine()->getManager(); 
    $em->remove($uorgType); 
    $em->flush(); 

    $response = new Response(json_encode(array('status' => 'ok'))); 
    $response->headers->set('Content-Type', 'application/json'); 

    return $response; 
} 

但我不斷收到錯誤

Controller "Cacic\CommonBundle\Controller\UorgTypeController::excluirAction()" requires that you provide a value for the "$idUorg" argument (because there is no default value or because there is a non optional argument after this one). 

回答

0

您可能需要更改默認一個空字符串,而不是NULL。您的要求也不符合您的默認值。試試這個:

cacic_uorg_type_excluir: 
    pattern: /uorg/type/excluir/{idUorgType} 
    defaults: { _controller: CacicCommonBundle:UorgType:excluir, idUorgType: ""} 
    requirements: 
     idUorgType: (^$|\d+) 
+0

感謝您的幫助,但這個錯誤仍然沒有任何變化 –

1

也許問題來自Request $request。在excluirAction中初始化請求(不要在參數中傳遞它)。我想你的方法只等待一個參數,因爲在你的錯誤中我們可以讀取[...]there is a non optional argument after this one

//... Don't forget to use Request at the top of the class 
public function excluirAction($idUorg/*, Request $request*/) 
{ 
    $request = new Request(); 
    // Your code ... 
} 
+0

感謝您的幫助,但這個錯誤仍然沒有任何變化 –

+0

我已經發表評論,並刪除了它,因爲我不河畔但也許你的路線你有'idUorgType'作爲變量,在你的方法中,參數是'$ idUorg'。我不知道它是否有發病。 – Debflav

+0

我已經做了一些測試,當我使用$ idUorgType時,它返回錯誤「找不到頁面」 –