2016-09-07 57 views
0

CakePHP 3を使用してREST APIを作成しています。公開するようにしていますので、誰でもAPIコールを操作することができます。 ここで定義されたように、私は、CORSヘッダを追加しました:http://book.cakephp.org/3.0/en/controllers/request-response.html#setting-cross-origin-request-headers-corsCakePHP 3 REST API + CORSリクエストとオプションメソッド

私がCORSヘッダを準備しDispatcher.beforeDispatchとDispatcher.beforeDispatch上でのEventListenerを実装しています。

class ApiResponseHeaders implements EventListenerInterface 
{ 

    /** 
    * Event bindings 
    * 
    * @return array 
    */ 
    public function implementedEvents() 
    { 
     return [ 
      'Dispatcher.beforeDispatch' => [ 
       'callable' => 'beforeDispatch', 
       'priority' => 0 
      ], 
      'Dispatcher.afterDispatch' => [ 
       'callable' => 'afterDispatch', 
       'priority' => 99999 
      ] 
     ]; 
    } 

    public function beforeDispatch(Event $event) 
    { 
     $request = $event->data['request']; 
     if ('OPTIONS' === $request->method()) { 
      $event->stopPropagation(); 
     } 
    } 

    public function afterDispatch(Event $event) 
    { 
     $request = $event->data['request']; 
     $response = $event->data['response']; 

     $response->cors($request) 
       ->allowOrigin('*') 
       ->allowMethods(['GET', 'POST', 'OPTIONS']) 
       ->allowHeaders(['Content-Type, Authorization, X-Requested-With, Accept']) 
       ->allowCredentials() 
       ->maxAge(0) 
       ->build(); 

     if ('OPTIONS' === $request->method()) { 
      $event->stopPropagation(); 
     } 
    } 

} 

しかし、問題は最初、それはOPTIONSメソッドを呼び出した後、POSTメソッドを作る、私はAngularJSからAPIエンドポイントにPOSTリクエストを作るとき、あります。 CakePHPはOPTIONSメソッドの呼び出しと戻りを処理しません:400不正な要求を以下

はサンプルリクエストとレスポンスヘッダです:

リクエストヘッダ

OPTIONS /v1/account/login HTTP/1.1 
Host: api.cake.dev 
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:48.0) Gecko/20100101 Firefox/48.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate 
Access-Control-Request-Method: POST 
Access-Control-Request-Headers: content-type 
Origin: http://demo.angular.dev 
Connection: keep-alive 

レスポンスヘッダ

HTTP/1.1 400 Bad Request 
Date: Wed, 07 Sep 2016 08:34:55 GMT 
Server: Apache/2.4.17 (Win64) PHP/5.6.16 
X-Powered-By: PHP/5.6.16 
X-DEBUGKIT-ID: b4e5654a-cefb-43b7-bf19-4e8c7ffdb0e0 
access-control-allow-origin: * 
access-control-allow-methods: GET, POST, OPTIONS 
access-control-allow-headers: Content-Type, Authorization, X-Requested-With, Accept 
Access-Control-Allow-Credentials: true 
access-control-max-age: 0 
Content-Length: 147 
Connection: close 
Content-Type: text/html; charset=UTF-8 

ですCakePHP 3でこのOPTIONS要求を処理し、正しい応答を返すための方法があります。次のPOST要求が正しく動作するか?

助けてください。 があなたのAppControllerの場所に次のようにあなた

答えて

0

ありがとう:

use Cake\Event\Event; //if you dont have this allready 

public function beforeFilter(event $event) { //if you dont have this beforeFilter already 
    if ($this->request->is('options')) { 
     return $this->response; 
    } 
} 
+0

本当にありがとうございましたが、実際にAppControllerに到達する前にそれを処理できませんか? –

+0

私は多分、わからない。しかし、これは私がそれを処理する方法です –

1

を、私はこの方法で行った:

public function beforeRender(event $event) { 
    $this->setCorsHeaders(); 
} 

public function beforeFilter(event $event) { 
    if ($this->request->is('options')) { 
     $this->setCorsHeaders(); 
     return $this->response; 
    } 
} 

private function setCorsHeaders() { 
    $this->response->cors($this->request) 
     ->allowOrigin(['*']) 
     ->allowMethods(['*']) 
     ->allowHeaders(['x-xsrf-token', 'Origin', 'Content-Type', 'X-Auth-Token']) 
     ->allowCredentials(['true']) 
     ->exposeHeaders(['Link']) 
     ->maxAge(300) 
     ->build(); 
} 

このようにして、私はCakePHPエラーページを扱うことさえできます。

ケースによっては、allowHeadersアレイの値を変更することができます。

withCredentialstrueに設定されている場合は、オリジンを指定する必要があります。

関連する問題