【问题标题】:Web shows blank page after post form submitting提交表单后网页显示空白页
【发布时间】:2021-11-05 12:45:52
【问题描述】:

这是我的路线。我使用 TaskController 类的资源

Route::resource('main',TaskController::class);

这是我在 main.blade.php 中的帖子表单。我知道 laravel post form 必须输入$crsf

 <form action="" method="post" enctype="multipart/form-data"">
    @csrf
    @method('POST')
  <input type="file" class="form-control"name="img" accept="image/*">
  <button class="btn btn-primary" name="upload" type="submit" value="ok">Button</button>
  </form>

这是我的任务控制器。我使用函数 index() 并尝试删除进程,如果它显示空白页 agian

 */
public function index(Request $req)
{
    //
    $task = Task::all();
    $data = [
        'nameTH'  =>"",
        'nameEng'   => "",
        'surnameEng' => "",
        'birth' => "",
        'religion' => "",
        'address' => "",
        'regis' => "",
        'expire' => "",
        'serial' => "",
        'task' => $task

    ];
   
    if($req->upload == "ok"){

        $seID = $this->setSecretId("key");
        $seKey = $this->setSecretKey("key");
    $file = "./3.jpg";
    $res = $this->recognizeImage(file_get_contents($file));   
    $data = [
        'nameTH'  => $res["result"]["name_th"],
        'nameEng'   => $res["result"]["first_name_en"],
        'surnameEng' => $res["result"]["last_name_en"],
        'birth' => $res["result"]["date_of_birth"],
        'religion' => $res["result"]["religion"],
        'address' => $res["result"]["address"],
        'regis' => $res["result"]["date_of_issue"],
        'expire' => $res["result"]["date_of_expiry"],
        'serial' => $res["result"]["serial_number"],
        'task' => $task
    
    ];   
   
 
  
}

    return view('main')->with($data);
}

   /**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    //
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    //
}

/**
 * Display the specified resource.
 *
 * @param  \App\Models\Task  $task
 * @return \Illuminate\Http\Response
 */
public function show(Task $task)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  \App\Models\Task  $task
 * @return \Illuminate\Http\Response
 */
public function edit(Task $task)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \App\Models\Task  $task
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, Task $task)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  \App\Models\Task  $task
 * @return \Illuminate\Http\Response
 */
public function destroy(Task $task)
{
    //
}
protected static $_endpoint = 'iai.flashsoftapi.com';
/**
 * $_requestUrl
 * request url
 * @var string
 */
protected static $_requestUri = '/v1/thai-id-card-ocr';

/**
 * $_secretId
 * secret ID
 * @var string
 */
protected $_secretId = '';

/**
 * $_secretKey
 * secret key
 * @var string
 */
protected $_secretKey = '';


/**
 * $_timeOut
 * timeout to connect host
 * @var int in seconds
 */
protected static $_timeOut = 10;

protected static function _signFC1($key, $date, $service, $str2sign)
{
    $dateKey = hash_hmac("SHA256", $date, "FC1".$key, true);
    $serviceKey = hash_hmac("SHA256", $service, $dateKey, true);
    $reqKey = hash_hmac("SHA256", "fc1_request", $serviceKey, true);
    return hash_hmac("SHA256", $str2sign, $reqKey);
}

protected function _sendRequest($url, $payload)
{
    $payload = is_array( $payload ) ? http_build_query( $payload ) : $payload;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, self::$_timeOut);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $timestamp = time();
    $nonce = rand();
    $host = self::$_endpoint;
    $content_type = "application/x-www-form-urlencoded";

    $payloadHash = hash("SHA256", $payload);

    $method = "POST";
    $canonicalUri = self::$_requestUri;
    $canonicalQueryString = "";
    $canonicalHeaders = "content-type:".$content_type."\n".
        "host:".$host."\n";
    $signedHeaders = "content-type;host";
    $canonicalRequest = $method."\n".
        $canonicalUri."\n".
        $canonicalQueryString."\n".
        $canonicalHeaders."\n".
        $signedHeaders."\n".
        $payloadHash;
    $date = gmdate("Y-m-d", $timestamp);
    $service = "th";
    $credentialScope = $date."/".$service."/fc1_request";
    $hashedCanonicalRequest = hash("SHA256", $canonicalRequest);
    $algo = "FC1-HMAC-SHA256";
    $str2sign = $algo."\n".
                $timestamp."\n".
                $credentialScope."\n".
                $hashedCanonicalRequest;
    $signature = self::_signFC1($this->_secretKey, $date, $service, $str2sign);
    $auth = $algo.
            " Credential=". $this->_secretId ."/".$credentialScope.
            ", SignedHeaders=content-type;host, Signature=".$signature;
    $headers = array(
        'X-FC-Timestamp:' . $timestamp,
        'X-FC-Nonce:' . $nonce,
        "Host:" . self::$_endpoint,
        "Content-Type:" . "application/x-www-form-urlencoded",
        "Authorization:" . $auth,
    );

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $resultStr = curl_exec($ch);
    $result = json_decode($resultStr, true);
    if (!$result)
    {
        return $resultStr;
    }
    return $result;
}

/**
 * setSecretId
 * set the secret ID
 * @param string $secretId
 */
public function setSecretId($secretId)
{
    $this->_secretId = $secretId;
    return $this;
}

/**
 * setSecretKey
 * set the secret key
 * @param string $secretKey
 */
public function setSecretKey($secretKey)
{
    $this->_secretKey = $secretKey;
    return $this;
}

public function recognizeImage($image)
{
    $payload = array();
    $payload['image'] = base64_encode($image);
    $url = "https://" . self::$_endpoint . self::$_requestUri;
    return $this->_sendRequest($url, $payload);
}

public function recognizeUrl($url)
{
    $payload = array();
    $payload['url'] = $url;
    $url = "https://" . self::$_endpoint . self::$_requestUri;
    return $this->_sendRequest($url, $payload);
}
//

当我提交表单时,laravel 显示黑页。问题可能出在哪里?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    表单标签中的action 属性为空。改成对应的路由

    【讨论】:

      【解决方案2】:

      您需要在表单上设置操作以匹配您的资源:

         <form action="/main" method="post" enctype="multipart/form-data"">
      

      然后,如果您发布到该端点,将调用的控制器函数是 store。 Documentation

      【讨论】:

        【解决方案3】:

        我找到了解决办法。我更改了以下路线:

        Route::resource('/main',TaskController::class);
        

        Route::get('/main',[TaskController::class,'index']);
        Route::post('/main',[TaskController::class,'index']);
        

        【讨论】:

        • 你不需要改变。您仍然可以使用路线&lt;form action="{{ route('main.store') }}"&gt;