2

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
    }
}
lvarayut
  • 13,963
  • 17
  • 63
  • 87
  • Can't you just give the route as a parameter ? – Brewal Feb 24 '14 at 15:30
  • So, I have to get the route of the current page using javascript in order to send the right route to the `controller`. I have been searching about it and still have no idea how to achieve this. – lvarayut Feb 24 '14 at 18:29

2 Answers2

1

You won't be able to access it from the controller directly. The ajax request is considered as a new master request to the server. (see http is stateless)

As suggested by @Brewal, a good idea would be to pass the route along with the ajax request.

To do that, you could do this in 3 steps:

1 Install FOSJsRoutingBundle

2 Expose the route you're trying to access

3 Send that routealong with the ajax request.

Community
  • 1
  • 1
Mick
  • 30,759
  • 16
  • 111
  • 130
  • Thanks for your response. I'm using the `FOSJsRoutingBundle`. The problem is that if I want to pass the route along with the ajax request, first of all, I have to get the route of the current page using javascript in order to send the right route to the `controller`. I have been searching about it and still have no idea how to achieve this. – lvarayut Feb 24 '14 at 18:30
  • 1
    +1 for FOSJsRoutingBundle. @LVarayut, you should edit your question with your current ajax call in javascript. – Brewal Feb 24 '14 at 18:32
  • @LVarayut I am not sure if there is a js way to get the route. However, you can get the route in your template: see [here](http://stackoverflow.com/questions/9378714/get-current-url-in-twig-template#answer-9384118), you can then decide how to use it. – Mick Feb 24 '14 at 18:39
  • Thanks both of you. Please take a look at my updated question. – lvarayut Feb 24 '14 at 18:43
1

A possible workaround without using FOSJsRoutingBundle in this case would be to pass the uri as parameter and the use the match method of the router to get the actual current route :

$this->get('router')->match($uri)

Giving you this ajax call :

var uri = window.location.pathname; // current page uri (something like `/hello/test`)
$.ajax({
    type: "POST",
    url: '/path/to/demo/ajax/update',
    data: JSON.stringify({
        id: patientId, 
        startDate: startDate, 
        endDate: endDate, 
        uri: uri // here you add the current uri
    }),
    dataType: "json",
    success: function (data) {
        // Doing something
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert('Error : ' + errorThrown);
    }
});

And your action :

public function handleAjaxUpdateAction(Request $request)
{
    $data = json_decode($request->getContent());

    $possibleRoutes = $this->get('router')->match($data->uri);

    if (in_array('demo_homepage', $possibleRoutes)) {
        // Do something
    } elseif (in_array('demo_anotherroute', $possibleRoutes)){
        // Do something
    }
}

But I think I would definitly make multiple AjaxUpdate like actions. At least one by controller. Then you don't have to wonder from where does the call comes from, and you will have shorter actions. If you plan to make ajax only controllers :

HomepageAjaxController
--> updateAction
--> deleteAction
--> createAction
--> readAction

AnotherAjaxController
--> updateAction
--> deleteAction
--> createAction
--> readAction
Brewal
  • 8,067
  • 2
  • 24
  • 37