php 如何在 Zend Framework 的控制器插件中获取引导程序资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2970548/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a bootstrap resource in a controller plugin in Zend Framework
提问by y2k
protected function _initDatabase()
{
     $params = array(
            'host'     => '',
            'username' => '',
            'password' => '',
            'dbname'   => '',
        );
    $database = Zend_Db::factory('PDO_MYSQL', $params);
    $database->getConnection();
    return $database;
}
.
.
class App_Controller_Plugin_Test extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Http $request)
    {
        // how i get database?
    }
}
回答by Bill Karwin
You can always get a reference to the front controller:
您始终可以获得对前端控制器的引用:
$front = Zend_Controller_Front::getInstance();
From that you can get the bootstrap:
从中你可以得到引导程序:
$bootstrap = $front->getParam("bootstrap");
From the bootstrap you can get bootstrap plugins:
从引导程序中,您可以获得引导程序插件:
if ($bootstrap->hasPluginResource("database")) {
      $dbResource = $bootstrap->getPluginResource("database");
}
$db = $dbResource->getDatabase();
But that's a lot of extra plumbing!
但这是很多额外的管道!
Honestly, you'd be better off storing the database adapter object in the registry during your bootstrap:
老实说,最好在引导过程中将数据库适配器对象存储在注册表中:
protected function _initDatabase()
{
     $params = array(
            'host'     => '',
            'username' => '',
            'password' => '',
            'dbname'   => '',
        );
    $database = Zend_Db::factory('PDO_MYSQL', $params);
    $database->getConnection();
    Zend_Registry::set("database", $database);
    return $database;
}
Then you can get the database adapter anywhere:
然后你可以在任何地方获取数据库适配器:
Zend_Registry::get("database");
See also my answer to What is the “right” Way to Provide a Zend Application With a Database Handler
另请参阅我对使用数据库处理程序提供 Zend 应用程序的“正确”方法是什么的回答
回答by typeoneerror
Too bad there's nothing like Zend_Controller_Action's getInvokeArg("bootstrap")in a plugin. You could always get the bootstrap reference through the front controller:
太糟糕了,插件中没有像Zend_Controller_Action'这样的东西getInvokeArg("bootstrap")。您始终可以通过前端控制器获取引导程序参考:
$db = Zend_Controller_Front::getInstance()->getParam("bootstrap")->getResource("database");
But what I usually do is
但我通常做的是
Zend_Registry::set('database', $database);
and then in your plugin:
然后在你的插件中:
try 
{
    $db = Zend_Registry::get('database');
}
catch (Zend_Exception $e)
{
   // do stuff
}
Easier, and database can be retrieved pretty much anywhere in the application.
更容易,并且几乎可以在应用程序的任何位置检索数据库。
回答by allnightgrocery
[I need to check this against some working code on another machine. I believe it's something like this...]
[我需要对照另一台机器上的一些工作代码进行检查。我相信它是这样的......]
$db = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('db');
回答by bigben3333
$db = Zend_Db_Table::getDefaultAdapter();
$db = Zend_Db_Table::getDefaultAdapter();

