Laravel:重定向到另一个需要 POST 变量的控制器

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

Laravel: redirect to another controller which requires POST variables

laravellaravel-4

提问by LushSoftware

I have ReportController Index which is routed as POST only

我有 ReportController 索引,它仅作为 POST 路由

public function index() // must have start, end, client
{
    $start  = Input::get('start'); // <<< This are POST variables
    $end    = Input::get('end');   // <<< This are POST variables
    $client = Input::get('client'); This are POST variables

    db request... output view..

}

when I click "delete row", it post info to

当我点击“删除行”时,它会将信息发布到

public function deleteRow()
{
    db request -> delete();
    //How do I go back to index controller and pass same $_POST['start'],$_POST['end'],$_POST['client']
}

How do I go back to index controller and pass same $_POST['start'],$_POST['end'],$_POST['client']?

如何返回索引控制器并传递相同的 $_POST['start'],$_POST['end'],$_POST['client']?

回答by JofryHS

You might be able to use Redirect::to('url')->withInput()

你也许可以使用 Redirect::to('url')->withInput()

Then you can use Input::get('key')

然后你可以使用 Input::get('key')

If that doesn't work, try Input::old('key')-> Less pretty

如果这不起作用,请尝试Input::old('key')-> 不那么漂亮

回答by The Alpha

Your post variables are no longer available once you make another request to the deleteRowmethod from the view, so you have to pass those variables to the deleteRowmethod. You build a view/uifrom your indexmethod like

一旦您deleteRow从视图向该方法发出另一个请求,您的 post 变量将不再可用,因此您必须将这些变量传递给该deleteRow方法。你view/ui从你的index方法构建一个

public function index() // must have start, end, client
{
    $start  = Input::get('start');
    $end    = Input::get('end');
    $client = Input::get('client');

    db request... output view.. // <-- Outputs view with "delete row" link
}

Hope, you pass those postvariables in this view, if not then pass those variables to this view and build the delete rowlink with these variables, something like

希望,您post在此视图中传递这些变量,如果没有,则将这些变量传递到此视图并delete row与这些变量建立链接variables,例如

"ReportController/deleteRow/$start/$end/$client" // just an idea

Which means, your deleteRowmethod now should look like (also make changes in the routing of this)

这意味着,您的deleteRow方法现在应该看起来像(也在此路由中进行更改)

public function deleteRow($start, $end, $client)
{
    // db request -> delete();
    return Redirect::to('index')
    ->with('postVars', array('start' => $start, '$end' => $end, 'client', $client));
}

So, it's clear that you have to pass those variables to the deleteRowmethod and that's why deleteRowmethods routeshould be reconstructed according to the params. So, finally, your indexmethod should look like

因此,很明显您必须将这些变量传递给deleteRow方法,这就是deleteRow方法route应该根据params. 所以,最后,你的index方法应该看起来像

public function index() // must have start, end, client
{
    $postVars = session::has('postVars') ? session::get('postVars') : Input:all();
    $start = $postVars['start'];
    $end = $postVars['end'];
    $client = $postVars['client'];

    db request... output view..

}