yii2 rest跨域

<?php
/**
 * Created by PhpStorm.
 * User: hetao
 * Date: 2018/1/13
 * Time: 20:44
 */

namespace frontend\controllers;

use yii\rest\ActiveController;
use yii\filters\Cors;

class CustomerController extends ActiveController
{
    public $modelClass = 'common\models\Customer';
    public function behaviors()
    {
        return [
            'corsFilter'=>[
                'class' => Cors::className(),
                'cors'=>['Access-Control-Allow-Credentials' => true,
                    'Origin' => ['*'],
                    'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
                    'Access-Control-Request-Headers' => ['*'],
                    'Access-Control-Allow-Credentials' => true,
                    'Access-Control-Max-Age' => 86400,
                    'Access-Control-Expose-Headers' => [],
                ]
            ]
        ];
    }
    protected function verbs()
    {
        return [
            'index' => ['OPTIONS','GET', 'HEAD'],
            'view' => ['OPTIONS','GET', 'HEAD'],
            'create' => ['OPTIONS','POST'],
            'update' => ['OPTIONS','PUT', 'PATCH'],
            'delete' => ['OPTIONS','DELETE'],
        ];
    }
}

相关推荐