Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Zend framework 在zend视图中取消设置会话变量_Zend Framework_Zend Form Element_Zend Session - Fatal编程技术网

Zend framework 在zend视图中取消设置会话变量

Zend framework 在zend视图中取消设置会话变量,zend-framework,zend-form-element,zend-session,Zend Framework,Zend Form Element,Zend Session,我将根据我所做的一些研究重新提出我的问题 我需要分别存储大量错误,如$u会话['client\u error']、$u会话['state\u error']等。 根据zend文档,对于每个错误,我是否必须像这样存储它 $client_error = new Zend_Session_Namespace(''client_error); $state_error = new Zend_Session_Namespace('state_erro'); and so on? 这是我在控制器中的代码。

我将根据我所做的一些研究重新提出我的问题

我需要分别存储大量错误,如$u会话['client\u error']、$u会话['state\u error']等。 根据zend文档,对于每个错误,我是否必须像这样存储它

$client_error = new Zend_Session_Namespace(''client_error);
$state_error = new Zend_Session_Namespace('state_erro'); and so on?
这是我在控制器中的代码。 我将它存储为 $this->view->state\u error\u message=$state\u error

在我回显视图中的$this->state\u错误后,我想取消设置它

好的,这里还有一些我尝试过的东西: 在policyInfoAction中的控制器中:

    session_start();
$error_message = new Zend_Session_Namespace('error_message');
$error_message="TEST";
$this->view->error_message=$error_message;
$this->_redirect('/pdp/client-info/');
在客户端信息中的视图中:

session_start();
<?php echo $this->error_message; ?>

当我回显$this->client\u错误时,我得到了“Array”

以下是一些建议和建议,希望能让您走上正确的道路

首先,当使用
Zend_Session
和/或
Zend_Session_命名空间
时,您永远不想使用PHP的
Session_start()
函数。如果使用
session\u start()
启动会话,然后尝试使用
Zend\u session
,它将抛出另一个会话已存在的异常

因此,从Zend Framework应用程序中删除所有
session_ustart()
调用

第二,您提到您需要存储很多消息,因此这可能不适合您,但请参阅action helper。这允许您在控制器中设置消息,然后在下一页请求中访问它。这些消息只在一个页面跳转中有效,因此在下一个页面加载后,它们将被删除。您可以使用FlashMessenger存储许多邮件,但您对这些邮件的访问不是很受控制。您还可以在不同的名称空间中使用多个flash Messenger

特别是要解决您的问题,您可以这样做:

// in controller that is validating
$errors = new Zend_Session_Namespace('errors');
$errors->client_error = array();
$errors->state_error  = array();

// to add errors to each type:
$errors->client_error['some_error'] = 'You had some error, please try again.';
$errors->client_error['other_error'] = 'Other error occurred.';
$errors->client_error[] = 'Other error, not using a named key';

$errors->state_error[] = MY_STATE_PARSING_0;
这里发生的事情是,我们得到一个名为
errors
的会话名称空间,为
client\u error
state\u error
这两个数组创建新属性。从技术上讲,您不必使用多个Zend_会话_名称空间

然后,要在下一页加载时清除消息,可以执行以下操作:

// from controller again, on the next page load
$errors = new Zend_Session_Namespace('errors');

// get the error arrays
$client_errors = (isset($errors->client_error)) ? $errors->client_error : array();
$state_errors  = (isset($errors->state_error)) ? $errors->state_error : array();

unset($errors->client_error, $errors->state_error); // delete from the session

// assign the values to the view
$this->view->client_errors = $client_errors;
$this->view->state_errors  = $state_errors;

另请参阅
Zend\u Controller\u Action\u Helper\u FlashMessenger
的源代码,它可以让您了解如何管理会话名称空间中的数据。

我不知道这是否对您有帮助,但下面是一个控制器的代码,该控制器只需从表单中获取id,然后根据该id收集数据并将该数据分配给会话(在整个模块中使用),然后在适当的时候取消设置该数据。并且永远不要离开索引页

<?php

class Admin_IndexController extends Zend_Controller_Action
{
    //zend_session_namespace('location')
    protected $_session;

    /**
     *set the layout from default to admin for this controller
     */
    public function preDispatch() {

        $this->_helper->layout->setLayout('admin');
    }

    /**
     *initiaize the flashmessenger and assign the _session property
     */
    public function init() {

        if ($this->_helper->FlashMessenger->hasMessages()) {
            $this->view->messages = $this->_helper->FlashMessenger->getMessages();
        }
        //set the session namespace to property for easier access
        $this->_session  = new Zend_Session_Namespace('location');

    }

    /**
     *Set the Station and gather data to be set in the session namespace for use
     * in the rest of the module
     */
    public function indexAction() {

        //get form and pass to view
        $form = new Admin_Form_Station();
        $form->setAction('/admin/index');
        $form->setName('setStation');

        $this->view->station = $this->_session->stationName;
        $this->view->stationComment = $this->_session->stationComment;
        $this->view->form = $form;

        try {
            //get form values from request object
            if ($this->getRequest()->isPost()) {

                if ($form->isValid($this->getRequest()->getPost())) {

                    $data = (object)$form->getValues();

                    //set session variable 'station'
                    $this->_session->station = $data->station;

                    $station = new Application_Model_DbTable_Station();
                    $currentStation = $station->getStation($this->_session->station);
                    $this->_session->stationName    = $currentStation->station;
                    $this->_session->stationComment = $currentStation->comment;

                    //assign array() of stations to session namespace
                    $stations = $station->fetchAllStation();
                    $this->_session->stations = $stations;

                    //assign array() of bidlocations to session namespace
                    $bidLocation  = new Application_Model_DbTable_BidLocation();
                    $bidLocations = $bidLocation->fetchAllBidLocation($this->_stationId);
                    $this->_session->bidLocations = $bidLocations;

                    $this->_redirect($this->getRequest()->getRequestUri());
                }
            }
        } catch (Zend_Exception $e) {

            $this->_helper->flashMessenger->addMessage($e->getMessage());
            $this->_redirect($this->getRequest()->getRequestUri());
        }
    }

    /**
     *Unset Session values and redirect to the index action
     */
    public function changestationAction() {

        Zend_Session::namespaceGet('location');
        Zend_Session::namespaceUnset('location');

        $this->getHelper('Redirector')->gotoSimple('index');
    }

}
以下是所有视图:

<?php if (!$this->station): ?>
    <div class="span-5 prepend-2">
        <?php echo $this->form ?>
    </div>
    <div class="span-10 prepend-2 last">
        <p style="font-size: 2em">Please select the Station you wish to perform Administration actions on.</p>
    </div>
<?php else: ?>
    <div class="span-19 last">
        <?php echo $this->render('_station.phtml') ?>
    </div>
<?php endif; ?>

请选择要对其执行管理操作的电台


我实际上想将错误消息发送到上一个有表单的页面。但我想你上面的方法可以工作。谢谢..我会尝试使用Zend_表单发送表单?该方法会发送到任何页面,具体取决于他们在表单后指向的页面。我有点困惑。因此我在/pdp/client info/中有一个表单,它将转到action='pd提交时的p/policy info。如果有错误,我将按照上面在policyInfo操作中提到的方式设置会话数组。我是否在clientInfo操作中取消设置它?尽管您提供了一个很好的解释,但很抱歉我感到困惑。好的,我更新了上面的代码。即使$strError有来自post变量的一些值,我也没有得到任何结果。从您发布的代码中,我发现了错误像您这样的OK在
policyInfoAction()中缺少
$errors=new Zend_Session_命名空间('errors');
您是否也确保在那里进行会话?到目前为止,这毫无意义。您想做什么…确切地说?您想验证表单并提醒用户吗?您想保存错误消息吗?您的数据来自哪里?请提供一些详细信息,以便我们有机会帮助您解决问题。
 protected function _initsession() {
        //start session
        Zend_Session::start();
    }
<?php if (!$this->station): ?>
    <div class="span-5 prepend-2">
        <?php echo $this->form ?>
    </div>
    <div class="span-10 prepend-2 last">
        <p style="font-size: 2em">Please select the Station you wish to perform Administration actions on.</p>
    </div>
<?php else: ?>
    <div class="span-19 last">
        <?php echo $this->render('_station.phtml') ?>
    </div>
<?php endif; ?>