Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route Middleware not work #8

Open
shotoreaper opened this issue May 4, 2015 · 3 comments
Open

Route Middleware not work #8

shotoreaper opened this issue May 4, 2015 · 3 comments
Assignees

Comments

@shotoreaper
Copy link

Hi!

This is my code, but not work:

require ('.././libs/CorsSlim/vendor/autoload.php');
$app = new \Slim\Slim();

$app->post('/item', 
          \CorsSlim\CorsSlim::routeMiddleware(), 
          function () use ($app) {
            $r = json_decode($app->request->getBody());
            $response["status"] = "success";
            $response["message"] = $r->data->nickname;
            echoResponse(200, $response);
          }
        );

The error:

XMLHttpRequest cannot load [...]. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

If I use this code, it works. But I need apply only in one route:

require ('.././libs/CorsSlim/vendor/autoload.php');
$app = new \Slim\Slim();

$corsOptions = array(
    "origin" => "*",
    "exposeHeaders" => array("X-My-Custom-Header", "X-Another-Custom-Header"),
    "maxAge" => 1728000,
    "allowCredentials" => True,
    "allowMethods" => array("POST, GET"),
    "allowHeaders" => array("Origin, X-Requested-With, Content-Type, Accept")
    );

$cors = new \CorsSlim\CorsSlim($corsOptions);
$app->post('/item',
          function () use ($app) {
            $r = json_decode($app->request->getBody());
            $response["status"] = "success";
            $response["message"] = $r->data->nickname;
            echoResponse(200, $response);
          }
        );

$app->add($cors);
@palanik palanik self-assigned this May 6, 2015
@palanik
Copy link
Owner

palanik commented May 6, 2015

Your code looks ok and I am able to run it locally with correct cors response.

Could you add $corsOptions to routeMiddleware() and try.

\CorsSlim\CorsSlim::routeMiddleware($corsOptions)

@shotoreaper
Copy link
Author

Nothing, I get the same error:
XMLHttpRequest cannot load [...]. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

The code:


$corsOptions = array(
    "origin" => "*",
    "exposeHeaders" => array("X-My-Custom-Header", "X-Another-Custom-Header"),
    "maxAge" => 1728000,
    "allowCredentials" => True,
    "allowMethods" => array("POST, GET"),
    "allowHeaders" => array("Origin, X-Requested-With, Content-Type, Accept, Authorization")
    );

$app->post('/item',
          \CorsSlim\CorsSlim::routeMiddleware($corsOptions),
          function () use ($app) {
            /*$r = json_decode($app->request->getBody());
            $response["status"] = "success";
            $response["message"] = "motherfucker";
            echoResponse(200, $response);*/
          }
        );

@palanik
Copy link
Owner

palanik commented May 13, 2015

The issue is with pre-flight call.

With routeMiddleware you have to explicitly support OPTIONS calls for Preflighted requests, like this:

$app->options('/item',
              \CorsSlim\CorsSlim::routeMiddleware(),
              function () use ($app) {}
              );

No need to return any response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants