开发者

cakephp passing data to view after redirect

Is it possible to display data that I pass with $this->set() when there is a redirect to another page a开发者_JAVA技巧fter the data setting?

Thanks, EL


Probably the easiest way is to store the data in the session with:

$this->Session->write('key', 'value');

and to read it later with:

$this->Session->read('key');


You can use the session as mentioned above but if you don't want to save the data in a session you could use the dispatcher object, here is an example. I find it useful..

    $login['Login']['username'] = $username;
    $login['Login']['password'] = $password;

    $this->autoRender = false; 
    $d = new Dispatcher(); 
    $d->dispatch( 
        array("controller" => "users", "action" => "login"), 
        array("data" => $login) 
    );                  


I know it has been looong time but I also had this problem and this is how I solve it. As for cakePHP 2.3.5 the solution provided by Introgy will not work since the definition for controller->dispatch is

public function dispatch(CakeRequest $request, 
                        CakeResponse $response, $additionalParams = array())

instead you can use

$this->requestAction

As the explanation in code said

/**
 * Calls a controller's method from any location. Can be used to connect
 *controllers together
 * or tie plugins into a main application. requestAction can be used to
 *return rendered views
 * or fetch the return value from controller actions.
 *
 * Under the hood this method uses Router::reverse() to convert the $url
 *parameter into a string
 * URL. You should use URL formats that are compatible with
 *Router::reverse()
 *
 * #### Passing POST and GET data
 *
 * POST and GET data can be simulated in requestAction. Use 
 *`$extra['url']` for
 * GET data. The `$extra['data']` parameter allows POST data simulation.
 *
 * @param string|array $url String or array-based URL. Unlike other URL
 *arrays in CakePHP, this
 *    URL will not automatically handle passed and named arguments in the
 *$url parameter.
 * @param array $extra if array includes the key "return" it sets
 *theAutoRender to true. Can
 *    also be used to submit GET/POST data, and named/passed arguments.
 * @return mixed Boolean true or false on success/failure, or contents
 *    of rendered action if 'return' is set in $extra.
 */

So Introgy example would be modified as:

$login['Login']['username'] = $username;
$login['Login']['password'] = $password;
$url = array('plugin'     => 'plug_in_if_there_is', 
             'controller' =>'your_target_controllers', 
             'action'     =>'actionOnThatController');

$this->requestAction($url, array('data' => $login));  

Your data will be available in the target's data:

class YourTargetControllerController extends PlugInIfThereIsAppController
{
  public function actionOnThatController()
  {
    $this->data; //will be having ['Login']['username'] = $username
                 //                        ['password'] = $password  
  }
}

and the view for actionOnThatController will be rendered.

EDIT: I forgot to add this, for the target view to be rendered it is necessary to add the key 'return' in the array passed as $extra, then you have to render the targeted action's view, therefor the complete correct modification would be

$login['Login']['username'] = $username;
$login['Login']['password'] = $password;
$url = array('plugin'     => 'plug_in_if_there_is', 
             'controller' =>'your_target_controllers', 
             'action'     =>'actionOnThatController');

$this->requestAction($url, array('return', 'data' => $login));
$this->render('PlugInIfThereIs.YourTargetControllers/action_on_that_controller');


You redirect with header('Location: ...')? This makes the browser start a new request. The script that triggered the redirection may be the same as the one handling the new request, but there are now two instances running (or the first may even have quit) and each doesn't have access to the variables of the other. Either you store the data anywhere (session, shared memory, ...) or you rebuild it the same way you did in the first request or you just don't issue a second request but redirect to another action/view internally.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜