【问题标题】:How to pass argument in Symfony?如何在 Symfony 中传递参数?
【发布时间】:2014-04-02 12:39:58
【问题描述】:

这是发布路由的路由信息​​:

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

【问题讨论】:

    标签: php symfony controller routing


    【解决方案1】:

    也许问题来自Request $request。在 excluirAction 中初始化 Request(不要在参数中传递)。我猜你的方法只等待一个参数,因为在你的错误中我们可以读取[...]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 ...
    }
    

    【讨论】:

    • 我发表了评论并删除了它,因为我不是 sur 但也许在你的路线中你有 idUorgType 作为变量,在你的方法中参数是 $idUorg。不知道有没有发病。
    • 我做了一些测试,当我使用 $idUorgType 它返回错误“页面未找到”
    • 那么您现在可以访问您的操作了吗?当我查看您的代码时,那是因为它不是 XmlHttpRequest(抛出异常 'page not found')。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-06-10
      • 2011-07-20
      • 2017-08-23
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 1970-01-01
      相关资源
      最近更新 更多