我有一个登录模式。当用户登录未通过身份验证时,我想重定向回此模式:
- 错误信息
- 和旧输入。
Controller
// if Auth Fail
return Redirect::to('/')
->with('error','Username/Password Wrong')
->withInput(Request::except('password'))
->withErrors($validator);
表格
{!! Form::open(array('url' => '/', 'class' => 'login-form')) !!}
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Enter Username" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Enter Password" required>
</div>
<button type="submit" class="btn btn-primary">Login</button>
{!! Form::close() !!}
正如您在我的图片中看到的那样,似乎显示了错误消息,但似乎没有填充用户名的旧输入。
有人可以纠正我吗? 我忘记做任何事了吗? 在 Laravel 中完成这样的事情最有效的方法是什么?
最佳答案
您可以像这样访问最后的输入:
$username = Request::old('username');
正如 @Arian Acosta 所指出的那样你可以简单地做
<input type="text" ... value="{{ old('username') }}" ... >
.
如 docs 中所述在 Blade View 中更方便。
Controller 中有几种可能性:
代替
->withInput(Request::except('password'))
做:
a) Input::flash();
或:
b) Input::flashExcept('password');
并重定向到 View :
a) ->withInput(Input::except('password'));
响应。与:
b) ->withInput();
docs about Old Input进一步阅读...
关于php - 如何在 Laravel 中使用旧输入进行重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30002608/