在控制器中路由。 Symfony2的

时间:2014-02-06 20:27:03

标签: php symfony routing

我在symfony中路由有问题。

我有一个功能

  public function showresultsAction($product)

需要运行对象$ product。

我尝试使用以下行调用该Action:

  $product = $form ->get('product'); 
  return $this->redirect($this->generateUrl('_gs_downloads_product', array(
       'object' => $product)));

当我运行我的应用程序时,我获得:

 Controller ProductController::showresultsAction()" requires that you provide a value 
 for the "$product" argument (because there is no default value or because there is a 
 non optional argument after this one).

感谢您的帮助。

我的路线定义:

_gs_downloads_product:
    path:  /download/list/product
    defaults: { _controller: AcmeProjectBundle:Product:showresults}  

2 个答案:

答案 0 :(得分:1)

问题在于您的路线定义。您案例中生成的网址将如下所示

"/download/list/product"

将您的路线更改为:

_gs_downloads_product:
    path:  /download/list/{product}
    defaults: { _controller: AcmeProjectBundle:Product:showresults}

您的网址将如下所示:

"/download/list/12"

假设您的$product变量为12

答案 1 :(得分:1)

查看路线,您没有在路线中定义参数,如

_gs_downloads_product:
    path:  /download/list/product/{object}
    defaults: { _controller: AcmeProjectBundle:Product:showresults} 

然后你的功能看起来像

public function showresultsAction($object)

您还可以将参数定义为可选参数或使用任何默认值

_gs_downloads_product:
    path:  /download/list/product/{object}
    defaults: { _controller: AcmeProjectBundle:Product:showresults, object: null} 

或使用默认值

_gs_downloads_product:
    path:  /download/list/product/{object}
    defaults: { _controller: AcmeProjectBundle:Product:showresults, object: 1} 

Routing