Laravel Input::flash() 如何检索 flash 值

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

Laravel Input::flash() How to retrieve flash values

flashlaravelredirectinput

提问by Wasim A.

Here is my Code to keep form data save.

这是我保存表单数据的代码。

Route::any('test', function(){
    if (Request::isMethod('post')){
        //return Redirect::back()->withInput();
        return Redirect::to('test2')->withInput();
    }
    else {
        return View::make('home');
    }
});
Route::get('test2', function(){
    return var_dump(Input::all());  // Getting Empty Array here.
});

I just make a single input with name='email' and post it. When Redirect to another page, its not forwarding inputs using Redirect::to('test2')->withInput();

我只输入 name='email' 并发布它。当重定向到另一个页面时,它不使用转发输入Redirect::to('test2')->withInput();

<form action="" method="post">
    <input type="text" name="email" value="" placeholder="Your Email">
    <input type="submit" value="Submit" />
</form>

I am confused, how to use Input::flash()or Redirect::to('test2')->withInput();

我很困惑,如何使用Input::flash()Redirect::to('test2')->withInput();

回答by alexrussell

When you use Redirect::to()->withInput()what that does is puts the current state of Input::all()into a session variable for later access (it doesn'tre-submit the input or anything like that). As such, in the new, redirected, request you can't access the same input using Input:all()(as it's not re-posted) but now it's under Input::old(). Much like a flahs variable, it will only be available for one request too, so if you want to re-flash it you may need to either do Session::reflash()or you maybe able to to ->withInput()again (though I doubt it, as the new Input::all()is empty).

当您使用Redirect::to()->withInput()它的作用是将当前状态Input::all()放入会话变量以供以后访问(它不会重新提交输入或类似的东西)。因此,在新的重定向请求中,您无法使用Input:all()(因为它没有重新发布)访问相同的输入,但现在它位于Input::old(). 就像一个FLAHS变量,它只能提供一个请求,所以,如果你想重新刷新它,你可能需要或者做Session::reflash()或者你可能能够以->withInput()试(尽管我对此表示怀疑,因为新的Input::all()空)。

Hopefully that makes sense. Basically your 'test2' route needs to use Input::old().

希望这是有道理的。基本上你的“test2”路线需要使用Input::old().

All that said, it may be that you're doing something not quite right if you're redirecting a POST request to then later handle via Input::old()- that's not the real purpose of this construct. Of course, if this is you just playing around with the framework then you're all good to do whatever you want.

综上所述,如果您将 POST 请求重定向到稍后处理 via,则可能是您做的事情不太正确Input::old()- 这不是此构造的真正目的。当然,如果您只是在玩弄框架,那么您可以随心所欲。

回答by Wasim A.

Exammple:

例子:

Route::any('test', function(){
        if (Request::isMethod('post')){
            Input::flash();
            return Redirect::back();
            // return Redirect::back()->withInput();
        }
        else {
            if(Input::old('email'))return Input::old(); //::old is only accessible when we did ::flash() or ::withInput()
            return View::make('home');
    }
});

To retrieve form data on error etc use

要检索有关错误等的表单数据,请使用

Input::old()   

and Input::old() will never work without

和 Input::old() 将永远不会工作

Redirect::back()->withInput()

or

或者

Input::flash();
return Redirect::back();

Detail is attached in code as comment

详细信息作为注释附加在代码中