php 如何在 Laravel 5 中删除会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29462374/
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
how to remove sessions in Laravel 5
提问by rockie
I am trying to remove a basic session but it's not removing. Here's the code
我正在尝试删除一个基本会话,但它没有删除。这是代码
welcome.blade.php
欢迎.blade.php
@if(Session::has('key'))
{{ Session::get('key')}}
<a href="logout">Sign Out</a>
@else
please signin
@endif
</div>
I don't know how to remove session. Here's the one I used but it's not working route.php
我不知道如何删除会话。这是我用过的,但它不起作用route.php
Route::get('/logout', function() {
$vv = Session::forget('key');
if($vv)
{
return "signout";
}
});
回答by Imtiaz Pabel
You should use this method
你应该使用这个方法
Route::get('/logout', function() {
Session::forget('key');
if(!Session::has('key'))
{
return "signout";
}
});
回答by Clément Rigo
You can try with Session::pull('key');
你可以试试 Session::pull('key');
And if you want to delete all sessions variable you can use Session::flush();
如果你想删除所有会话变量,你可以使用 Session::flush();
回答by dylan_the_wizard
Session::forget()
does not return true/false. You can just remove your if
statement.
Session::forget()
不返回真/假。您可以删除您的if
声明。
As a side note, if you're only using the user
key in Session
for storing the currently logged in user, you can just use Auth::user()
instead.
附带说明一下,如果您仅使用user
密钥Session
来存储当前登录的用户,则可以Auth::user()
改为使用。
回答by J.C. Gras
You can use the Request
parameter, containing the current session. This way you can delete any session value by the key:
您可以使用Request
包含当前会话的参数。这样您就可以通过键删除任何会话值:
use Illuminate\Http\Request;
Route::get('/logout', function(Request $request) {
//Uncomment to see the logs record
//\Log::info("Session before: ".print_r($request->session()->all(), true));
if ($request->session()->has('key')) {
$request->session()->forget('key');
}
//Uncomment to see the logs record
//\Log::info("Session after: ".print_r($request->session()->all(), true));
return redirect('/');
});
Or you can delete all values in the session:
或者您可以删除会话中的所有值:
use Illuminate\Http\Request;
Route::get('/logout', function(Request $request) {
//Uncomment to see the logs record
//\Log::info("Session before: ".print_r($request->session()->all(), true));
$request->session()->flush();
//Uncomment to see the logs record
//\Log::info("Session after: ".print_r($request->session()->all(), true));
return redirect('/');
});
Reference: https://laravel.com/docs/5.3/session#using-the-session
回答by Adeyemo Kabir A
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class StudentRecord extends Controller
{
public function logout(Request $req)
{
if($req-session()->has('key'){
$req->session()->forget('key');
return redirect('/');
}
//Or simple
public function logout(Request $req)
{
//if($req-session()->has('key')
$req->session()->flush();
}
}
//Then Do this in your route file
Route:get("/logout",StudentRecord@logout);
<a href=" {{ url(logout)}}">logout</a>