提交表单后,Web显示空白页面



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

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

这是我在main.blade.php中的帖子表单。我知道laravel帖子表单必须输入$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((,并尝试删除进程,如果它在中显示空白页

*/
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 IlluminateHttpResponse
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @return IlluminateHttpResponse
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param  AppModelsTask  $task
* @return IlluminateHttpResponse
*/
public function show(Task $task)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param  AppModelsTask  $task
* @return IlluminateHttpResponse
*/
public function edit(Task $task)
{
//
}
/**
* Update the specified resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @param  AppModelsTask  $task
* @return IlluminateHttpResponse
*/
public function update(Request $request, Task $task)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param  AppModelsTask  $task
* @return IlluminateHttpResponse
*/
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显示黑色页面。问题可能出在哪里?

form标记中的action属性为空。将其更改为相应的路线

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

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

之后,如果您向该端点发布,那么将调用的控制器函数就是store。文档

我找到了一个解决方案。我改变了以下路线:

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

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

最新更新