我正在使用zend框架。我使用以下语句重定向到另一个操作。
$this->_helper->redirector('some_action');
上述语句完美运行,并调用'some_action'。现在我想将一些参数传递给'some_action'。
some_action?uname=username&umail=username@example.com
如何在被调用的动作中获取参数。通常我们会这样:
$userName = $_REQUEST['uname'];
$usermail = $_REQUEST['umail'];
如何执行此操作?示例代码请。感谢
答案 0 :(得分:50)
您可以尝试使用重定向器:
$params = array('user' => $user, 'mail' => $mail);
$this->_helper->redirector($action, $controller, $module, $params);
答案 1 :(得分:29)
使用Zend_Controller_Action::redirect()
方法(刚刚通过Sarfraz的辅助方法)
$this->redirect('/module/controller/action/username/robin/email/robin@example.com');
注意:从Zend Framework 1.7开始,不推荐使用_redirect()
方法。请改用redirect()
。
然后在被召唤的行动中:
$username = $this->_getParam('username');
$email = $this->_getParam('email');
_getParam()接受第二个可选参数,如果找不到参数,该参数将设置为变量。
答案 2 :(得分:6)
你可能想试试这个:
$this->_redirector->gotoUrl('/my-controller/my-action/param1/test/param2/test2');
答案 3 :(得分: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
答案 4 :(得分:1)
你如何得到param sorta取决于你的位置,
您不必捕获请求$ 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
?>