symfony会话未保存

时间:2018-04-25 14:42:13

标签: symfony session symfony-2.8

我目前正在一个有很多桌子的网站上工作。 几乎所有的表都有过滤器,并且由于用户不想在重新加载时丢失所有过滤器,我将在会话中保存过滤器。

到目前为止,所有这些都有效,每次过滤器更改时都会运行ajax调用以在会话中保存新过滤器。 我的问题是当我尝试清除过滤器时。

当用户点击清除按钮或关闭最后一个过滤器框时,会话密钥被清空,并且它打印为空,但是当我重新加载页面时,会加载最后一个过滤器,并且会话打印它们就好像我永远不会删除它们。

我用来填充/清空会话的功能:

public function addPersistenceAction(Request $request)
{
    $parameters = $request->request->all();
    $session = $this->get('session');
    $session->start();//I tried removing/adding this line but it didn't change anything

    //if the key wasn't send, nothing happends
    if (!isset($parameters['storage'])){
        return new JsonResponse(0);
    }

    //if no data was send, it removes the key. 
    if(!isset($parameters['data'])) {
        $session->remove($parameters['storage']);
        // I also tried setting an empty array but it didn't work either
        //$session->set($parameters['storage'], []);
        return new JsonResponse($session->all()); // this prints the correct data
    }

    //if the data was sent, it is set. This works perfectly
    $session->set(
        $parameters['storage'],
        $parameters['data']
    );

    return new JsonResponse($session->get($parameters['storage']))
}

该函数始终返回我期望的内容,但每次重新加载页面时,会话仍然具有最后一个过滤器。我尝试过不同的过滤器,它总是最后一个过滤器,所以它不能在我的控制器中进行编码。

有趣的是,我注意到如果我在重新加载页面之前手动运行获取过滤器数据的函数,它会显示正确的值,如果我在此之后重新加载,则过滤器被正确清空。

所以我的问题:我的会话似乎没有得到保存是有原因的吗?有没有办法以编程方式"强制" symfony保存会话数据?

一些有用的代码:
获取会话数据的函数:

/**
 * @param Request $request
 * @return JsonResponse
 */
public function getSessionDataAction(Request $request){
    $parameters = $request->request->all();
    $session = $this->get('session');
    $session->start();

    if (!empty($parameters['storage'])) {
        $data = $session->get($parameters['storage'])?:[];
        return new JsonResponse($data);
    }else{
        return new JsonResponse(array('status' => 'failed', 'message' => 'storage is mandatory'));
    }
}

我的控制器功能:

/**
 * @param Request $request
 * @return Response
 */
public function indexAction(Request $request)
{
    $this->denyAccessUnlessGranted(RoleVoterHelper::SECTION_USER_VIEW);

    $session = $request->getSession();

    //I checked and the session key I use is not touched in this function
    $filterWidgets = $this->get('dmt.filter.manager')->getWidgetData('user_filter');

    if (!$this->get('dmt.role_voter')->getExtGranted(RoleVoterHelper::SECTION_USER_VIEW)) {
        $filterWidgets['company'] = $this->getDoctrine()->getRepository('BugTrackerModelBundle:Company')->findByAuthorities($this->getUser()->getAuthorities());
    }
    $authorities = $this->getDoctrine()->getRepository('BugTrackerModelBundle:Authority')->findAll();
    return $this->render('DMTBundle:User:index.html.twig', [
        'filter_widgets' => $filterWidgets,
        'usersColumns'   => $session->get('usersColumns'),
        'usersSort'      => $session->get('usersSort'),
        'isSuperAdmin'   => $this->isGranted(UserHelper::ROLE_SUPER_ADMIN),
        'authorities'     => $authorities,
    ]);
}

页面打开时调用的javascript:

$.post(dependencies.bridgeGetUrl, {
    // something like "project_page_filter", the key for the session.
    storage : dependencies.storage
}).success(function(response){
    dependencies.filterManager.hook(function(app){
        app.scheme = response;
        app.readScheme();
    });
    run(dependencies);
    if(typeof dependencies.callback == "function"){
        dependencies.callback(dependencies.params);
    }
});

向会话发送任何修改的javascript:
App.scheme始终是过滤器的当前值

dependencies.filterManager.setLifecycle(["add", "change-value"], function(app) {
    $.post(dependencies.bridgeUrl, {
        storage : dependencies.storage,
        data : app.scheme
    });
}).setLifecycle(["remove-all", "remove"], function(app) {
    $.post(dependencies.bridgeUrl, {
        storage : dependencies.storage,
        data : app.scheme
    });

    $("#filterSubmit").click();
});

0 个答案:

没有答案