2

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
 *
 */
4

1 回答 1

2

From my own staff:

" Ok, so the way seems to be to create a custom "session handler" model. Use the setting like this:

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

where the CustomSession.php model file has the configs. 'sessions' is a config I added to my config/database.php file:

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

"Unfortunately, the Cake documentation on this feature is terrible. The cake Session.handler, which actually defines the behavior of Session, is different than the Cake Session.handler.model. By default, the handler.model is AppModel (In Cake internals, see lib/Cake/Model/Session/DatabaseSession.php line 48 __construct() function - where it instantiates $this->_model. If you don't set a custom model, it will instantiate to AppModel. Try putting in a debug to see it in action.) The sparse documentation I linked to explains it as

The above will tell CakeSession to use the built in ‘database’ defaults, and specify that a model called CustomSession will be the delegate for saving session information to the database.

Also, see http://api20.cakephp.org/class/database-session _model property: "Reference to the model handling the session data"

So that's how/why it works....

-AWD"

于 2012-08-23T05:15:06.767 回答