将 Laravel 输入数据持久化到会话的优雅方式

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

An Elegant Way to Persist Laravel Input Data to the Session

phplaravellaravel-5.2

提问by Samuel Hawksby-Robinson

I'm trying to find if there is an elegant way to pass data from the request->input() into the request->session(); . What I'm trying to achieve is to receive user input which I'd like to then reuse in subsequent http requests.

我试图找出是否有一种优雅的方式将数据从 request->input() 传递到 request->session(); . 我想要实现的是接收用户输入,然后我想在后续的 http 请求中重用这些输入。

The Ugly Way:

丑陋的方式:

I can do this in an ugly way:

我可以用一种丑陋的方式做到这一点:

$request->session()->put('name', $request->input('name'));
$request->session()->put('address', $request->input('address'));
$request->session()->put('age', $request->input('age'));
$request->session()->put('bed_time', $request->input('bed_time'));

The Laravelly Way:

Laravelly 方式:

But that doesn't seem very Laravelly, I feel like there should be a way so that I could do something like:

但这似乎不是很 Laravelly,我觉得应该有一种方法可以让我做类似的事情:

$request->input()->persist();

or

或者

$request->persistInput();

Unfortunately I can't find anything in the docs that gives me a nicer way to implement this. Is there a way to achieve this? Or am I stuck with this ugly unlaravelly way of persisting data into the session?

不幸的是,我在文档中找不到任何可以让我更好地实现这一点的内容。有没有办法实现这一目标?或者我是否坚持这种将数据持久化到会话中的丑陋的unlaravely方式?



Edit :

编辑 :

I've accepted Jeff's answeras the solution to my problem. Which partially details the Laravelly way of approaching this problem.

我已经接受杰夫的回答作为我问题的解决方案。其中部分详细说明了 Laravelly 解决此问题的方法。

$request->flash();

Above is the solution on the request that received the input data. However in all subsequent http requests I need to include the following, and access the data like such.

以上是对接收到输入数据的请求的解决方案。但是,在所有后续的 http 请求中,我需要包含以下内容,并像这样访问数据。

//This makes the data available for the next request.
$request->reflash();

// I can access the data from the last request using the 'old' method.
$name = $request->old('name');
$address = $request->old('address');

I need to reflash everytime I want to pass the data down the line another request.

每次我想将数据向下传递到另一个请求时,我都需要重新刷新。

回答by Jeff Lambert

See this manual page. It covers flashing data to session for the next request, which may be what you're after:

请参阅此手册页。它涵盖了为下一个请求将数据闪存到会话,这可能是您所追求的:

$request->flash();

This will take all of your old input fields and store them in the session's flash data, keeping them there until the next request. This is useful if, for instance, a user inputs data that does not validate and you wish to redirect with the old data so that you can prepopulate form fields. In fact, there's an easier way to return both a redirect andflash the same data into session:

这将获取所有旧的输入字段并将它们存储在会话的闪存数据中,将它们保留在那里直到下一个请求。例如,如果用户输入未验证的数据并且您希望使用旧数据重定向以便您可以预填充表单字段,则这很有用。事实上,有一种更简单的方法可以返回重定向并将相同的数据闪存到会话中:

return redirect()->back()->withInput();

You can even specify data that you do notwant to keep:

你甚至可以指定数据,你希望保留:

return redirect()->back()->withInput($request->except('password'));

Update

更新

Thanks to @Rifki for pointing this out in the comments, but if you would like to keep the data for subsequent HTTP requests and you're using flash data, then on the next request you can tell the Laravel Session to keep the data for another request:

感谢@Rifki 在评论中指出这一点,但是如果您想保留后续 HTTP 请求的数据并且您使用的是 flash 数据,那么在下一个请求中,您可以告诉 Laravel Session 为另一个请求保留数据要求:

$request->session()->reflash();

Depending on how long lived this data is, though, you may just want to go ahead and permanently store the data into session. Have a look at the different ways you can retrieve input in thismanual entry:

但是,根据这些数据的生存时间,您可能只想继续将数据永久存储到会话中。看看您可以在手册条目中检索输入的不同方式:

// Store all input
$request->session()->put('some_key', $request->all());
// Store a subset of input
$request->session()->put('some_key', $request->only(['field1', 'field2']));
// Store all except
$request->session()->put('some_key', $request->except('password'));

回答by jakehallas

You could shorten this down to one line. If you use the exceptmethod it will return an array meaning you can do this:

您可以将其缩短为一行。如果您使用该except方法,它将返回一个数组,这意味着您可以这样做:

$request->session()->put('request', $request->except('la'))

It will persist the array to the requestsession key.

它将数组持久化到request会话密钥。

I've also noticed other answers giving you alternative options. All of these seem to reduce your code base, which is good but I don't think it's meeting your 'Elegance' criteria.

我还注意到其他答案为您提供了替代选择。所有这些似乎都减少了您的代码库,这很好,但我认为它不符合您的“优雅”标准。

In my opinion you can shorten your code to give the appearance of 'elegance', but in terms of finding the 'Laravel way' - to my knowledge there isn't one.

在我看来,您可以缩短代码以呈现“优雅”的外观,但就找到“Laravel 方式”而言 - 据我所知,没有。

What you are after is some kind of helper function/class you can use across your App, which will provide the single method elegance you want.

您所追求的是某种可以在您的应用程序中使用的辅助函数/类,它将提供您想要的单一方法优雅。

回答by user2094178

You are almost there.

你快到了。

If you were to do the following:

如果您要执行以下操作:

session()->put('user', request()->all());

Then the following should be available and persist:

然后以下内容应该可用并保持:

session()->get('user')->name
session()->get('user')->address

Then of course, since we are talking about Laravel, there is a more elegant way of doing it.

那么当然,既然我们在谈论 Laravel,那么有一种更优雅的方式来做到这一点。

You could place the following in a middleware or service provider:

您可以将以下内容放置在中间件或服务提供商中:

view()->composer('*', function ($view)
{              
    if(session()->has('user')) {
        $view->with('user', session()->get('user'));                
    }
});

Now you can use it like that:

现在你可以这样使用它:

$user->name
$user->address

回答by scrubmx

What you can do is name your inputs under a namespace

您可以做的是在命名空间下命名您的输入

<input name="user[name]">
<input name="user[address]">
<input name="user[age]">
<input name="user[bed_time]">

The in your controller you can call putonly once:

在您的控制器中,您只能调用put一次:

$request->session()->put('user', $request->input('user'));

However, if you want to pass the data for the next request only you can do:

但是,如果您只想为下一个请求传递数据,您可以执行以下操作:

return redirect()->route('step.two')->withInput();

This will flash the input to the session.

这将刷新会话的输入。