CorsSlim and slim 3

hello every body, i can’t do cross domain request but i’ve installed corsslim .

Here my composer file :

 {
    "require": {
        "slim/slim": "^3.0",
        "palanik/corsslim": "dev-slim3"
    }
  } 

My code for use:

$api = new \Slim\App();
$cors = new \CorsSlim\CorsSlim();
$api->add($cors);
$api->get('/','HomeFct');
$api->get('/news','GetNewsFct');
$api->get('/news/{id}','GetNewsDetail');
$api->get('/histoire','GetMenuHistory');
$api->get('/histoire/{page}','getPageHistoire');
$api->get('/events','GetEventsFct');
$api->get('/phpinfo','GetPhpInfo');
$api->get('/authentification/{identifiant}/{motdepasse}','AuthFct');
$api->run();

Have an idea?

Are the correct headers being set by your application?

Also, I thought CORS required that you respond to an OPTIONS request?

I have no idea what CorsSlim does, but when I worked with frontend frameworks that required CORS it was easy enough to setup a middleware to deal with OPTIONS request. Here is some code below, not tested but should work.

<?php

$app->add(function ($request, $response, $next) {
    $response['Access-Control-Allow-Origin'] = 'http://yourdomainhere';
    $response['Access-Control-Allow-Methods'] = 'GET, OPTIONS';
    $response['Access-Control-Allow-Headers'] = 'X-PINGARUNER';
    $response['Access-Control-Max-Age'] = 1728000;

    $response['Content-Type'] = 'application/json';
    
    $response = $next($request, $response);
    
    return $response;
});

@silentworks I don’t think you can use $response as an array.

What do you mean?
Can show me what you mean by headers being set by your application

Thank

Haha this is what happen when copying code from old Slim 2 project to Slim 3 code without thinking.

I came up with something like this.

> $newResponse = $response->withHeader('Access-Control-Allow-Origin', '*')
>                         ->withHeader('Access-Control-Allow-Headers', array('Content-Type', 'X-Requested-With', 'Authorization'))
>                         ->withHeader('Access-Control-Allow-Methods', array('GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'));
> if($request->isOptions()) {
>     return $newResponse;
> }
> return $next($request, $newResponse);