Zend:带参数重定向到Action

32

我正在使用Zend框架。 我正在使用以下语句重定向到另一个操作。

$this->_helper->redirector('some_action');

上述语句完美地运行了,'some_action'被调用。现在我想像这样传递一些参数给'some_action'。

some_action?uname=username&umail=username@example.com

如何获取被调用操作中的参数。通常我们会这样做:

$userName = $_REQUEST['uname'];
$usermail = $_REQUEST['umail']; 

怎样实现这个功能?请提供示例代码。谢谢。

5个回答

50

你可以尝试使用重定向器:

$params = array('user' => $user, 'mail' => $mail);
$this->_helper->redirector($action, $controller, $module, $params);

2
当你到达那里时,如何检索 $params? - Andrei Cristian Prodan

30

使用 Zend_Controller_Action::redirect() 方法(该方法只是传递到 Sarfraz 的助手方法)

$this->redirect('/module/controller/action/username/robin/email/robin@example.com');

注意:_redirect()方法已经在Zend Framework 1.7中被弃用,应使用redirect()替代。

然后在调用的操作中:

$username = $this->_getParam('username');
$email = $this->_getParam('email');

_getParam()函数包含第二个可选参数,如果未找到该参数,则将其设置为默认变量。


7
您可以尝试以下方法:
  $this->_redirector->gotoUrl('/my-controller/my-action/param1/test/param2/test2');

4

您还可以添加$params,例如userid

public function indexAction ()
{
    $auth = Zend_Auth::getInstance();
    if ($auth->hasIdentity()) {
        // Identity exists; get it
        $identity = $auth->getIdentity();
        $this->_redirect('/user/profile/' . $identity->user_id);
    } else {
        $this->_redirect('/user');
    }
}

我忘了提到,当然前提是您已经设置好路由来接受$param。举个例子,对于上面示例中重定向到的页面,路由应该类似于:

/application/configs/application.ini

resources.router.routes.user-profile.route = /user/profile/:id
resources.router.routes.user-profile.defaults.module = default
resources.router.routes.user-profile.defaults.controller = user
resources.router.routes.user-profile.defaults.action = profile


1

如何获取参数取决于您所在的位置,

您不必捕获请求$param来实现您想要做的事情。您只需使用FlashMessenger助手将消息添加到堆栈中。然后在您想要显示消息的操作中检索消息,然后像我在successAction中那样将其分配给视图。请记住,您可以通过在控制器中分配任何$var或数据数组来传递它,例如:$this->view->var = $var; 在视图中,它将被访问为$this->var。

由于您询问了登录问题,我将向您展示我通常的做法。并不是最好的方法。

我的LoginController的索引视图包含表单:

    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.';
            }
        }
    }
}

在成功的操作中,我们这样做:
public function successAction() {
    if ($this->_helper->getHelper('FlashMessenger')->getMessages()) {
        $this->view->messages = $this->_helper
                        ->getHelper('FlashMessenger')
                        ->getMessages();
    } else {
        $this->_redirect('/login/success');
    }
}

在视图脚本中,我们可以像下面这样做一些事情。我这样做的原因是有时候在控制器中只会传递一个单独的消息,在这种情况下,我简单地使用:
$this->view->message = 'message goes here';

如果在视图中都设置了它们,那么就同时捕获它们:

<?php 
    if(isset($this->message) || isset($this->messages)):
?>

<?php
if(is_array($this->messages))
{
    echo implode($this->messages);
} else {
    echo $this->message;
}?>

<?php 
endif 
?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接