配置Cake 2.x以使用不同的数据库进行会话

时间:2012-07-26 21:52:51

标签: php cakephp cakephp-2.0

Cake 1.x有一个允许Session.database的配置选项,以便可以将完全不同的机器/数据库用于会话表。在Cake 2.0中,默认的core.php配置文件似乎不再具有该选项。

如果不编写自定义会话处理程序,这仍然可以吗?

/**
 * Session configuration.
 *
 * Contains an array of settings to use for session configuration. The defaults key is
 * used to define a default preset to use for sessions, any settings declared here will override
 * the settings of the default config.
 *
 * ## Options
 *
 * - `Session.cookie` - The name of the cookie to use. Defaults to 'CAKEPHP'
 * - `Session.timeout` - The number of minutes you want sessions to live for. This timeout is handled by CakePHP
 * - `Session.cookieTimeout` - The number of minutes you want session cookies to live for.
 * - `Session.checkAgent` - Do you want the user agent to be checked when starting sessions? You might want to set the
 *    value to false, when dealing with older versions of IE, Chrome Frame or certain web-browsing devices and AJAX
 * - `Session.defaults` - The default configuration set to use as a basis for your session.
 *    There are four builtins: php, cake, cache, database.
 * - `Session.handler` - Can be used to enable a custom session handler.  Expects an array of of callables,
 *    that can be used with `session_save_handler`.  Using this option will automatically add `session.save_handler`
 *    to the ini array.
 * - `Session.autoRegenerate` - Enabling this setting, turns on automatic renewal of sessions, and
 *    sessionids that change frequently. See CakeSession::$requestCountdown.
 * - `Session.ini` - An associative array of additional ini values to set.
 *
 * The built in defaults are:
 *
 * - 'php' - Uses settings defined in your php.ini.
 * - 'cake' - Saves session files in CakePHP's /tmp directory.
 * - 'database' - Uses CakePHP's database sessions.
 * - 'cache' - Use the Cache class to save sessions.
 *
 * To define a custom session handler, save it at /app/Model/Datasource/Session/<name>.php.
 * Make sure the class implements `CakeSessionHandlerInterface` and set Session.handler to <name>
 *
 * To use database sessions, run the app/Config/Schema/sessions.php schema using
 * the cake shell command: cake schema create Sessions
 *
 */

1 个答案:

答案 0 :(得分:2)

来自我自己的员工:

” 好的,所以似乎是创建一个自定义的“会话处理程序”模型。使用如下设置:

Configure::write('Session.handler.model', 'CustomSession');

CustomSession.php模型文件具有配置。 'sessions'是我添加到config / database.php文件中的配置:

<?php
class CustomSession extends AppModel {
    var $useDbConfig = 'sessions';
    var $useTable = 'cake_sessions';
}

“不幸的是,关于此功能的Cake文档非常糟糕。实际定义Session行为的蛋糕Session.handler与Cake Session.handler.model不同。默认情况下,handler.model是AppModel(在Cake内部,请参阅lib / Cake / Model / Session / DatabaseSession.php第48行__construct()函数 - 它实例化$ this-&gt; _model。如果您没有设置自定义模型,它将实例化为AppModel。尝试进行调试以查看它的实际效果。)我链接的稀疏文档将其解释为

以上将告诉CakeSession使用内置的“数据库”默认值,并指定名为CustomSession的模型将委托将会话信息保存到数据库。

另请参阅http://api20.cakephp.org/class/database-session _model属性:“对处理会话数据的模型的引用”

这就是它的工作方式/原因......

-AWD“