CakePHP在用JavaScript打开新窗口后没有重定向


CakePHP not redirecting after opening new window with JavaScript

在我的一项操作中,我通过javascript窗口将商品添加到外部网站上的购物车中。添加后,我重定向回主页,但是CakePHP没有重定向。正在将项目正确添加到购物车中。

//OrdersController
function place_filled_orders($id = null){
    $this->layout = false;
    $this->autoRender = false;
    ?>
        <script>
            cart_window = window.open("http://www.example.com/load_cart_with_stuff");
            cart_window.close();
        </script>
    <?
        $this->redirect(array('controller' => 'orders', 'action' => 'home'));
}

当我点击与此操作对应的链接时,它只停留在/orders/place_filled_orders上,而不是重定向到/orders/home

您不能以这种方式在控制器中添加脚本。这完全违反MVC规则,因此你应该避免它。你应该添加一个同时执行这两个操作的视图(或元素):

因此,添加一个app/View/Orders/place_filled_order.ctp文件,其中包含以下内容:

<?php
echo $this->Html->scriptBlock('
    cart_window = window.open("http://www.example.com/load_cart_with_stuff");
    cart_window.close();
    window.location.href = "' . $this->webroot . '/orders/home";
');

编辑

乍一看,实际上您正在寻找requestAction方法。所以你会让你的控制器看起来像:

function place_filled_orders($id = null) {
    $this->autoRender = false;
    $this->requestAction('/load_cart_with_stuff');
    $this->redirect(array('controller' => 'orders', 'action' => 'home'));
}