cakephp重定向后传递数据进行查看

时间:2010-12-13 10:21:00

标签: php cakephp redirect cakephp-1.3

在数据设置后重定向到另一个页面时,是否可以显示我通过$this->set()传递的数据?

谢谢, EL

4 个答案:

答案 0 :(得分:5)

可能最简单的方法是将数据存储在会话中:

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

稍后阅读:

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

答案 1 :(得分:4)

您可以使用上面提到的会话,但如果您不想在会话中保存数据,则可以使用调度程序对象,这是一个示例。我发现它很有用..

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

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

答案 2 :(得分:1)

我知道它已经过了很长时间,但我也有这个问题,这就是我解决它的方法。 至于cakePHP 2.3.5,由于控制器 - >发送的定义是

,因此Introgy提供的解决方案将无效。
public function dispatch(CakeRequest $request, 
                        CakeResponse $response, $additionalParams = array())

而你可以使用

$this->requestAction

正如代码中的解释所说

/**
 * 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.
 */

因此,Introgy示例将被修改为:

$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));  

您的数据将在目标数据中提供:

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

将呈现 actionOnThatController 的视图。

编辑: 我忘了添加这个,为了呈现目标视图,有必要在作为 $ extra 传递的数组中添加键'return',然后你必须渲染目标动作的视图,因此完全正确的修改将是

$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');

答案 3 :(得分:0)

您使用header('Location: ...')重定向?这使浏览器启动新请求。触发重定向的脚本可能与处理新请求的脚本相同,但现在有两个实例在运行(或者第一个实例可能已退出),并且每个实例都无法访问另一个实例的变量。您可以将数据存储在任何位置(会话,共享内存,...),也可以像在第一个请求中那样重建数据,或者只是不发出第二个请求,而是在内部重定向到另一个操作/视图。