0

I am using ajax post request to send data to my database using Slim 3. All the data gets posted and inserts into my database correctly but it will not redirect to the GET path.

Ajax Post request code

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

These are my Routes

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

This is my ProductController POST and GET functions

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

Not sure what i have wrong here, but it just will not redirect to the GET function.

John
  • 111
  • 10

1 Answers1

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

In slim 3 the 3rd argument is the args-array which contains the path variables so you actually need to do this:

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

In you create method the same

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

to

public function createProductAccent(Request $request, Response $response, $args) {
    $sku = $args['sku'];
jmattheis
  • 10,494
  • 11
  • 46
  • 58
  • If i insert manually in the browser the URL with the `sku` and the `hash` it retrieves the data, but it is not even getting to that point because it will not redirect in the first place. In my code above i have commented when the code runs to and will not run the redirect – John Jan 29 '17 at 10:38