php - 如何在 Laravel 中使用旧输入进行重定向?

标签 php forms laravel laravel-5 laravel-blade

我有一个登录模式。当用户登录未通过身份验证时,我想重定向回此模式:

  • 错误信息
  • 和旧输入。

enter image description here


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/

相关文章:

java - 无法验证使用 jBcrypt 使用 php password_hash() 创建的 passwordhash+salt

ruby-on-rails - 为相关数据库创建Rails下拉列表

javascript - Rails 中渲染表单的结果没有表单标签,只有输入

laravel - 在Laravel中为Elasticquent创建索引

php - Laravel 一次修改多行

php - 获取 30 天前的日期并转换为 iso8601 格式

php - 如何整合 COUNT(*)?

php - 我在将日期格式化为dd-mm-yyyy时遇到问题

php - 通过 HTML 表单覆盖 MySQL 值

php - 我无法从 Laravel 中的请求 AJAX 检索数据