cakephp 在重定向后将数据传递给视图

发布于 2024-10-07 19:05:48 字数 84 浏览 1 评论 0原文

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

谢谢, EL

Is it possible to display data that I pass with $this->set() when there is a redirect to another page after the data setting?

Thanks,
EL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愛上了 2024-10-14 19:05:48

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

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

并稍后使用:

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

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');
心如狂蝶 2024-10-14 19:05:48

您可以如上所述使用会话,但如果您不想将数据保存在会话中,您可以使用调度程序对象,这是一个示例。我觉得很有用..

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

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

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) 
    );                  
み青杉依旧 2024-10-14 19:05:48

我知道已经很长时间了,但我也遇到了这个问题,这就是我解决它的方法。
至于cakePHP 2.3.5,Introgy提供的解决方案将不起作用,因为controller->dispatch的定义是

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

您的数据将在目标的data:

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

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');
べ映画 2024-10-14 19:05:48

您使用 header('Location: ...') 进行重定向?这使得浏览器开始一个新的请求。触发重定向的脚本可能与处理新请求的脚本相同,但现在有两个实例正在运行(或者第一个甚至可能已退出),并且每个实例都无法访问另一个实例的变量。要么将数据存储在任何地方(会话、共享内存等),要么按照第一个请求中的方式重建数据,或者只是不发出第二个请求,而是在内部重定向到另一个操作/视图。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文