Symfony2:函数参数问题
Symfony2: Problems with function parameter
在我的模板中,我调用了这样一个函数:
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).
我需要做哪些调整?
因为 TWIG 在您可以使用 js 之前呈现页面,所以您无法使用 TWIG 编写正确的路由。
您可以使用两种方法归档您的问题:
1) 使参数可选并将其传递给查询字符串,如下所示:
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
希望对您有所帮助
在我的模板中,我调用了这样一个函数:
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).
我需要做哪些调整?
因为 TWIG 在您可以使用 js 之前呈现页面,所以您无法使用 TWIG 编写正确的路由。 您可以使用两种方法归档您的问题:
1) 使参数可选并将其传递给查询字符串,如下所示:
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
希望对您有所帮助