I have a handleAjaxAction
in my controller
. In this action, I want to do different things depended on the route
or url
. So, I'm trying to get the route of the current page by using:
$request->attributes->get('_route');
However, it gave me the ajax
route, demo_ajax_update
, instead of the route of the current page, demo_homepage
.
How can I get the current route in Ajax
action?
EDIT:
My Ajax
code is following:
var route; <-------- Should be set as the route of the current page
$.ajax({
type: "POST",
url: Routing.generate('demo_ajax_update'),
data: JSON.stringify({id: patientId, startDate: startDate, endDate: endDate, route: route}),
dataType: "json",
success: function (data) {
// Doing something
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('Error : ' + errorThrown);
}
});
In the controller
:
public function handleAjaxUpdateAction(Request $request)
{
$data = json_decode($request->getContent());
if($data->route == 'demo_homepage'){
// Do something
}
elseif($data->route == 'demo_anotherroute'){
// Do something
}
}