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

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」が表示されます

4

2 に答える 2

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 に答える
1

ここでは、うまくいけば正しい軌道に乗るためのアドバイスと提案をいくつか紹介します。

まず、Zend_Sessionand/orZend_Session_Namespaceを使用する場合、PHP のsession_start()関数1を使用したくありません。でセッションを開始してsession_start()から を使用しようとするとZend_Session、別のセッションが既に存在するという例外がスローされます。

したがって、session_start()Zend Framework アプリケーションからすべての呼び出しを削除してください。

次に、保存する必要があるメッセージがたくさんあるとおっしゃっていたので、これは適切ではないかもしれませんが、FlashMessengerアクション ヘルパーを参照してください。これにより、コントローラーにメッセージを設定し、次のページ要求でそれにアクセスできます。メッセージは 1 ページ ホップの間だけ存在するため、次のページの読み込み後に削除されます。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_errorと呼ばれるセッション名前空間を取得してstate_errorいることです。これは両方とも配列です。技術的には、複数の Zend_Session_Namespace を使用する必要はありません。

次に、次のページの読み込み時にメッセージをクリアするには、次のようにします。

// 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 に答える