laravel 控制器重定向回 POST 表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20547037/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:45:28  来源:igfitidea点击:

Controller redirect back to a POST form

laravellaravel-4

提问by James

I have a table showing a list of names, with an "edit" button and hidden id value at the side. Clicking the "edit" button will post the hidden id as a form value and display the edit page so the user can change details of that person. Pretty standard.

我有一个显示名称列表的表格,旁边有一个“编辑”按钮和隐藏的 id 值。单击“编辑”按钮会将隐藏的 id 作为表单值发布并显示编辑页面,以便用户可以更改该人的详细信息。很标准。

When editing the details and submitting I'm using a validator. If the validation fails it needs to go back to the edit page and display the errors. Problem is that the edit page required an Id value via POST method, but the redirect only seems to utilise the GET method which leads to a "Controller Method Not Found" error as there is no Get route set up.

编辑详细信息并提交时,我使用的是验证器。如果验证失败,则需要返回编辑页面并显示错误。问题是编辑页面需要通过 POST 方法获得 Id 值,但重定向似乎只利用了 GET 方法,这会导致“找不到控制器方法”错误,因为没有设置 Get 路由。

Does anyone know how I can redirect back to the page via POST not GET. Currently my code looks like:

有谁知道我如何通过 POST 而不是 GET 重定向回页面。目前我的代码看起来像:

public function postEditsave(){
    ...
    if ($validator->fails())
    {
        return Redirect::to('admin/baserate/edit')
        ->withErrors($validator)
        ->withInput();
    }else{ 
                ...

thanks

谢谢

采纳答案by edpaez

You don't need to use POST to go to the edit page. You can use GET and a parameter to the route, check this out: http://laravel.com/docs/routing#route-parameters

您无需使用 POST 即可进入编辑页面。您可以使用 GET 和路由参数,请查看:http: //laravel.com/docs/routing#route-parameters

You'd have a GET route to show the edit page, and a POST route to handle the request when the user submits the form.

您将有一个 GET 路由来显示编辑页面,还有一个 POST 路由来处理用户提交表单时的请求。

It'll look like this (notice the parameters):

它看起来像这样(注意参数):

public function getEdit($id)
{
    return View::make(....);

}

public function postEdit($id)
{
    ...
    return Redirect::back()->withErrors($validator)->withInput();
}

回答by Juliano Pezzini

You can use Redirect::back()->withInput();

您可以使用 Redirect::back()->withInput();

You may wish to redirect the user to their previous location, for example, after a form submission. You can do so by using the back method

您可能希望将用户重定向到他们之前的位置,例如,在提交表单之后。您可以通过使用 back 方法来做到这一点

See: http://laravel.com/docs/5.0/responses

请参阅:http: //laravel.com/docs/5.0/responses

回答by egig

if "redirect with POST" is exist, then I don't know it. I recomend you to just use flash data

如果存在“使用 POST 重定向”,那么我不知道。我建议你只使用闪存数据

Redirect::to('user/login')->with('id', 'something');

回答by allaghi

You can use Redirect::to("dashboard/user/$id")->withErrors($validator)->withInput();. You should use double quote to pass parameter if there is any errors with validation.

您可以使用Redirect::to("dashboard/user/$id")->withErrors($validator)->withInput();. 如果验证有任何错误,您应该使用双引号来传递参数。