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



我会根据我所做的一些研究重新定义我的问题吗?

我需要单独存储许多错误,如$_SESSION["客户端错误"]、$_SESSION["状态错误"]等。根据zend文档,对于每个错误,我都必须这样存储吗?

$client_error = new Zend_Session_Namespace(''client_error);
$state_error = new Zend_Session_Namespace('state_erro'); and so on?

这是我在控制器中的代码。我将其存储为$this->view->state_error_message=$state_error;

在视图中回显$this->state_error后,我想取消设置。

好吧,下面是我尝试过的其他几件事:在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; ?>

这不会返回任何结果。

好的,这是我更新的代码:

    public function clientInfoAction()
        {
            $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;
}

    public function policyInfoAction()
    {
         if (count($arrErrors) > 0)
         {
            // The error array had something in it. There was an error.
            $strError="";
            foreach ($arrErrors as $error)
            {
            $strError="";
            $errors->client_error = array();
            $errors->state_error  = array();

            foreach ($arrErrors as $error)
            {
                $strError .= $error;
                // to add errors to each type:
                $errors->client_error['client_error'] = $strError;
                $errors->client_error[] = $strError;
                $this->_redirect('/pdp/client-info/');

            }
               }
}

当我回显$this->client_errors时,我得到"Array"

以下是一些建议和建议,希望能让您走上正轨。

首先,当使用Zend_Session和/或Zend_Session_Namespace时,您永远不希望使用PHP的session_start()函数1。如果使用session_start()启动会话,然后尝试使用Zend_Session,则会引发另一个会话已存在的异常。

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

第二,你提到你有很多需要存储的消息,所以这可能不适合你,但请参阅FlashMessenger操作助手。这允许您在控制器中设置消息,然后在下一页请求中访问它。这些消息只在一个页面跃点内有效,因此在下一个页面加载后,它们将被删除。您可以使用FlashMessenger存储许多信息,但您对这些信息的访问并不是很受控制。您还可以在不同的命名空间中使用多个flash信使。

特别是为了解决你的问题,你可以做这样的事情:

// 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_errorstate_error创建了新的属性。从技术上讲,您不必使用多个Zend_SessionNamespace。

然后要清除下一页加载的消息,您可以这样做:

// 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_Controller_Action_Helper_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');
    }
}

为了完成,我在引导程序中启动会话。理论上,如果我需要它,即使不是没有伤害,也很好。

 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; ?>

最新更新