鍍金池/ 問答/PHP  網(wǎng)絡(luò)安全/ slim框架如何自定義 請(qǐng)求方式錯(cuò)誤

slim框架如何自定義 請(qǐng)求方式錯(cuò)誤

比如規(guī)定了POST方式請(qǐng)求接口,但是GET來請(qǐng)求會(huì)報(bào)如下圖錯(cuò)誤
圖片描述

那么如何自定義返回的錯(cuò)誤呢?比如返回:
{"error":"Method not allowed"}

回答
編輯回答
墨沫
// 重寫默認(rèn)的notAllowedHandler
$container['notAllowedHandler'] = function ()
{
    return function ($req, $res, $allowMethod) {
        return $res->withJson([
            "status" => 0,
            "info" => "請(qǐng)求方法錯(cuò)誤",
            "data" => $allowMethod
        ]);
    };
};
2018年4月22日 00:50