[英]ZF2 How to put adapter into registry
我在我的應用程序中使用zend類(沒有mvc)。 我想將適配器實例放入容器中,並在其他類中使用它。 這就是我現在所擁有的。
load.php
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'pdo_mysql',
'database' => DB_PREFIX.DB_NAME,
'username' => DB_USER,
'password' => DB_PW
));
如何在其他班級訪問$ adapter?
OtherClass.php
public class OtherClass{
function __construct() {
//$adapter is needed here
$this->_model = get_class($this);
$this->sql = new Sql\Sql($this->dbo);
}
}
以前在ZF1中有一個Zend :: Registry,可在其中設置變量。 然后,稍后通過調用get方法再次訪問它。 但是,我在ZF2中找不到類似的情況。 我嘗試使用ServiceManager,但在我的情況下似乎無法正常工作。
感謝任何專家的建議。
ZF2沒有像ZF1那樣的Registry
。 相反,我們有所謂的ServiceManager
。 您應該使用此SM將依賴項注入服務中。
您可以在此處閱讀有關SM的更多信息-> http://zf2.readthedocs.org/en/latest/modules/zend.service-manager.intro.html
我建議在github-> https://github.com/ZF-Commons/ZfcUser上簽出ZfcUser並簽出其Module.php文件。 它可以幫助您更好地了解工作示例如何使用SM來處理注入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.