开发者

Zend: Redirect to Action with parameters

开发者 https://www.devze.com 2022-12-13 23:44 出处:网络
I am using zend framework. I am using the following statement to redirect to another action. $this->_helper->redirector(\'some_action\');

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

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

Above statement is w开发者_如何学运维orking perfectly and 'some_action' is called. Now I want to pass some parameters to 'some_action' like this.

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


you can try with redirector:

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


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

$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.

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.


You may want to try this:

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


You can also add $params say 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:

/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


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:

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

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

<?php 
endif 
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消