Session 会话

Extending Laravel with a custom session driver is just as easy as extending the cache system. Again, we will use the extend method to register our custom code:

扩展 Laravel 的会话机制和上文的缓存机制一样简单。和刚才一样,我们使用 extend 方法来注册自定义的代码:

Session::extend('mongo', function($app)
    // Return implementation of SessionHandlerInterface

Note that our custom cache driver should implement the SessionHandlerInterface. This interface is included in the PHP 5.4+ core. If you are using PHP 5.3, the interface will be defined for you by Laravel so you have forward-compatibility. This interface contains just a few simple methods we need to implement. A stubbed MongoDB implementation would look something like this:

注意我们自定义的会话驱动(译者注:原味是 cache driver ,应该是笔误。正确应为 session driver )实现的是 SessionHandlerInterface 接口。这个接口在 PHP 5.4 以上版本才有。但如果你用的是 PHP 5.3 也别担心,Laravel 会自动帮你定义这个接口的。该接口要实现的方法不多也不难。我们用 MongoDB 来实现就像下面这样:

class MongoHandler implements SessionHandlerInterface {
    public function open($savePath, $sessionName) {}
    public function close() {}
    public function read($sessionId) {}
    public function write($sessionId, $data) {}
    public function destroy($sessionId) {}
    public function gc($lifetime) {}

Since these methods are not as readily understandable as the cache StoreInterface, let's quickly cover what each of the methods do:

这些方法不像刚才的 StoreInterface 接口定义的那么容易理解。我们来挨个简单讲讲这些方法都是干啥的:

  • The open method would typically be used in file based session store system. Since Laravel ships with a native session driver that uses PHP's native file storage for sessions, you will almost never need to put anything in this method. You can leave it as an empty stub. It is simply a fact of poor interface design (which we'll discuss later) that PHP requires us to implement this method.
  • open 方法一般在基于文件的会话系统中才会用到。Laravel 已经自带了一个 native 的会话驱动,使用的就是 PHP 自带的基于文件的会话系统,你可能永远也不需要在这个方法里写东西。所以留空就好。另外这也是一个接口设计的反面教材(稍后我们会继续讨论这一点)。

  • The close method, like the open method, can also usually be disregarded. For most drivers, it is not needed.

  • close 方法和 open 方法通常都不是必需的。对大部分驱动来说都不必要实现。

  • The read method should return the string version of the session data associated with the given $sessionId. There is no need to do any serialization or other encoding when retrieving or storing session data in your driver, as Laravel will perform the serialization for you.

  • read 方法应该根据 $sessionId 参数来返回对应的会话数据的字符串形式。在你的会话驱动里,不论读写都不需要做任何数据序列化工作。因为 Laravel 会负责数据序列化的。

  • The write method should write the given $data string associated with the $sessionId to some persistent storage system, such as MongoDB, Dynamo, etc.

  • write 方法应该将 $sessionId 对应的 $data 字符串放置在一个持久化存储系统中。比如 MongoDB,Dynamo 等等。

  • The destroy method should remove the data associated with the $sessionId from persistent storage.

  • destroy 方法应该将 $sessionId 对应的数据从持久化存储系统中删除。

  • The gc method should destroy all session data that is older than the given $lifetime, which is a UNIX timestamp. For self-expiring systems like Memcached and Redis, this method may be left empty.

  • gc 方法应该将所有时间超过参数 $lifetime 的数据全都删除,该参数是一个 UNIX 时间戳。如果你使用的是类似 Memcached 或 Redis 这种有自主到期功能的存储系统,那该方法可以留空。

Once the SessionHandlerInterface has been implemented, we are ready to register it with the Session manager:

一旦 SessionHandlerInterface 实现完毕,我们就可以将其注册进会话管理器:

Session::extend('mongo', function($app)
    return new MongoHandler;

Once the session driver has been registered, we may use the mongo driver in our app/config/session.php configuration file.

注册完毕后,我们就可以在 app/config/session.php 配置文件里使用 mongo 驱动了。

Share Your Knowledge 分享你的知识

Remember, if you write a custom session handler, share it on Packagist!


results matching ""

    No results matching ""