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

时间:2012-02-07 23:21:14

标签: zend-framework zend-form-element zend-session

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

我需要分别存储很多错误,如$ _SESSION ['client_error'],$ _ SESSION ['state_error']等。 根据zend文档,我是否必须为每个错误存储它?

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

这是我在控制器中的代码。 我把它存储为     $这 - >视图 - > 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/');

在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-&gt; client_errors时,我得到'数组'

2 个答案:

答案 0 :(得分:1)

以下是一些建议,可以帮助您走上正确的道路。

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

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

其次,你提到你需要存储很多消息,所以这可能不适合你,但请参阅FlashMessenger动作助手。这允许您在控制器中设置消息,然后在下一页请求中访问它。这些消息仅适用于一个页面跃点,因此在下一页加载后,它们将被删除。您可以使用FlashMessenger存储许多消息,但您对它们的访问权限不受控制。您也可以在不同的命名空间中使用多个闪存信使。

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

// 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_Session_Namespaces。

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

// 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的源代码,它可以让您了解如何管理会话命名空间中的数据。

答案 1 :(得分:1)

我不知道这是否会对你有所帮助,但这里是控制器的代码,只需从表单中获取一个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');
    }

}

只是为了完成我在bootstrap中启动会话。关于理论,如果我需要它很好,如果不是没有伤害。

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