Skip to content

Instantly share code, notes, and snippets.

@pebriana
Forked from sourcec0de/ApiController.php
Last active August 29, 2015 14:23
Show Gist options
  • Save pebriana/f2dac5003b2a0b675551 to your computer and use it in GitHub Desktop.
Save pebriana/f2dac5003b2a0b675551 to your computer and use it in GitHub Desktop.
// add this to your API controller in Yii
public function actionPreflight() {
$content_type = 'application/json';
$status = 200;
// set the status
$status_header = 'HTTP/1.1 ' . $status . ' ' . $this->_getStatusCodeMessage($status);
header($status_header);
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
header("Access-Control-Allow-Headers: Authorization");
header('Content-type: ' . $content_type);
}
// Where ever the function "_sendResponse" is located
// add these lines, this will allow CORS
// Allows from any origin
// Allows a header called Authorization
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Authorization");
// REST CORS pattern
// A preflight request is basically an OPTIONS request to ask for permission to use cross-domain features.
// So we have add the proper verb in the url manager rules (config/main.php):
array('api/preflight', 'pattern'=>'api/*', 'verb'=>'OPTIONS'),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment