会话数据在同一控制器上的两个不同呼叫之间丢失,为什么?

时间:2017-03-27 20:22:07

标签: php symfony session

我有一个运行Symfony 3.2.6(SF)和Zend Framework 1(ZF)的"混合" 项目。有时我需要从SF跳转到ZF,因为并非所有代码都在SF中重构。目前我遇到了问题,因为ZF和SF以不同的方式存储会话。到目前为止,我的解决方案是查找SF Request对象或布尔值。快速浏览下面的代码,该代码属于ZF控制器:

abstract class MainInitController extends Zend_Controller_Action
{
    ...
}

class ExportfileController extends MainInitController
{
    public static function registerZendDbSelect($reportId, $select, $options = [])
    {
        $sess    = new Zend_Session_Namespace('exportfile');
        $reports = $sess->reports;
        ...
        if (isset($options['request']) && $options['request'] instanceof Request) {
            $session = new Session();
            $session->set('reports', $reports);

            return $shaKey;
        }

        $sess->reports = $reports;

        return $shaKey;
    }

    public static function fetchQuery($shaKey, $sf = false)
    {
        if ($sf) {
            $session = new Session();
            $reportS = $session->get('reports');
        } else {
            $sess    = new Zend_Session_Namespace('exportfile');
            $reportS = $sess->reports;
        }
        ...

        return $report;
    }

    public function gridexportAction()
    {
        $shaKey      = $this->getRequest()->getParam('key');
        $sf          = (bool) $this->getRequest()->getParam('sf');
        ...
        $report      = self::fetchQuery($shaKey, $sf);
        ...
    }
}

方法registerZendDbSelect()在页面加载时执行。此方法属于ZF类,但它是从Symfony控制器调用的:

class AgreementEditorGrid extends DynamicGrid
{
    ...

    protected function getExportLink()
    {
        $reportId    = 'agreement_list';
        $exportQuery = clone $this->mappedSelect;
        $exportQuery->limit();
        $blocked      = ['actions'];
        $extra_fields = [];
        $options      = [
            'report_name'                => 'Agreement',
            'blocked_fields'             => $blocked,
            'extra_fields_selects_value' => $extra_fields,
            'request'                    => $this->request,
        ];

        $exportKey = ExportfileController::registerZendDbSelect($reportId, $exportQuery, $options);

        return "/exportfile/gridexport/key/{$exportKey}/sf/1";
    }
}

我已经通过添加这两个dump()句子检查了会话中存储的数据,它看起来如下:

(new \Symfony\Component\VarDumper\VarDumper())->dump($session->get('reports'));
(new \Symfony\Component\VarDumper\VarDumper())->dump($session->getId());

array:1 [
  "48362e9fcfbe08d81ddcf534cd3fe19dfc9c8d7f" => array:4 [
    "query" => Query {#522
      -origQuery: "SELECT `agreement`.`agreement_id` AS `agreement.agreement_id`, `agreement`.`AgreementNumber` AS `agreement.AgreementNumber`, `agreement`.`StartDate` AS `agreement.StartDate`, `agreement`.`EndDate` AS `agreement.EndDate`, `agreement`.`AgreementTypeID` AS `agreement.AgreementTypeID`, `agreement`.`CFProgramLevelID` AS `agreement.CFProgramLevelID`, `agreement`.`TAMFlag` AS `agreement.TAMFlag`, `agreement`.`ParentAgreementNumber` AS `agreement.ParentAgreementNumber`, `agreement`.`DistributorID` AS `agreement.DistributorID`, `agreement`.`CustomerSiteID` AS `agreement.CustomerSiteID`, `agreement`.`Source` AS `agreement.Source`, `agreement`.`Status` AS `agreement.Status` FROM `agreement`"
      -select: null
      -query: false
      -mapFunction: null
      -paginationCount: null
      -paginationOffset: null
      -pagination: false
      -curRow: null
      -curKey: 0
    }

    ...

Here是一个完整的参考文件。

当我点击调用了gridexportAction()的同一页面上的链接时,会调用方法registerZendDbSelect()

因此调用方法fetchQuery()。我通过转储内容reports方法检查了fetchQuery()的内容 - 但即使session_id没有更改,意外也是空的:

$session = new Session();
$reportS = $session->get('reports');

(new \Symfony\Component\VarDumper\VarDumper())->dump($session->get('reports'));
(new \Symfony\Component\VarDumper\VarDumper())->dump($session->getId()); 

null
"49tig5ghqevsd7t0qqu3hkm7l0"

这里有什么问题?我缺少Symfony中的会话管理或者PHP或网络中的一些基础?

0 个答案:

没有答案