我想输入错误的密码或用户名时进行错误处理。
在这里,我使用json url进行发布/登录
我尝试进行重定向,但仍然出错
我有这样的表单登录(index.blade.php)代码:
<form id="form-login" method="post" action="{{URL::to('/login')}}">
{{ csrf_field() }}
<div>{{ session('alert') }}</div>
<p><b>Username : <input type="text" id="username" name="noRm" value="" placeholder=" Nomor Rm"></p>
<p><b>Password : <input type="password" id="password" name="tglLahir" value="" placeholder=" Tanggal Lahir"></p>
<p><b>Captcha : <label type="text" id="mainCaptcha"></label><input type="image" id="refresh" onclick="Captcha();" src="/images/refresh.png"/></p>
<p><input type="text" id="txtInput" onchange="ValidCaptcha();"/>  
<label id="tickT" style="background-color: green; width: 25px; border-radius: 25px;"></label>
<label id="tickF" style="background-color: red; width: 25px; border-radius: 25px;"></label>
</p>
<p><input type="hidden" id="resultCaptcha" name="resultCaptcha"/></p>
<p class="submit"><input type="submit" name="commit" value="Login"></p>
</form>
这是我的路线:
Route::post('login', 'userController@getLogin');
这是我的控制器:
function getLogin(Request $req){
if ($req->resultCaptcha !== "false" && $req->resultCaptcha !== null) {
if ($req->noRm !== null && $req->tglLahir !== null) {
$client = new Client();
$response = $client->get('http://192.168.11.183:8080/PasienV2/PasienV2/cekLoginRegistro/'.$req->noRm.'/'.$req->tglLahir );
$result = json_decode($response->getBody()->getContents());
return view('login', compact('result'));
} else {
return redirect()->back()->with('alert', '' + '<button type="button" onclick="Delete(this)">X</button>' );
}
} else {
return redirect()->back()->with('alert',' False Captcha !');
}
}
用户输入错误密码后如何处理?
当我运行代码并输入错误密码时,出现如下错误:
"Undefined property: stdClass::$tanggalLahir"
我开始使用laravel框架。有人可以帮助我吗?谢谢:)
答案 0 :(得分:0)
您必须使用try catch处理第三方api方面的错误和异常。
try {
$response = $client->get('http://192.168.11.183:8080/PasienV2/PasienV2/cekLoginRegistro/'.$req->noRm.'/'.$req->tglLahir );
$result = json_decode($response->getBody()->getContents());
return view('login', compact('result'));
}catch (\Exception $e){
abort( $e->getCode() ?? 500, json_decode($e->getResponse()->getBody(true))->message??"Server error!");
}
处理异常或添加Flash消息并将其重定向到您的视图文件
像这样在刀片侧使用变量:
@if(isset($result->tanggalLahir)) {{ $result->tanggalLahir }} @endif