Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add Ajax capabilites to Symfony
    text
    copied!<p>I have a set of sessions in a page, which I want to remove using AJAX. i.e click on a link, and without having to navigate for a new page, just remove the session, and show a message on success. </p> <p>Now, as per the given answer (<em>which still did not work for me</em>), I have the following:</p> <p><strong>Controller</strong></p> <pre><code>use Symfony\Component\HttpFoundation\JsonResponse; //.. public function ajaxRemoveSessionAction() { $session = $this-&gt;getRequest()-&gt;getSession(); $session-&gt;remove('name'); return new JsonResponse(array('success' =&gt; true)); } </code></pre> <p><strong>routing:</strong></p> <pre><code>ajax_remove_session: pattern: /remove-session defaults: { _controller: FooTestBundle:Page:ajaxRemoveSession } </code></pre> <p><strong>twig:</strong></p> <pre><code>&lt;a href="#" id="remove_session"&gt;Remove session&lt;/a&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#remove_session').click(function(){ event.preventDefault(); $.ajax({ url: {{ url('ajax_remove_session') }}, cache: false, success: function(result){ $(".success").append(result); } }); }); }); &lt;/script&gt; </code></pre> <hr> <h1><strong>UPDATING QUESTION:</strong></h1> <p>With all the codes found in the the routing, controller and template </p> <hr> <blockquote> <p>Controller: PageController.php</p> </blockquote> <p>/src/Simon/TestBundle/Controller/PageController.php</p> <pre><code>&lt;?php namespace Simon\TestBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\JsonResponse; class PageController extends Controller { public function helloAction($name) { $session = new Session(); $session-&gt;start(); $session-&gt;get('name', 'Drak'); $session-&gt;get('name'); $session-&gt;getFlashBag()-&gt;add('notice', 'Profile Updated'); $messages = null; foreach($session-&gt;getFlashBag()-&gt;get('notice', array()) as $message){ $messages = $message; } return $this-&gt;render('SimonTestBundle:Page:index.html.twig', array('name' =&gt; $name.' '.$messages)); } public function ajaxRemoveSessionAction() { // Destroy the desired session $session = $this-&gt;getRequest()-&gt;getSession(); $session-&gt;remove('name'); return new JsonResponse(array('success' =&gt; true)); } } </code></pre> <blockquote> <p>Template: Twig template</p> </blockquote> <p>/src/Simon/TestBundle/Resources/views/Page/index.html.twig</p> <pre><code>{% extends 'SimonTestBundle::layout.html.twig' %} {% block body %} &lt;a href="#" id="remove_session"&gt;Remove session&lt;/a&gt; &lt;script type="text/javascript"&gt; $('#remove_session').click(function(e){ e.preventDefault(); $.ajax({ url: {{ url('ajax_remove_session') }}, cache: false, success: function(html){ // do something on success } }).fail(function(event){ console.log(event); }); }); }); &lt;/script&gt; {% endblock %} </code></pre> <blockquote> <p>Routing: </p> </blockquote> <p>/src/Simon/TestBundle/Resources/config/routing.yml</p> <pre><code>simon_test_homepage: pattern: /hello/{name} defaults: { _controller: SimonTestBundle:Page:hello } ajax_remove_session: pattern: /remove-session defaults: { _controller: SimonTestBundle:Page:ajaxRemoveSession } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload