Zend:使用参数重定向到Action

时间:2022-05-22 11:13:03

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&umail=username@example.com

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

怎么做?示例代码请。谢谢

5 个解决方案

#1


27  

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/robin@example.com');

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

注意:从Zend Framework 1.7开始,不推荐使用_redirect()方法。请改用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()接受第二个可选参数,如果未找到该参数,则该参数将设置为变量。

#2


50  

you can try with redirector:

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

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

#3


6  

You may want to try this:

你可能想试试这个:

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

#4


4  

You can also add $params say like for userid

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

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

#5


0  

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 
?>

#1


27  

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/robin@example.com');

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

注意:从Zend Framework 1.7开始,不推荐使用_redirect()方法。请改用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()接受第二个可选参数,如果未找到该参数,则该参数将设置为变量。

#2


50  

you can try with redirector:

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

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

#3


6  

You may want to try this:

你可能想试试这个:

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

#4


4  

You can also add $params say like for userid

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

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

#5


0  

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&nbsp;' . $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 
?>