php Laravel 5 withInput old 总是空的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31148109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 withInput old always empty
提问by Diogo Mendon?a
I'm building a simple login form using Laravel 5 and I would like to fill some inputs by default when some error occurred. My router is calling these functions as given
我正在使用 Laravel 5 构建一个简单的登录表单,我想在发生某些错误时默认填写一些输入。我的路由器正在调用这些函数
Route::get('/login','AuthController@login');
Route::post('/login','AuthController@do_login');
My Controller is like showed below
我的控制器如下所示
namespace App\Http\Controllers;
use App;
use App\Models\User;
use Request;
use Illuminate\Support\Facades\Redirect;
class AuthController extends Controller {
function login()
{
if(User::check())
{
/*User is logged, then cannot login*/
return Redirect::back();
}
else
{
return view('auth.login');
}
}
function do_login()
{
return view('auth.login')->withInput(Request::except("password"));
}
}
Noticing that withInput is being underlined by PhpStorm saying that can't find it's implementation in Illuminate\View\View. Inside my view, i have the following:
注意到 withInput 被 PhpStorm 加了下划线,说在 Illuminate\View\View 中找不到它的实现。在我看来,我有以下几点:
{!! Form::open() !!}
<div class="form-group">
{!! Form::text('email', Input::old('email'), array('placeholder' => Lang::get('auth.email'), 'class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::password('password', array('placeholder' => Lang::get('auth.password'),'class'=>'form-control')) !!}
</div>
<div class="form-group">
<button class="btn btn-primary btn-block">@lang('auth.signin')</button>
<span class="pull-right"><a href="/{{App::getLocale()}}/register">@lang('auth.register')</a></span>
</div>
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
{!! Form::close() !!}
What am I missing here? This can be done in Laravel 5 ? Many thanks for your time!
我在这里缺少什么?这可以在 Laravel 5 中完成吗?非常感谢您的时间!
回答by ceejayoz
withInput()
is for preserving the input during redirects.
withInput()
用于在重定向期间保留输入。
It is not necessary (nor possible) to call it if you're doing a return view()
.
如果您正在执行return view()
.
You might consider having your do_login
do return redirect()->back()->withInput(Request::except("password"));
though, to properly implement Post/Redirect/Get.
不过,您可能会考虑自己do_login
做return redirect()->back()->withInput(Request::except("password"));
,以正确实施Post/Redirect/Get。
回答by egdavid
I would suggest you to use the MessageBag class. First import it's namespace at the top of your controller's file:
我建议您使用 MessageBag 类。首先在控制器文件的顶部导入它的命名空间:
use Illuminate\Support\MessageBag;
Then you can use it in your controller:
然后你可以在你的控制器中使用它:
$message = new MessageBag(["username" => "A default and valid username"]); // Create your message
Redirect::to('auth/login')->withErrors($message); // Return it to your view, whatever the url
And then in your auth/login view:
然后在您的身份验证/登录视图中:
{!! Form::text('username', Input::old('username'), array('placeholder' => ($errors->has('username')?$errors->first('username'):Lang::get('auth.username')), 'class' => 'form-control')) !!}
You could even replace the input's value too if you want:
如果需要,您甚至可以替换输入的值:
{!! Form::text('username', ($errors->has('username')?$errors->first('username'):null), array('placeholder' => ($errors->has('username')?$errors->first('username'):Lang::get('auth.username')), 'class' => 'form-control')) !!}
Your "username" input should now contain the "A default and valid username" message if an error is encountered.
如果遇到错误,您的“用户名”输入现在应包含“默认且有效的用户名”消息。
回答by drcat
Passing data in the error object is a helpful tip? What if you're going to use the error object the way it's intended?
在错误对象中传递数据是一个有用的提示吗?如果您打算按照预期的方式使用错误对象怎么办?
Instead, there is a proper way to do that. The second parameter you're passing to ->view is data! horray!
相反,有一种正确的方法可以做到这一点。您传递给 ->view 的第二个参数是数据!天啊!
return view('your.view', dataYouWannaPass)
does the job perfectly!
完美地完成工作!
laravel 5.1 doc also recomends using just
laravel 5.1 doc 也推荐使用 just
->with($data)
回答by Anthony Pillos
My solution for this problem... Instead of putting your routes inside of this
我对这个问题的解决方案......而不是把你的路线放在这个里面
Route::group(['middleware' => ['web']], function () { });
Just remove it and it will work properly. Its like 'web' middleware is loading twice.
只需删除它,它就会正常工作。它就像“网络”中间件加载了两次。
回答by WebTim
Building on @AnthonyPillos 's answer,
基于@AnthonyPillos 的回答,
Laravel 5.2.27 I think it was removed the need for the web middleware group, hence why removing this works.
Laravel 5.2.27 我认为它删除了对 web 中间件组的需求,因此为什么删除它有效。
So:
所以:
Route::group(['middleware' => ['web']], function () { });
becomes redundant.
变得多余。
Hope it helps someone else searching through validateRequests, validatesLogin etc. etc. just to discover it was the route.
希望它可以帮助其他人通过 validateRequests、validatesLogin 等进行搜索,以发现它是路由。
回答by Niladri Banerjee - Uttarpara
In Laravel 5.2, you can write
return Redirect::back()->withErrors([$errors])->withInput(Input::all());
在 Laravel 5.2 中,你可以写
return Redirect::back()->withErrors([$errors])->withInput(Input::all());
回答by haibo cu
When you return one redirect requirement(http response header "Location" item is not null), the session data corresponding to withInput will not be erased. So the next request can access this data.
当你返回一个重定向要求(http响应头“Location”项不为空)时,withInput对应的会话数据不会被擦除。所以下一个请求可以访问这个数据。
When you return the response page with no redirect requirement("Location" item is null), the session data corresponding to withInput will be erased.
当您返回没有重定向要求的响应页面时(“Location”项为空),withInput 对应的会话数据将被删除。
So, we can see that "withInput" function only work in redirect.
所以,我们可以看到“withInput”函数只在重定向中起作用。