Is $request->isXmlHttpRequest() reliable in symfony2?
ajax, symfony
Solution
`Symfony\Component\HttpFoundation\Request::isXmlHttpRequest()` is a simply utility-method that checks whether HTTP request came up with `X-Requested-With` header with value `XMLHttpRequest`. So it's as reliable as `X-Requested-With` header is.
However, this is not really important. The important thing to notice is the fact that when the user clicks on the back button the browser does not send a new HTTP request to the server. It just restores the page from its internal memory/cache.
Problem
Let's consider a scenario like below: - User selects filter button which create a AJAX call to a symfony2 controller and return the result in JSON format. - User select some other links and system will redirect him to the page - User select browser Back button. - User will see JSON response, but he should see the original page. My controller look like below : ``` /** * * * @Route("/ajax", name="ajax_route" , options={"expose"=true}) * @Template() */ public function someAction() { $request = $this->getRequest(); $json = array( ); if($request->isXmlHttpRequest()) { $res = json_encode($json); return new Response($res , 200 , array( 'Content-Type' => 'application/json' )); } return array( ); } ``` In other words, if user press back button the `if($request->isXmlHttpRequest())` returns true which is not the result I am looking for. Is it a normal behavior or what ?