lvarayut
lvarayut

Reputation: 15349

Symfony2: How to get a current route in Ajax controller?

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
    }
}

Upvotes: 2

Views: 2392

Answers (2)

Brewal
Brewal

Reputation: 8199

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

Upvotes: 1

Mick
Mick

Reputation: 31959

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.

Upvotes: 1

Related Questions