控制器的cakephp全局变量

控制器的cakephp全局变量,php,cakephp,global-variables,Php,Cakephp,Global Variables,我正在尝试更改数据库连接,具体取决于试图登录到我的页面的用户 我需要的是一种保存正确db名称的方法,以便我的所有控制器都可以访问它 使用会话是可行的,但我怀疑它的安全性和/或良好实践。 如果我能从AccountsController在AppController中设置一个变量,那就太完美了。但基本上,任何能让我在所有控制器之间共享变量的方法 在我的AccountsController中,我在标准数据库中查询正确的名称。然后我使用configure::write('CompanyDB',$myDbV

我正在尝试更改数据库连接,具体取决于试图登录到我的页面的用户

我需要的是一种保存正确db名称的方法,以便我的所有控制器都可以访问它

使用会话是可行的,但我怀疑它的安全性和/或良好实践。 如果我能从AccountsController在AppController中设置一个变量,那就太完美了。但基本上,任何能让我在所有控制器之间共享变量的方法

在我的AccountsController中,我在标准数据库中查询正确的名称。然后我使用
configure::write('CompanyDB',$myDbVar)
。这对于这个控制器很好,但我不能在任何其他控制器中使用
configure::read('CompanyDB')

在我的AppModel中,我有一个构造函数,它根据
configure::read('campanyDB')
中的值设置db连接。如前所述,我需要在我的所有控制器中使用
configure::write('CompanyDB',$myDbVar)

在我的帐户模型中,我设置了
$specific=true
。这告诉AppModel它应该使用构造并更改db连接

class AccountsController extends AppController {

    public $helpers = array('Html', 'Form','Js');
    public $components = array('RequestHandler');
    var $uses = array('User', 'Company');
    public $name = 'Accounts';
    public $myDbVar='coolbeans';

    public function beforeFilter() {
        parent::beforeFilter();
        Configure::write( 'companyDB',$this->myDbVar);
    }   
}

class AppModel extends Model {
    var $specific = false;
    function __construct($id = false, $table = null, $ds = null) {
        if ($this->specific) {
            // Get saved company/database name
            $dbName = Configure::read('companyDB');
            // Get common company-specific config (default settings in database.php)
            $config = ConnectionManager::getDataSource('companySpecific')->config;
            // Set correct database name
            $config['database'] = $dbName;
            // Add new config to registry                 
            ConnectionManager::drop('companySpecific');
            ConnectionManager::create('companySpecific', $config);
            // Point model to new config
            $this->useDbConfig = 'companySpecific';
        }
        parent::__construct($id, $table, $ds);
    }
}

class Account extends AppModel {

    public $primaryKey = '_id';
    //var $useDbConfig = 'mongo';
    var $specific = true;
    .....
}

最好的选择可能是使用配置文件:

读取和写入配置文件:

基本思想是,在
Config/
目录中创建一个包含应用程序设置的文件,然后在引导中加载该配置文件,从而使这些变量在应用程序中的任何位置都可用

示例文件:
Config/dbconnections.php

<?php
$config = array(
    'MyAppDBs' => array(
        'company1' => 'connectionName',
        'company2' => 'differentConnectionName
    )
);
应用程序中的任意位置:

$whatever = Configure::read('MyAppDBs.companyDB');

我想如果你这么做

configure::write('CompanyDB', $myDbVar);
在appController中,然后您可以使用在任何控制器中访问它

configure::write('CompanyDB',$myDbVar);

因为所有控制器都继承了appController。

如果我错了,请纠正我。但这需要我预先填充所有公司的配置文件?我希望找到一个更具活力的解决方案。但是谢谢:)@user2596886-如果您试图跟踪数据库连接,我假设您不想连接到数据库来检索要使用的连接。因此,文件似乎是下一个明显的选择。只要编写一个函数,在您添加新公司时更新配置文件,瞧,“dynamic”。)另一方面,如果您确实希望连接到数据库以确定要使用哪个其他数据库连接,只需执行您正在执行的操作,但在AppController的beforeFilter中执行,而不是在特定的控制器中执行。啊,我想我可以创建一个函数来更新该文件。我会调查的。关于Appcontroller,我不知道如何将在控制器中检索到的值传递到我的Appcontroller。@user2596886-是否有理由需要在特定控制器中设置该值?在查询数据库之前,我不知道公司名称。我有一个默认数据库,其中包含所有公司及其用户的列表。当用户尝试登录时,我首先查询默认数据库以确定他所属的公司。当我知道这一点时,我可以切换数据库,让用户正确登录并加载所有内容。
configure::write('CompanyDB',$myDbVar);