php 如何在不使用请求的情况下删除 Laravel 5.3 中的会话?

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

How to delete session in Laravel 5.3 without using Request?

phplaravelsessionlaravel-5laravel-5.3

提问by cytsunny

I have a method in a controller that needs to handle the session. That method is called by a get method that doesn't require any user input, so I would like to do it without Requestclass.

我在需要处理会话的控制器中有一个方法。该方法由不需要任何用户输入的 get 方法调用,所以我想在没有Request类的情况下进行。

Currently, I am able to set the session, but I cannot find a way to delete it. It looks something like this:

目前,我可以设置会话,但找不到删除它的方法。它看起来像这样:

if ($boolean_storing_condition_value)
    session(['some_data'=>'Some Data']);
else
   /* What should be the unset function? */

In Laravel 4.2, it is done with Session::forget('some_data');or Session::flush(). How should that be done in Laravel 5.3?

在 Laravel 4.2 中,它是通过Session::forget('some_data');或完成的Session::flush()。在 Laravel 5.3 中应该如何做到这一点?

回答by Jerodev

You can use the session helperwithout having to use a request object.

您可以使用会话助手而不必使用请求对象。

session()->forget('some_data');
session()->flush();

回答by Alexey Mezenin

In Laravel 5.3 you still can use flush()and forget()methods:

在 Laravel 5.3 中你仍然可以使用flush()forget()方法:

session()->flush();
session()->forget('key');

https://laravel.com/docs/5.3/session#deleting-data

https://laravel.com/docs/5.3/session#deleting-data

回答by Jishnu RS

add session()->save();after that.

session()->save();在那之后添加。

回答by Nadeem Qasmi

To delete session variable in Laravel 5.6

在 Laravel 5.6 中删除会话变量

session()->forget(['key1']);

to delete the session variables (More the one value delete from the session) use the arguments as arguments session()->forget([' ']);

删除会话变量(从会话中删除更多的一个值)使用参数作为参数session()->forget([' ']);

session()->forget(['key1','key1','key3','...']);