laravel 4在重新加载页面后形成超时

时间:2016-04-13 12:34:11

标签: laravel forms

如果我重新加载/加载视图或按下浏览器中的后退按钮并获取表单并立即确认表单,我会收到以下错误消息"请等到再次提交表单"。

有可能阻止吗?

1 个答案:

答案 0 :(得分:0)

您应该有两条路径来处理表单。一个使用GET作为HTTP谓词并显示表单和使用POST并处理表单的表单。我将使用一个示例来说明使用表单来更新用户的详细信息和两个简单​​的路由定义。

表单视图,我们称之为user_details.blade.php并存储在resources/view/forms中,如下所示:

<form action="update-user/{$user->id}" action="POST">
    <input type="text" name="name" value="{{ $user->name }}">
    <input type="text" name="phone" value="{{ $user->phone }}">
    <input type="text" name="email" value="{{ $user->email }}">

    <button type="submit">Save</button>
</form>

现在您应该定义两个路径:一个显示表单,另一个处理表单。以下代码的注释解释了逻辑:

// Accessing http://domain.com/update-user/1 from the browser
// will show the user update form for the user with ID 1
Route::get('update-user/{id}', function ($id) {
    // Get the current user details so you can pass them to the view
    $user = User::find($id);

    return view('forms.my_form')->with(compact('user'));
});

// Using the same path `update-user/1` but with POST for your
// form action will match this `Route::post` definition so it
// will process the submitted form
Route::post('update-user/{id}', function(Request $request, $id) {
    $user = User::find($id);
    $user->fill($request->only('name', 'phone', 'email');
    $user->save();

    // After you've finished processing the form redirect to
    // the `update-user/{id}` route path, but since it's
    // using GET for the redirect it will match the route
    // definition that shows the form
    return redirect()->to('update-user/' . $id);
});

有一个单独的路由处理表单并自动重定向意味着当回击时你将总是返回到只显示表单的Route::get定义,浏览器不会提示你以及需要重新提交表单的消息。