0

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

我需要单独存储很多错误,例如 $_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?

这是我在控制器中的代码。我将其存储为 $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 我得到“数组”

4

2 回答 2

1

这里有一些建议和建议,希望能让你走上正轨。

首先,当使用Zend_Sessionand/or时Zend_Session_Namespace,你永远不想使用 PHP 的session_start()函数1。如果您使用 开始一个会话session_start(),然后尝试使用Zend_Session,它将抛出另一个会话已经存在的异常。

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

其次,您提到您需要存储大量消息,因此这可能不适合您,但请参阅FlashMessenger操作助手。这允许您在控制器中设置消息,然后在下一页请求中访问它。消息只存在一个页面跃点,因此在下一页加载后,它们将被删除。您可以使用 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_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其中可以为您提供有关在会话命名空间中管理数据的一些想法。

于 2012-02-09T00:18:47.010 回答
1

我不知道这是否对您有帮助,但这里是一个控制器的代码,它只从表单中获取一个 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; ?>
于 2012-02-09T12:49:24.583 回答