Zend: Redirect to Action with parameters

前端 未结 5 1074
我寻月下人不归
我寻月下人不归 2020-12-13 18:19

I am using zend framework. I am using the following statement to redirect to another action.

$this->_helper->redirector(\'some_action\');
5条回答
  •  轮回少年
    2020-12-13 19:03

    How you get the param sorta depends on where you are,

    You do not have to catch a request $param to achieve what you want to do here. You are just using the FlashMessenger helper to add a message to the stack. You then retrieve the message within the action where you want to show the message, then assign it to the view as I do in the successAction. Keep in mind that you can pass any $var or array of data by assigning it in the controller like: $this->view->var = $var; Within the view that will then be accessed as $this->var.

    Since you asked about login I will show you how I usually do it. Not that its the best way.

    My LoginController's index view holds the form:

        public function indexAction() {
        $form = new Zfcms_Form_Login;
        $this->view->form = $form;
         /*check for valid input
           authenticate using adapter
           persist user record to session
           redirect to original request URL if present*/
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
    
                $authAdapter = $this->getAuthAdapter();
    
                # get the username and password from the form
                $username = $values['username'];
                $password = $values['password'];
    
                # pass to the adapter the submitted username and password
                $authAdapter->setIdentity($username)
                        ->setCredential($password);
    
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);
    
                if ($result->isValid()) {
    
                    # all info about this user from the login table
                    # ommit only the password, we don't need that
                    $userInfo = $authAdapter->getResultRowObject(null, 'password');
    
                    # the default storage is a session with namespace Zend_Auth
                    $authStorage = $auth->getStorage();
                    $authStorage->write($userInfo);
    
    
                    $session = new Zend_Session_Namespace('zfcms.auth');
                    if (isset($session->requestURL)) {
                        $url = $session->requestURL;
                        unset($session->requestURL);
                        $this->_redirect($url);
                    } else {
                        $this->_helper->getHelper('FlashMessenger')
                                ->addMessage('You were successfully logged in as ' . $userInfo->username);
                        $this->_redirect('/login/success');
                    }
                } else {
                    $this->view->message = 'You could not be logged in. Please try again.';
                }
            }
        }
    }
    

    In the success action we do this:

    public function successAction() {
        if ($this->_helper->getHelper('FlashMessenger')->getMessages()) {
            $this->view->messages = $this->_helper
                            ->getHelper('FlashMessenger')
                            ->getMessages();
        } else {
            $this->_redirect('/login/success');
        }
    }
    

    In the view script we can do something like what I have below. The reason I do it this way is that sometimes I will pass only a single message in a controller, in this case I simply use:

    $this->view->message = 'message goes here';
    

    Then catch them both if they are set in the view:

    message) || isset($this->messages)):
    ?>
    
    messages))
    {
        echo implode($this->messages);
    } else {
        echo $this->message;
    }?>
    
    
    

提交回复
热议问题