关于ZendFramework1.9基础知识的几个问题。
_initAutoload()
或_initDoctype()
方法。那么这些方法什么时候被隐式调用呢?
由于在index.php中,我已经将配置文件'/configs/application.ini'
“传递”给Zend_Application构造函数,是否有任何方法在其他地方检索配置条目?resources.db.*
)。因此,在同一个application.ini文件中,我有,例如。
custdb.adapter = "PDO_MYSQL“custdb.params.host = "localhost”custdb.params.username =“用户名”custdb.params.password =“密码”custdb.params.dbname = "custdb“
管理DB适配器的最佳实践是什么?
是否可以(也应该)在index.php或Bootstrap.php中创建DB适配器,并在需要时在其他地方检索它(以及如何)?
或者可以(而且我应该)在其他地方检索配置条目(如何检索?)并在需要时实例化DB适配器?谢谢!
发布于 2010-01-26 10:30:33
这里有几个答案。
2a。所有请求都重定向到index.php。这是用mod_rewrite完成的,并在.htaccess文件中指定。
2b。引导程序调用任何以_init
为前缀的方法。请参阅Zend框架-操作理论
2c。是。Zend::Config。您可以在Zend::Registry
中存储一个实例,以便于访问。例:
$config = new Zend_Config((APPLICATION_PATH . '/configs/application.ini'));
$application = new Zend_Application(APPLICATION_ENV, $config);
Zend_Registry::set('config', $config);
检查API引用,查看这两个类的构造函数。
我不认为快速启动是有帮助的。我建议你买本书。我喜欢基思·波普的“ZendFramework1.8Web应用程序开发”。
发布于 2010-01-26 15:42:39
为了回答问题3,ZF使用Application Zend_Application_Resource_Db
来摄取配置并创建一个数据库适配器实例。
如果您需要多个数据库是一个环境问题,那么您可以很容易地在application.ini文件中命名您的DB参数。
[production]
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = user
resources.db.params.password = pass
resources.db.params.dbname = production_db
[staging : production]
resources.db.params.dbname = staging_db
[development : production]
resources.db.params.dbname = development_db
在本例中,我们将在[production]
部分中设置公共信息,并为我们的暂存和开发环境覆盖它。应用程序的.htaccess
中的环境变量控制应用的配置
如果您需要在一个应用程序中访问多个数据库,那么我建议滚动您自己的,并创建某种结构来容纳多个连接。
这并不像看上去那么困难。阅读它,这里并创建Zend_Application_Resource_ResourceAbstract
的子类。使用这个类,您可以轻松地获取配置文件中的resources.*
:
$this->getBootstrap()-getResource('mydb')`
然后您可以通过引导对象访问您的插件:
$bootstrap->getPluginResource('mydb')
希望这能有所帮助。
编辑:我忘了提到,如果您将资源插件作为application.ini的一部分,Zend_Application引导程序将自动知道将其作为引导过程的一部分,因此您不需要在引导文件中定义任何_init()
方法。就像那样的魔法。
此外,就存储适配器实例而言,我可能只是用户Zend_Registry。
发布于 2010-01-28 01:40:42
谢谢你的回复!它确实帮助我理解了ZF的概念。
为了加深了解,我还浏览了参考资料和源代码,这就是我采用的方法:
在我的application.ini中有:
custom.db.customers.adapter = "PDO_MYSQL"
custom.db.customers.params.host = "localhost"
custom.db.customers.params.username = "username"
custom.db.customers.params.password = "password"
custom.db.customers.params.dbname = "custdb"
然后,在我的Bootstrap.php中我有:
protected function _initCustomDbCustomers()
{
$config = $this->getOptions();
$cfgCustom = $config['custom'];
if (null != $cfgCustom)
{
$cfgCustomDb = $cfgCustom['db'];
if (null != $cfgCustomDb)
{
$cfgCustomDbCustomers = $cfgCustom['customers'];
if (null != $cfgCustomDbCustomers)
{
$resrcCustomDbCustomers = new Zend_Application_Resource_Db($cfgCustomDbCustomers);
return $resrcCustomDbCustomers
}
}
}
}
当然,在我的index.php中,我调用:
$application->bootstrap();
$application->run();
然后,在需要获得DB适配器的控制器中,我需要:
$bootstrap = $this->getInvokeArg('bootstrap');
$resrcCustomDbCustomers = $bootstrap->getResource('customDbCustomers');
$adpCustomDbCustomers = $resrcCustomDbCustomers->getDbAdapter();
// Do Stuffs With DB Adapter
这是一种好/坏的做事方式吗?还有什么我该注意的陷阱吗?
谢谢!
https://stackoverflow.com/questions/2138547
复制相似问题