How to add Ajax capabilites to Symfony

后端 未结 4 679
旧巷少年郎
旧巷少年郎 2021-01-20 03:47

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

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-20 04:24

    Example controller:

    use Symfony\Component\HttpFoundation\JsonResponse;
    
    
    public function ajaxRemoveSessionAction()
    {
        // Destroy the desired session
        $session = $this->getRequest()->getSession();
        $session->remove('name');
    
        return new JsonResponse(array('success' => true));
    }
    

    Example routing:

    ajax_remove_session:
        pattern:  /remove-session
        defaults: { _controller: FooTestBundle:Page:ajaxRemoveSession }
    

    Example twig:

    Remove session
    
    
    

    These are just examples need testing.

提交回复
热议问题