Symfony2:函数参数的问题

时间:2015-05-28 06:08:56

标签: php oop symfony twig

在我的模板中,我调用了这样的函数:

  loadResults('asc');

  function loadResults(order) {
            return $.get('{{ url('_example_results', { 'order' : ''}) }}'+order, function (html) {
                $('#results').html(html);
            });
        }

我的控制器中的功能如下所示:

 public function resultsAction($order, Request $request)
 {
      // content is not crucial for solving my problem
 }

我的结果没有加载,我收到以下错误:

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

我需要做出哪些调整?

1 个答案:

答案 0 :(得分:2)

因为TWIG渲染页面之前你可以用js动作,所以你不能用TWIG组成正确的路线。 您可以使用两种方法归档问题:

1)使param成为可选项并将其传递给查询字符串,如下所示:

<强> JS

loadResults('asc');

  function loadResults(order) {
            return $.get('{{ url('_example_results') }}'+"?order="order, function (html) {
                $('#results').html(html);
            });
        }

<强>控制器

public function resultsAction(Request $request)
 {
      //...
      $order= $request->get('order','asc'); // second parameter is the default if is null 
 }

2)使用FOSJsRoutingBundle

希望这个帮助