简体   繁体   English

Slim 框架请求验证

[英]Slim Framework Request Validation

Hello so I am using slim framework, and I have a code that check if all of the inputs are not empty, here is the code:你好,所以我使用的是瘦框架,我有一个代码来检查所有输入是否为空,这是代码:

$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";
    }

What I wanted to do is instead of coding all request inputs in the condition, I only want a single variable to check all of the request inputs if not empty.我想要做的不是对条件中的所有请求输入进行编码,我只想要一个变量来检查所有请求输入(如果不是空的)。 Is there a way?有办法吗? Like (!empty($allFields)) ?(!empty($allFields)) Thank you in advance.先感谢您。

You could loop through your $request array and set $status if one of the values is not empty.如果其中一个值不为空,您可以遍历 $request 数组并设置 $status。

Or you could implode the array over an empty string I guess.或者你可以在我猜的空字符串上内爆数组。

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

Look at the following function.看看下面的函数。 It takes two arguments.它需要两个参数。 $requestData will contain all your parameters received via request. $requestData 将包含您通过请求收到的所有参数。 $parameters will be an array where you will write all the parameters you want to make sure that you have received in request. $parameters 将是一个数组,您将在其中写入要确保已在请求中收到的所有参数。

The call to function can be something like this:对函数的调用可以是这样的:

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

In case you are having lots of parameters, you may also make a separate array of parameters and then pass it as an argument.如果您有很多参数,您也可以创建一个单独的参数数组,然后将其作为参数传递。

 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;
            }
        }

Return 1 stands for good to go while 0 stands for some error.返回 1 代表正常运行,而 0 代表一些错误。

Use below function to check all param for validation and it works with all type of request methods(put/post/get/...).使用下面的函数来检查所有参数以进行验证,它适用于所有类型的请求方法(put/post/get/...)。 param should be array of fields like: $required_fields=array('field_1','field_2'); 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM