我通常使用这样的代码。
<a href="{{ path('person_edit', {'id': 0}) }}' + document.getElementById('person_search').value;">
Goto item
</a>
这将为路由/edit/{id}
生成正确的url,但是当我可以将路由更改为{id}/edit
时,我必须重写此代码。(不是很有效)
是否有可能在'id'中使用document.getElementById('person_search').value
?
如(不工作):
<a href="{{ path('person_edit', {'id': 'document.getElementById('person_search').value'}) }}">
Goto item
</a>
提前感谢。
不,这是不可能的,因为小枝代码被解释并在服务器端执行,例如,在浏览器中执行javascript之前,你可以肯定地做的是使用查询参数,如:
path = '{{ path('person_edit', {'id': 0}) }}?userId='+document.getElementById("user_id").value;
在控制器中,你喜欢
$request = $this->getRequest();
$userId= $request->get("userId");
但这样你的url总是id为0所以提交它或用javascript构建url或使用这个bundle来完成这项工作
Fos Routing Js Bundle