【问题标题】:cakephp global variable for controllers控制器的 cakephp 全局变量
【发布时间】:2014-09-11 19:02:19
【问题描述】:

我正在尝试根据谁尝试登录我的页面来更改我的数据库连接。

我需要一种方法来保存正确的数据库名称,以便我的所有控制器都可以访问它。

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

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

在我的 AppModel 中,我有一个构造函数,它根据前面提到的 configure::read('campanyDB') 中的值设置数据库连接,我需要在我的所有控制器中使用 configure::write('CompanyDB',$myDbVar) 才能工作。

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

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;
    .....
}

【问题讨论】:

    标签: php cakephp global-variables


    【解决方案1】:

    最好的办法可能是使用配置文件:

    读写配置文件: http://book.cakephp.org/2.0/en/development/configuration.html#reading-and-writing-configuration-files

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

    示例文件:Config/dbconnections.php

    <?php
    $config = array(
        'MyAppDBs' => array(
            'company1' => 'connectionName',
            'company2' => 'differentConnectionName
        )
    );
    

    在您的引导文件中:

    Configure::load('dbconnections');
    

    应用中的任何位置:

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

    【讨论】:

    • 如果我错了,请纠正我。但这需要我预先填充所有公司的配置文件?我希望找到一个更动态的解决方案。但是谢谢:)
    • @user2596886 - 如果您尝试跟踪数据库连接,我假设您不想连接到数据库来检索要使用的连接。因此,文件似乎是下一个显而易见的选择。只需编写一个函数来在添加新公司时更新配置文件,瞧——“动态”。 :) 另一方面,如果您确实想连接到数据库以确定要使用哪个 OTHER 数据库连接,只需完全按照您正在做的事情进行操作,但是在 AppController 的 beforeFilter 中而不是在特定的控制器中进行操作。
    • 啊,我想我可以创建一个函数来更新该文件。会调查的。关于 Appcontroller,我不知道如何将在控制器中检索到的值传递到我的 appcontroller。
    • @user2596886 - 是否有需要在特定控制器中设置它的原因?
    • 在查询数据库之前,我不知道公司的名称。我有一个默认数据库,其中包含所有公司及其用户的列表。当用户尝试登录时,我首先查询默认数据库以确定他属于哪个公司。当我知道时,我可以切换数据库并正确登录用户+加载所有内容。
    【解决方案2】:

    我想如果你这样做

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

    在您的 appController 中,您可以使用任何控制器访问它

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多