超薄框架请求验证

时间:2015-08-02 02:16:26

标签: php slim

您好我正在使用slim框架,我有一个代码检查所有输入是否都为空,这里是代码:

{
"name": "wordpress-theme",
"version": "1.0.0",
"description": "WordPress theme",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-browser-sync": "^2.1.1",
"grunt-contrib-concat": "^0.5.1",
"grunt-contrib-copy": "^0.8.0",
"grunt-contrib-imagemin": "^0.9.4",
"grunt-contrib-uglify": "^0.9.1",
"grunt-contrib-watch": "^0.6.1",
"grunt-csscomb": "^3.0.0",
"grunt-newer": "^1.1.0",
"grunt-notify": "^0.4.1",
"grunt-sass": "^0.18.1",
"load-grunt-tasks": "^3.1.0",
"time-grunt": "^1.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/blainerobison/_s.git"
}
}

我想要做的不是在条件中对所有请求输入进行编码,我只想要一个变量来检查所有请求输入,如果不是空的话。有办法吗?像$request = \Slim\Slim::getInstance()->request()->post(); if($request['txt_1'] != "" AND $request['txt_2'] != "" AND $request['txt_3'] != "" AND $request['txt_4'] != "" AND $request['txt_5'] != "" AND $request['txt_6'] != "" AND $request['txt_7'] != "" AND $request['txt_8'] != "" AND $request['txt_21'] != "" AND $request['txt_22'] != "" AND $request['txt_23'] != "" AND $request['txt_24'] != "" AND $request['txt_31'] != "" AND $request['txt_32'] != "" AND $request['txt_41'] != "") { $status = "0"; } else { $status = "1"; } 一样?提前谢谢。

3 个答案:

答案 0 :(得分:0)

如果其中一个值不为空,您可以遍历$ request数组并设置$ status。

或者你可以将数组内插在一个空字符串上。我想。

$status = implode ('', $request) !== '';

答案 1 :(得分:0)

查看以下功能。它需要两个参数。 $ requestData将包含通过请求收到的所有参数。 $ parameters将是一个数组,您可以在其中编写所有要确保已收到请求的参数。

对函数的调用可以是这样的:

checkParameters($requestData,array('txt_1','txt_2','txt_3','txt_4')) ;

如果您有很多参数,您也可以创建一个单独的参数数组,然后将其作为参数传递。

 function checkParameters($requestData,$parameters) 
        {   

            $check=0;
            if ((count($requestData) >= count($parameters)) 
            {
                $check=0;
                foreach ($requestData as $key=>$value) 
                {
                    if (in_array($key,$parameters, TRUE)) 
                    {
                        $check=1;
                        unset($parameters[array_search($key,$parameters)]);
                    }
                    else
                    {
                            return 0;
                    }
                }
            }
            if($check)
            {   
                if (count($parameters) == 0) 
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }

返回1代表好去,而0代表一些错误。

答案 2 :(得分:0)

使用以下函数检查所有参数以进行验证,它适用于所有类型的请求方法(put / post / get / ...)。 param应该是以下字段数组:$ required_fields = array('field_1','field_2');

function verifyRequiredParams($required_fields) {
    $error = false;
    $error_fields = "";
    $request_params = array();
    $request_params = $_REQUEST;
    // Handling PUT request params
    if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
        $app = \Slim\Slim::getInstance();
        parse_str($app->request()->getBody(), $request_params);
    }
    foreach ($required_fields as $field) {
        if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
            $error = true;
            $error_fields .= $field . ', ';
        }
    }

    if ($error) {
        // Required field(s) are missing or empty
        // echo error json and stop the app
        $response = array();
        $app = \Slim\Slim::getInstance();
        $response["error"] = true;
        $response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
        echoResponse(400, $response);
        $app->stop();
    }
}