gears/session

此包已被弃用且不再维护。没有建议的替代包。

Laravel Sessions 独立版

v0.8.0 2014-10-03 04:21 UTC

This package is not auto-updated.

Last update: 2020-01-24 15:26:25 UTC


README

寻找维护者,我现在几乎不做PHP开发,我已经转向其他领域,现在主要工作在 dotnet core、node.js 和 golang 上。如果有人有兴趣接管这些项目,请联系我 - brad@bjc.id.au

Session Gear

Build Status Latest Stable Version Total Downloads License

Laravel Sessions 独立版

好吧,现在可能你已经听说过 Laravel,这个让PHP开发变得简单的框架。所以首先,我们必须给 Taylor Otwell 的 Session API 点赞。

如何安装

通过 composer 安装很简单

composer require gears/session:*

如何使用

在你的 旧版 非Laravel应用中。你可以像这样使用Laravel Session API

// Make sure you have composer included
require('vendor/autoload.php');

// Create a new gears session.
$session = new Gears\Session();

// Configure the session container
$session->dbConfig = 
[
	'driver'    => 'mysql',
	'host'      => 'localhost',
	'database'  => 'db_name',
	'username'  => 'db_user',
	'password'  => 'abc123',
	'charset'   => 'utf8',
	'collation' => 'utf8_unicode_ci',
	'prefix'    => '',
];

// Install the session api
$session->install();

// Next you will probably want to make the session object global.
$session->globalise();

注意:dbConfig数组必须描述一个有效的数据库连接。此数组直接传递给$capsule->addConnection。有关更多信息,请参阅

现在你可以使用以下代码

// Storing An Item In The Session
Session::put('key', 'value');

// Push A Value Onto An Array Session Value
Session::push('user.teams', 'developers');

// Retrieving An Item From The Session
$value = Session::get('key');

// Retrieving An Item Or Returning A Default Value
$value = Session::get('key', 'default');
$value = Session::get('key', function() { return 'default'; });

// Retrieving An Item And Forgetting It
$value = Session::pull('key', 'default');

// Retrieving All Data From The Session
$data = Session::all();

// Determining If An Item Exists In The Session
if (Session::has('users'))
{
    //
}

// Removing An Item From The Session
Session::forget('key');

// Removing All Items From The Session
Session::flush();

// Regenerating The Session ID
Session::regenerate();

// Flashing Data
Session::flash('key', 'value');

// Reflashing The Current Flash Data For Another Request
Session::reflash();

// Reflashing Only A Subset Of Flash Data
Session::keep(array('username', 'email'));

有关Session API的更多信息,请参阅: https://laravel.net.cn/docs/session

注意:虽然Laravel Session API支持许多不同的驱动程序。但此包目前仅支持数据库驱动程序。

警告:不要使用内置的PHP session函数和/或全局$_SESSION数组

我们的额外方法:hasExpired

据我所知,Laravel中没有内置的方法来判断一个会话是否已设置但已过期。因此,在一个普通的Laravel应用中,如果你想显示一个“您的会话已过期!”的消息,你可能需要进行一些自定义过滤器或其他操作...请参阅

http://stackoverflow.com/questions/14688853/check-for-session-timeout-in-laravel

但使用 Gears\Session 只需调用

if (Session::hasExpired())
{
	echo 'Due to inactivity, your session has expired!';
	echo 'Please <a href="/login">click here</a> to login again.';
}

那么,为什么会有这个呢?

虽然laravel非常酷,但如果要从Laravel中提取一个功能用于其他项目,可能会变得困难。首先,你必须对 IoC 容器 有深入的了解。

然后你会发现这个类需要那个类,而那个类又需要一些其他配置变量,通常在运行在普通Laravel应用中的IoC中存在。但在你的情况下,你没有定义它,而且也不想在你的应用中定义这个值,因为它在你的例如 旧版 应用中没有意义。

完美的例子是我试图将session API提取出来用于WordPress。它想知道一个booted方法,我认为这个方法来自Illuminate\Foundation\Application。在这个时候,我已经不得不添加各种其他东西到IoC中,以使其满意,这是压垮骆驼的最后一根稻草,我发起了程序员的脾气,走到冰箱前,抓起另一罐红牛,并以新的方法坐下来。

结果就是这个项目。

由Brad Jones开发 - brad@bjc.id.au