Slim icon indicating copy to clipboard operation
Slim copied to clipboard

Cors not working

Open anytrack-software opened this issue 8 years ago • 4 comments

I have tried to follow the documentation but CORS https://www.slimframework.com/docs/v3/cookbook/enable-cors.html does not work, I have already used the Cors Middleware Tuupola and it does not send any header data, it is what determinesRouteBeforeAppMiddleware is enabled. Does anyone have any idea what might be wrong?

anytrack-software avatar Nov 24 '18 13:11 anytrack-software

@douglasnoronha It worked for me perfectly. Could you share your response? My CORS settings looks like this : $app->add(function ($req, $res, $next) { $response = $next($req, $res); return $response ->withHeader('Access-Control-Allow-Origin', 'https://website.com') ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization') ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS'); });

malithmcr avatar Dec 20 '18 21:12 malithmcr

@douglasnoronha It would be better to handle CORS in .htaccess

ovimughal avatar Dec 25 '18 06:12 ovimughal

This is what my code looks like that handles CORS. Note: that I've subclassed Slim and $this really means $app Important: the CORS middleware is wrapped in a try...catch(\Throwable) that only works in PHP 7+ and is an easy way of overriding all Slim error handling into your custom error handlers. You should be able to remove the try...catch code. Also I do not allow DELETE so this is missing from the Access-Control-Allow-Methods header. Two more important points: the middleware add() for options allowing all requests and the CORS middleware must be the last code executed. Middleware works by LIFE (Last In First Executed) so it is essential to handle the CORS crazyness as the last add() in your code.

Hope this helps:

// Accept all routes for options
$this->options('/{routes:.+}', function (Request $request, Response $response, $args) {
    return $response;
});

// Override the CORS stupidity and Slim's exception handlers via Middleware LIFE
$this->add(function(Request $request, Response $response, callable $next): ResponseInterface
{
    try {
        /** @var Response $response */
        $response = $next($request, $response);

        return $response
            ->withHeader('Access-Control-Allow-Origin', '*')
            ->withHeader('Access-Control-Allow-Credentials', true)
            ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
            ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, OPTIONS')
            ->withHeader('Content-Type', 'application/json');
    } catch (\Throwable $throwable) {
        $handleErrors = new HandleRuntimeError();
        return $handleErrors($request, $response, $throwable);
    }
});

RyanNerd avatar Jan 27 '19 10:01 RyanNerd

This is 3 months old, closing due to no activity from @douglasnoronha. The code in the docs work, please join us via our Slack Channel if you need further assistance.

l0gicgate avatar Feb 18 '19 03:02 l0gicgate