如何在Symfony中传递参数?

时间:2014-04-02 12:39:59

标签: php symfony controller routing

这是邮政路线的路线信息:

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).

2 个答案:

答案 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 ...
}

答案 1 :(得分:0)

您可能需要将默认值更改为空字符串,而不是NULL。您的要求也不符合您的默认值。试试这个:

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