如何在AJAX调用之间保持PHP对象存活(或在内存中)


How to keep PHP object alive (or in memory) between AJAX calls

我有以下类定义:

class DatasegmentationController
{
    public function indexAction()
    {
        $options['permissions'] = array(
            'canDelete'     => false,
            'canEdit'       => false
        );
        if ($this->getRequest()->isXmlHttpRequest()) {
            $table = $this->getRequest()->getParam('table');
            if ($table !== '' && $table !== null) {
                $utilStr = new UtilString();
                // This is a patch because class and tables names does not match
                // so far it only happens with company and this is only for
                // instantiate the proper class dynamically
                $param_table = $table;
                $table       = $table === 'companies' ? 'company' : $table;
                $classObj    = strpos($table, '_') !== false ? $utilStr->stringToCamelCase($table, '_') : $utilStr->stringToCamelCase($table);
                $className   = new $classObj();
                $module_map = $field_map[$param_table];
                /** @var  $module_map array */
                $fields = [];
                foreach ($module_map as $key => $value) {
                    $fields[] = [
                        'id'   => $key,
                        'text' => $key
                    ];
                }
                $conditions      = json_decode($this->_request->getParam('conditions'), true);
                $dynDataGridName = "DataSegmentation{$this->classObj}Grid";
                $dynMethodName   = "get{$this->classObj}GridModel";
                $gridObj = new $dynDataGridName(
                    $this->className->$dynMethodName($conditions),
                    $this->view->users_id,
                    "{$table}_list",
                    "{$table}.{$table}_id",
                    '/datasegmentation/index',
                    'editor',
                    $options
                );
                return $this->_helper->json([
                    'fields' => $fields,
                    'grid'   => $gridObj->getGridJs()
                ]);
            }
            if (isset($classObj, $className, $gridObj)) {
                $page  = $this->_request->getParam('page', 1);
                $limit = $this->_request->getParam('rows', 20);
                $col   = $this->_request->getParam('sidx', 1);
                $order = $this->_request->getParam('sord', 0);
                $search  = $this->_request->getParam('val', null);
                echo $gridObj->getData($page, $limit, $col, $order, $search);
            }
        }
    }
}

以上代码的作用如下:

    URL http://localhost/datasegmentation被称为
  • 视图呈现一个带有选项
  • 的选择元素(modules)
  • select#modules被改变我发送它的值作为URL的一部分,所以下一个AJAX调用变成:http://localhost/datasegmentation?table=companies(例如)
  • indexAction()函数然后执行$table不为空或不为null时的条件
  • 在所有这些东西中,它试图动态地生成所有东西,正如你可能在代码中注意到的。
  • 其中一个的东西是一个动态网格($gridObj),它有一个AJAX调用相同的indexAction(),但没有参数填充数据后得到渲染
  • 在视图中渲染网格后,它进行AJAX调用,并再次调用indexAction(),它跳过表格的条件,因为参数没有设置,并尝试第二个条件,但意外的是它失败了,因为代码需要工作的对象已经消失了。

在这种情况下,我的问题是:

    如何在AJAX调用之间保持对象的活动?存储在会话变量中?还有其他解决方法吗?
  • 如果答案是将它们存储在会话变量中,是否值得推荐?关于这个,这个和这个的答案呢?
  • 你会如何处理这种情况?

    第二个AJAX调用是向网格添加数据的调用,它依赖于动态参数。这就是我需要解决的问题。

我不知道这是否有用,但这是在Zend Framework 1项目上使用PHP 5.5.x进行测试和开发的

如何在AJAX调用之间保持对象的活动?存储在会话变量?还有其他解决方法吗?

将数据存储在会话变量中将数据存储在带有客户端ID的文件中(可以是登录、随机、IP等)在数据库中存储数据

如果答案是将它们存储在会话变量中,是否值得推荐?关于这个,这个和这个的答案呢?

如果您正在存储关键数据,请使用端到端加密,SSL, HTTPS。

你会如何处理这种情况?

使用会话变量