1

I am new in PHP Slim Framwork. I am build an Rest API base on https://github.com/moritz-h/slim3-rest-skeleton and I have an error like this

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access "Slim"

And I fix it by using https://github.com/palanik/CorsSlim library with configure below

$settings = require __DIR__.'/settings.php';
$app = new \Slim\App($settings);
$corsOptions = array(
    "origin" => "*",
    "exposeHeaders" => array("Content-Type", "X-Requested-With", "X-authentication", "X-client"),
    "allowMethods" => array('GET', 'POST', 'PUT', 'DELETE', 'OPTIONS')
);
$cors = new \CorsSlim\CorsSlim($corsOptions);
$app->add($cors);
require __DIR__.'/dependencies.php';
require __DIR__.'/../app/routes.php';

It seem like I found the right library for fix it but the error still happened. Do I miss something with Slim? Any help or suggestion would be great aprreciated.

Linh
  • 57,942
  • 23
  • 262
  • 279
  • im not into slim php but as a personal experience using the generic way of handling CORS in php i found that "origin: *" do not work as expected so can tou try set origin to $_SERVER["HTTP_ORIGIN"] and see if that works – Strife86 Sep 19 '16 at 14:35
  • @Strife86 I have tried it but it say Undefined index: HTTP_ORIGIN – Linh Sep 19 '16 at 14:57
  • 1
    http://stackoverflow.com/questions/38005829/cors-post-request-fails/38035128#38035128 – geggleto Sep 19 '16 at 18:43
  • @geggleto I also tried your solution but it still not work, in your answer you 1/2 the solution, can you help me full solution. I'm stuck at this for a long time :((( please help – Linh Sep 20 '16 at 09:46
  • then try this "http://" . $_SERVER['HTTP_HOST'] – Strife86 Sep 20 '16 at 15:22
  • @PhanVanLinh the original question holder had the other 1/2 of the solution1 – geggleto Sep 20 '16 at 15:39

0 Answers0