如何在Laravel中使用旧输入重定向? [英] How can I redirect with old input in Laravel?

查看:80
本文介绍了如何在Laravel中使用旧输入重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录模式.当用户登录通过身份验证失败时,我想使用:

I have a login modal. When the user log-in fail the authentication, I want to redirect back to this modal with :

  • 错误消息
  • 和旧输入.

// 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() !!}


正如您在我的图像中看到的那样,似乎显示了错误消息,但是似乎没有填充用户名的旧输入.


As you can see as part of my image, the error message seem to display, but the old input of username doesn't seem to populate.

有人可以纠正我吗? 我忘了做任何事吗? Laravel完成这样的最有效的方法是什么?

Can someone please correct me ? Did I forget to do anything ? What is the most efficient way in Laravel to accomplish something like this ?

推荐答案

您可以像这样访问最后的输入:

You can access the last inputs like so:

$username = Request::old('username');

@Arian Acosta所指出的,您可以简单地

<input type="text" ... value="{{ old('username') }}" ... >.

docs 中所述,在刀片视图中更加方便.

As described in the docs it is more convenient in a blade view.

控制器中有几种可能性:

There are several possibilties in the controller:

代替 ->withInput(Request::except('password'))

这样做:

a) Input::flash();

或:

b) Input::flashExcept('password');

并使用以下命令重定向到视图:

and redirect to the view with:

a) ->withInput(Input::except('password'));

resp.与:

b) ->withInput();

有关旧输入法的文档以供进一步阅读...

The docs about Old Input for further reading...

这篇关于如何在Laravel中使用旧输入重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆