Slim 3 - 无法从ajax post请求重定向到带有GET参数的路由

时间:2017-01-29 09:05:09

标签: php jquery ajax slim-3

我正在使用ajax post请求使用Slim 3将数据发送到我的数据库。所有数据都会被发布并正确插入我的数据库,但它不会重定向到GET路径。

Ajax Post请求代码

    jQuery(function() { 
        _accent.click(function() {
            fpd.getProductDataURL(function(dataURL) {
                var sku = Math.floor((Math.random() * 10000000000) + 1);
                $.ajax({
                    type: "POST",
                    url: "{{ path_for('product.createProductAccent', {sku: product.sku}) }}",
                    data: { 
                        sku: sku,
                        img: dataURL
                    }
                });
            });
        });
    });

这些是我的路线

$app->post('/golf-bags/accent/{sku}', ['Base\Controllers\ProductController', 'createProductAccent'])->setName('product.createProductAccent');
$app->get('/golf-bags/accent/{sku}/{hash}', ['Base\Controllers\ProductController', 'getProductAccent'])->setName('product.getProductAccent');

这是我的ProductController POST和GET函数

public function createProductAccent($sku, Request $request, Response $response) {
    $product = Product::where('sku', $sku)->first();
    $hash = bin2hex(random_bytes(32));

    $uploads = Upload::where('sku', $sku)->first();

    $path = __DIR__ . '/../../public/assets/uploads/';

    $img = $request->getParam('img');
    $img = str_replace('data:image/png;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $data = base64_decode($img);
    $file = mt_rand() . '.png';
    file_put_contents($path . $file, $data);

    $sku = $request->getParam('sku');

    $uploads = Upload::create([
        'sku' => $sku,
        'hash' => $hash,
        'accent_colour' => $file
    ]);
    /****  
    ALL THE CODE RUNS UPTO HERE AND INSERTS INTO DB  
    BUT WILL NOT REDIRECT WITH THE RESPONSE BELOW
    ****/
    return $response->withRedirect($this->router->pathFor('product.getProductAccent', [
        'sku' => $sku,
        'hash' => $hash
    ]));
}

public function getProductAccent($sku, $hash, Request $request, Response $response) {
    $product = Product::where('sku', $sku)->first();
    $design = Design::where('sku', $sku)->first();
    $uploads = Upload::where('hash', $hash)->first();
    $colours = Colour::all();

    $array = [];

    foreach($colours as $colour) {
        $array[] = $colour->hex_colour_bg;
    }

    return $this->view->render($response, 'product/product-design-accent.php', [
        'product' => $product,
        'design' => $design,
        'uploads' => $uploads,
        'colours' => json_encode($array)
    ]);
}

不确定我在这里有什么问题,但它不会重定向到GET功能。

1 个答案:

答案 0 :(得分:0)

public function getProductAccent($sku, $hash, Request $request, Response $response) {

在slim 3中,第3个参数是args - 数组,其中包含路径变量,因此您实际需要这样做:

public function getProductAccent(Request $request, Response $response, $args) {
    $sku = $args['sku'];
    $hash = $args['hash'];

在你创建方法时相同

public function createProductAccent($sku, Request $request, Response $response) {

public function createProductAccent(Request $request, Response $response, $args) {
    $sku = $args['sku'];