CakePHP - 根据路由或url选择数据库配置? [英] CakePHP - select database config based on route or url?
问题描述
我正在使用一个小的CakePHP应用程序,受到以下约束(尴尬但不受我的控制):我需要它工作在两个相同的数据库,选择是基于URL。例如:
I'm working on a small CakePHP application that is subject to the following constraint (awkward but out of my control): I need it to work on either of two identical databases, with the choice being based on URL. For example:
http://www.example.com/myapp/foo/action/param
http://www.example.com/myapp/bar/action/param
第一个明显的解决方案是具有不同数据库配置的 myapp / foo
和 myapp / bar
上有两个相同的CakePHP应用程序。这有一个kludgy感觉,虽然,所以我试图找到一个优雅的方式创建一个单一的应用程序。
The first obvious solution is to have two identical CakePHP applications at myapp/foo
and myapp/bar
with different database configurations. This has a kludgy feel to it, though, so I'm trying to find an elegant way of creating a single application.
我考虑的方法是:定义路由,使 myapp / foo
和 myapp / bar
将与同一个控制器相关联。然后给我的 DATABASE_CONFIG
类一个构造函数:
The approach I'm considering is this: Define routes such that myapp/foo
and myapp/bar
will be associated with the same controller. Then give my DATABASE_CONFIG
class a constructor:
function __construct() {
$pathParts = explode('/', $_SERVER['REQUEST_URI']);
if (array_search('foo', $pathParts)) {
$this->default = $this->fooConfig;
} else if (array_search('bar', $pathParts)) {
$this->default = $this->barConfig;
}
}
$ c> fooConfig 和 barConfig
两个数据库。)我可以控制URL,所以我可以相信,在网址中是 foo
或 bar
的额外事件。
(Where of course I've defined fooConfig
and barConfig
for the two databases.) I do have control over the URL, so I can be confident that there won't be extraneous occurrences of foo
or bar
in the URL.
我的问题是这样:有更简单,更优雅的方式来处理这种奇怪的情况吗?也许在 AppModel
和/或 AppController
中有些东西?虽然我摆脱了重复的代码,我不能动摇的感觉,我用另一个替换一个代码气味。
My question is this: Is there a simpler, more elegant way of handling this odd situation? Maybe something in AppModel
and/or AppController
? Although I'm getting rid of duplicated code, I can't shake the feeling that I'm replacing one code smell with another.
推荐答案
有几种方法可以做到这一点,这里有一个。
There are a few ways to do this, here is one.
写一个你总是匹配的甜蜜的自定义路线:
Write a sweet custom route in which you always match:
Router :: connect('/:ds / *',array(),array('routeClass'=>'SweetDbRoute'));
然后让SweetDbRoutes设置一个类变量,你可以在任何地方使用,包括在模型构造函数中。然后它应该失败,所以你不实际调整请求。
Then have SweetDbRoutes set a class variable you can use everywhere, including in your model constructors. Then it should fail so you don't actually adjust the request.
App::import('SweetDbClass', array('file' => '/path/to/your/sweet_db_class.php'));
class SweetDbRoute extends CakeRoute {
// put your failing route code here, but use your SweetDbClass to track datasource ...
// see http://book.cakephp.org/view/1634/Custom-Route-classes
}
然后在您的AppModel中:
Then in your AppModel:
App::import('SweetDbClass', array('file' => '/path/to/your/sweet_db_class.php'));
class AppModel extends Model {
public function __construct($id = false, $table = null, $ds = null) {
$ds = SweetDbClass::$ds;
parent::__construct($id, $table, $ds);
}
}
这篇关于CakePHP - 根据路由或url选择数据库配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!