API使用GET方法但不使用yii2中的POST方法

时间:2015-12-04 06:20:36

标签: php yii2

在这里,我使用POST方法编写API for register注册表,使用postman检查API,但如果我使用GET方法insted它工作,它不能用POST方法吗?

 public function actionRegister() {
        $response = [];

        if (isset($_POST['practiceCode']) &&
            isset($_POST['officeID']) &&
            isset($_POST['token']) &&
            isset($_POST['workingDays']) &&
            isset($_POST['morningStart']) &&
            isset($_POST['morningEnd']) &&
            isset($_POST['afternoonStart']) &&
            isset($_POST['afternoonEnd']) &&
            isset($_POST['eveningStart']) &&
            isset($_POST['eveningEnd'])) {

            $practiceModel = Practice::find()->where(['practiceCode' => $_POST['practiceCode'], 'deleted' => 'N'])->one();
            if ($practiceModel != null) {
                $officeModel = Office::find()->where(['practiceCode' => $_POST['practiceCode'], 'deleted' => 'N'])->all();

                if ($officeModel != null) {
                    $arrayOffice = Office::find()->where(['id' => $_POST['officeID'], 'deleted' => 'N'])->one();

                    if($arrayOffice != null) {
                        $scheduleModel = new Schedule();
                        $scheduleModel->officeID = $_POST['officeID'];
                        $scheduleModel->token = $_POST['token'];
                        $scheduleModel->workingDays = $_POST['workingDays'];
                        $scheduleModel->morningStart = $_POST['morningStart'];
                        $scheduleModel->morningEnd = $_POST['morningEnd'];
                        $scheduleModel->afternoonStart = $_POST['afternoonEnd'];
                        $scheduleModel->eveningStart = $_POST['eveningStart'];
                        $scheduleModel->eveningEnd = $_POST['eveningEnd'];
                       // $scheduleModel->practiceCode = $_POST['practiceCode'];
                        if ($scheduleModel->save()) {
                            $response = ['code' => '200',
                                'result' => 'Success',
                                'message' => 'Schedule created successfully',
                                'details' => $scheduleModel->toArray(),
                            ];
                        } else {
                            $response = ['code' => '400',
                                'result' => 'Failure',
                                'message' => 'Could not Schedule you at this moment'];
                        }
                    } else {
                        $response = ['code' => '400',
                            'result' => 'Failure',
                            'message' => 'Specified Office does not exist.',
                        ];
                    }
                } else {
                    $response = ['code' => '400',
                        'result' => 'Failure',
                        'message' => 'Specified practice code Office does not exist.',
                    ];
                }
            } else {
                $response = ['code' => '400',
                    'result' => 'Failure',
                    'message' => 'Specified practice code does not exist. ',
                ];
            }
    } else {
            $response = ['code' => '400',
                'result' => 'Failure',
                'message' => 'Invalid request/missing parameters',
            ];
        }

        \Yii::$app->response->format = 'json';
        return $response;
    }

2 个答案:

答案 0 :(得分:4)

出于安全考虑,yii 2.0不允许第三方POST请求。例如,在你的情况下邮递员。要启用POST请求,请在控制器中添加:

this->$enableCsrfValidation = false;

您可以查看stackoverflow网站,有很多与此相关的主题。查看:
Disable CSRF validation for individual actions in Yii2

进一步阅读退房:
 Yii 2.0 reference

答案 1 :(得分:0)

app/config/web.php

您需要添加 'application/json' => 'yii\web\JsonParser' 进入请求数组。

enter image description here

相关问题