Laravel-如何使用响应进行验证并给出响应

时间:2019-06-28 12:36:33

标签: laravel

我正在下面的代码中在Laravel端点中编写一个验证请求

public function validationrequest(Request $request)
{

    //log information
     $notification = new NotificationLog();
     $notification->error_message = (string)$request;
     $notification->save();

$password = $request->header('Password');
$username = $request->header('Username');

    $msisdn=$request->msisdn;
    $useresponse=$request->userresponse;
    $useroption=$request->useroption;
    $hash=$request->hash;
    $hash_key="CIA152515276585";

    $data=$msisdn.$useresponse.$useroption.$hash_key;
    $hashed = hash('sha512',$data);
//      return $hashed ;

if($username=="hdf" && $password=="980!-=_hff")
{ 
      if($hashed==$hash)
      {
      //implemetation 
     $ref_id=self::gen_uuid();
     $total_amount=456;
     $statuscode="00";
     $statusMsg = "Successful";
     $message="Welcome To BBNaija 2019";
     $data = $total_amount.$message.$statuscode.$statusMsg.$hash_key;
     $hashed = hash('sha512',$data);
     //adding saving user response
      $user_response=new User_Response;
      $user_response->msisdn=$msisdn;
      $user_response->answer=$useroption;
      $user_response->user_channel="GTB-737";
      $user_response->game_code=$useresponse;
      $user_response->save();
        return response()->json([
         'amount'=>$total_amount,
          'statusCode'=>$statuscode,
           'statusMessage'=>$statusMsg,
            'message'=>$message,
              'hash'=>$hashed
            ]);

}
else
{

    return response(['errorMsg' =>"99", 'description' =>"Invalid haskey"]);
}
}
else
{
    return response(['errorMsg' =>"99", 'description' =>"Invalid username and password"]);
}

    }

保存后我希望它在里面。

  

if($ username ==“ gtpay” && $ password ==“ 678!-= _ bbnaija1234”)

>If it is successful:

>If $user_response->game_code = 101: JSS 1 succeesfully saved 
>If $user_response->game_code = 102: JSS 2 succeesfully saved
>If $user_response->game_code = 103: JSS 3 succeesfully saved

我该如何实现并回应提交的内容。我希望验证在保存后保存在else语句

之前

1 个答案:

答案 0 :(得分:0)

    ...
    if($username=="hdf" && $password=="980!-=_hff")
    {
        ...
        $rules = [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ];

        $request->validate($rules);
        ...

标准验证实施效果不佳吗?

您可以在docs中阅读有关规则等的更多信息。

话虽如此,在将请求持久化到数据库之前,最好创建请求验证类并验证所有数据。