php Zend:使用参数重定向到操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1913509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:17:38  来源:igfitidea点击:

Zend: Redirect to Action with parameters

phpzend-frameworkredirect

提问by Naveed

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

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

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

Above statement is working perfectly and 'some_action' is called. Now I want to pass some parameters to 'some_action' like this.

上面的语句运行良好,并且调用了 'some_action'。现在我想像这样将一些参数传递给“some_action”。

some_action?uname=username&[email protected]

And how to get parameters in called action. Usually we do like this:

以及如何在被调用的动作中获取参数。通常我们这样做:

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

How to perform this? Example code please. Thanks

如何执行此操作?请示例代码。谢谢

采纳答案by Robin M. Canaday

Use the Zend_Controller_Action::redirect()method (which just passes through to Sarfraz's helper method)

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

$this->redirect('/module/controller/action/username/robin/email/[email protected]');

Note: _redirect()method is deprecated as of Zend Framework 1.7. Use redirect()instead.

注意:_redirect()从 Zend Framework 1.7 开始不推荐使用方法。使用redirect()来代替。

And then in the called action:

然后在调用的操作中:

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

_getParam() takes a second optional argument which is set to the variable as a default if the parameter isn't found.

_getParam() 接受第二个可选参数,如果未找到该参数,该参数将设置为变量作为默认值。

回答by armandfp

you can try with redirector:

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

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

回答by Sarfraz

You may want to try this:

你可能想试试这个:

  $this->_redirector->gotoUrl('/my-controller/my-action/param1/test/param2/test2');

回答by Jsmith

You can also add $params say like for userid

您还可以添加 $params 说 like for 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');
    }
}

I forgot to mention, of course this is assuming you have the routing setup to accept a $param. As an example the routing would look something like this for the page that is being redirected to in the example above:

我忘了提,当然这是假设你有路由设置来接受 $param。例如,对于在上面的示例中重定向到的页面,路由看起来像这样:

/application/configs/application.ini

/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


回答by Jsmith

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

你如何获得 param sorta 取决于你在哪里,

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.

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

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

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:

如果在视图中设置了它们,则同时捕获它们:

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

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

<?php 
endif 
?>