[英]Symfony: use parameter from route in other function
My controller: 我的控制器:
**
* @Route("/", name="homepage")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction(Request $request)
{
return $this->render('default/index.html.twig');
}
/**
* @Route("/title/{title}", name="Title")
* @param Request $request
* @param $title
* @return \Symfony\Component\HttpFoundation\Response
*/
public function TitleAction(Request $request, $title)
{
//Some DB calls to retrieve data with 'where .. = $title'
return $this->render('default/title.html.twig', array(
'title' => $title,
'results' => $results[0],
'cs' => $cs,
'gs' => $gs,
...
}
}
/**
* @param Request $request
* @return JsonResponse
*/
public function updateDataAction(Request $request)
{
//Here I also want to use $title so I can use it in my query
$dataCS = $request->get('inputCS');
$dataGS = $request->get('inputGS');
if(!empty($dataGS) and !empty($dataCS)) {
// $results = Query
}
else {
// $results = Query
}
return new JsonResponse(array('results' => $results[0]));
}
My AJAX call in script.js: 我在script.js中的AJAX调用:
$(document).ready(function () {
$("#filter1").change(function() {
var inputCS = $(this).val();
if(inputCs.length >= 1) {
var data = {inputCS: inputCS};
$.ajax({
type: "POST",
url: ROOT_URL + "default/update/data",
data: data,
dataType: 'json',
timeout: 3000,
success: function(results){
//some innerHTML replacements
if(result.low === null) {
document.getElementById("low").innerHTML = 'N/A'
}
else {
document.getElementById("low").innerHTML =
Math.floor(Number(result.result.low));
}
},
error: function() {
//
}
})
}
});
})
My twig: Filters: 我的树枝:过滤器:
<select id="filter1">
<option selected disabled>Choose</option>
{% for c in cs %}
<option value="{{ c.name }}">{{ c.name }}</option>
{% endfor %}
</select>
<select id="filter2">
<option selected disabled>Choose</option>
{% for g in gs %}
<option value="{{ g.name }}">{{ g.name }}</option>
{% endfor %}
</select>
Where result is used: 使用结果的地方:
{% if results.low != 0 %}
<p class="amount" id="low">{{ results.low|round(0, 'floor') }}</p>
{% else %}
<p class="amount">N/A</p>
{% endif %}
A bit background info: With function TitleAction I show some results on the twig page that is based on the value from the route (title/{title}) - with the help of some DB calls (with a where
clause). 一点背景信息:通过TitleAction函数,我在树枝页面上显示了一些基于路由值(title / {title})的结果-借助一些DB调用(带有
where
子句)。 On this page I have 2 filters which you can use to filter the results that are shown on the page. 在此页面上,我有2个过滤器,可用于过滤页面上显示的结果。 When a filter is triggered (value selected) I do an AJAX call to the updateDataAction function where I do again some specific DB calls, but with an extra
where
clause and the filter as value. 当触发过滤器(选择值)时,我对updateDataAction函数进行AJAX调用,在此我再次执行一些特定的DB调用,但附加了
where
子句和过滤器作为值。 I return the filtered result set with a JSON respons to my AJAX call. 我将过滤后的结果集与JSON响应返回给我的AJAX调用。 At the success I do some interHTML replacements on the original values.
成功的话,我会对原始值进行一些interHTML替换。
My question is the following. 我的问题如下。 How can I pass or use the same value {title} from the route (title/{title}) so I can also here use that specific value in my query.
我如何在路由中传递或使用相同的值{title}(标题/ {title}),以便在这里也可以在查询中使用该特定值。
I tried to also use the @route
annotation on that function, but no results and the $request->query...
didn't help either. 我也尝试在该函数上使用
@route
批注,但没有结果,并且$request->query...
也无济于事。
If I've understood your question correctly, what I'd do is have the {{results.title}}
in an HTML attribute that you can read with your JS when making the AJAX request. 如果我正确理解了您的问题,我要做的是在HTML属性中添加
{{results.title}}
,您可以在发出AJAX请求时使用JS读取该属性。
eg 例如
<body data-title="{{results.title}}">
Then in your JS: 然后在您的JS中:
$("#filter1").change(function() {
var inputCS = $(this).val();
var title = $('body').data('title');
if(inputCs.length >= 1) {
var data = {
inputCS: inputCS,
title: title
};
$.ajax({
type: "POST",
url: ROOT_URL + "default/update/data",
data: data,
dataType: 'json',
timeout: 3000,
success: function(results){
// ...
},
error: function() {
//
}
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.