简体   繁体   中英

How to use sessions on the database.php file

What I would want to do is to switch between development_db and local_db. in the index page, there is a list of databases that i can choose from if i click a database name on the list, the whole app will change to that specific database that i choose. What I did was, I set a session variable in my controller that will be read when the database.php loads up, but my problem is that I can't use

$this->Session->read(); 

on the database.php file. is there a way that the database.php file can read my session variables?

You can use Cake's Configure::write() / Configure::read() in your Controller, then access it in the database.php file - something like this:

//controller
Configure::write('DEV', true);

//database
function __construct() {
    $dev = Configure::read('DEV');
    if($dev)  $this->default = $this->dev;
}

function DATABASE_CONFIG() {
    $this->__construct();
}

Another option:

The other way (the way we currently do it) is to check for $_SERVER['DEV'] . Just set it in your httpd-vhosts.conf file (if using wamp):

<VirtualHost mysite>
    DocumentRoot C:\wamp\www\mysite
    ServerName mysite
    SetEnv DEV 1
</VirtualHost>

This is mostly from memory, so pardon if there are slight issues. I assume this is not a secure method, so if you're working with sensitive information, I would look more into the do's and dont's...etc, but - it works fine for our non-sensitive purposes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM