ozee31 / cakephp-cors

A CakePHP (3.3+) plugin for activate cors domain in your application
MIT License
43 stars 31 forks source link

New Cake 3.4 [BUG] #11

Closed lpj145 closed 7 years ago

lpj145 commented 7 years ago

new cake 3.4 Cors not work, on normal execution, Allow-Control-Access-Origin not set yet.

munvier commented 7 years ago

I've got a similar trouble with CakePHP 3.4.

First of all, i've had to create the Application.php (see https://book.cakephp.org/3.0/en/controllers/middleware.html#using-middleware and https://book.cakephp.org/3.0/en/controllers/middleware.html#adding-http-stack).

But then, i've got new errors i.e. "->is() is not longer an available method" and has to be checked with getMethod() === "OPTIONS"...

rgr commented 7 years ago

Could you share what you wrote in your application.php and index.php ? I seem to have the same problem...

xpacman commented 7 years ago

I've run into same problem. It is still not solved?

orbitTVO commented 7 years ago

I'm suffering from the same problem:

`2017-08-07 11:13:59 Error: [Error] Call to undefined method Zend\Diactoros\ServerRequest::is() Stack Trace:

0 /path/vendor/cakephp/cakephp/src/Http/Runner.php(65): Cors\Routing\Middleware\CorsMiddleware->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response), Object(Cake\Http\Runner))`

The problem is this: (from ozee31\cakephp-cors\Routing\Middleware\CorsMiddleware.php)

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next) {
        if ($request->getHeader('Origin')) {
            $response = $response
                ->withHeader('Access-Control-Allow-Origin', $this->_allowOrigin($request))
                ->withHeader('Access-Control-Allow-Credentials', $this->_allowCredentials())
                ->withHeader('Access-Control-Max-Age', $this->_maxAge());
            if ($request->is('options')) {
                $response = $response
                    ->withHeader('Access-Control-Allow-Headers', $this->_allowHeaders($request))
                    ->withHeader('Access-Control-Allow-Methods', $this->_allowMethods())
                    ->withHeader('Access-Control-Expose-Headers', $this->_exposeHeaders());

                return $response;
            }
        }

        return $next($request, $response);
    }

The $request->is('options') tries to call a method #is() on what is thought to be an instance of Zend\Diactoros\ServerRequest. I'm still running CakePHP 3.3(.16), so I don't think this issue was only introduced on CakePHP 3.4.

Does anyone know a solution to this?

EDIT: I have worked around my problem ("#is() is not a valid method for the request") by reverting this plugin from v1.1.0 to v1.0.2.

ozee31 commented 7 years ago

It's fixed

Thank you @munvier