这是从头开始的项目,所以如果您想要,您可以按照我的说明完成。使用withInput的Laravel重定向不允许更改输入值
我创建了以下迁移刚刚创建的用户表并添加新的记录:
public function up()
{
Schema::create('users', function ($table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name', 60)->unique();
$table->string('email', 120)->unique();
$table->string('password', 256);
$table->rememberToken();
$table->timestamps();
});
DB::table('users')->insert(
[
[
'name' => 'admin',
'email' => '[email protected]',
'password' => Hash::make('password')
]
]
);
}
public function down()
{
Schema::drop('users');
}
我的路线是:
<?php
Route::get('/', [
'as' => 'main_route',
function() {
return View::make('hello');
}
]);
Route::post('/', [
'as' => 'main_route',
function() {
if (!Auth::attempt(
[
'email' => Input::get('email_to_fill'),
'password' => Input::get('password_to_fill'),
]
)
) {
}
return Redirect::route('main_route')->withInput();
}
]);
Route::get('/logout', [
'as' => 'logout',
function() {
Auth::logout();
return Redirect::route('main_route');
}
]);
我hello.blade.php
是:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
@if (Auth::check())
I'm logged in. E-mail {{ Auth::user()->email }}
<a href="{{ URL::route('logout') }}">Log out</a>
{{Form::open(['url' => URL::route('main_route'), 'role' => 'form']) }}
{{ Form::text('email', Auth::user()->email) }}
{{Form::submit()}}
{{Form::close() }}
@else
{{Form::open(['url' => URL::route('main_route'), 'role' => 'form']) }}
{{ Form::text('email', 'do not touch this') }}
{{ Form::text('email_to_fill', '[email protected]') }}
{{ Form::text('password_to_fill', 'password') }}
{{Form::submit()}}
{{Form::close() }}
@endif
</body>
</html>
没有什么复杂至今。
因此,我打开我的浏览器主页面并单击发送表单(数据已填充到HTML中,因此我不需要填写任何内容)。
以下图片:
发送表单后我被登录,但正如你看到的输入值是不正确的。在代码中,它应该显示在输入值Auth::user()->email
中,但它在发送前从email
字段显示旧值。
问题: - 在使用withInput
与重定向它会自动填充具有相同值的所有表单输入,甚至通过手动其他值将使用旧数据是否正确Laravel行为?大概这个例子可能会更简单一些(根本没有用户登录),但这是我面对的确切问题,所以我尽可能简单地把它放在这里。
所以它基本上意味着当我有表单时,我不需要在使用withInput时为任何字段设置值,因为Laravel会自动填充所有字段的值。如果例如对于一个字段,我想设置其他值,我需要更改此输入名称或不使用withInput方法或使用Input :: except?我浪费了大约6个小时将问题缩小到上面的代码,因为我认为这里的'Auth'存在相当大的问题,而不仅仅是形式。 – 2014-09-19 12:43:04
是的 - 如果你真的需要,我会亲自使用'Input :: except',但是你也可以做一些类似于通过插页式控制器操作(例如'/ login' POSTs到'/ login/process'然后在登录时转发到'/ dashboard')。这样,你可以在失败时将' - > withInput()'重定向到 – Joe 2014-09-19 13:11:20
事实上,这是更复杂的形式,其中有很多字段,如果用户没有登录,他可以填写要登录的字段,如果他重新发送表单并且其他一些数据无效,我想显示他的用户数据。现在我知道我可以例如更改这两个输入名称并以其他方式解决它。谢谢 – 2014-09-19 13:14:08