开发者

How to get resource in controller action?

开发者 https://www.devze.com 2023-01-01 04:26 出处:网络
How to get resource in controller action? Resource db was initialized in application.ini. class IndexController extends Zend_Controller_Acti开发者_开发知识库on

How to get resource in controller action? Resource db was initialized in application.ini.

class IndexController extends Zend_Controller_Acti开发者_开发知识库on
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // I want db resource here
    }
}


Try and see if this works:

$this->getFrontController()->getParam('bootstrap')->getResource('db') 


UPDATE : While this solution works, it is NOT a recommended practice. Please, read comment by @Brian M. below.

You can use Zend_Registry. Initialize the database connection in the bootstrap and store it in the registry:

// set up the database handler
// (...)
Zend_Registry::set('dbh', $dbh);

Then you can retireve it from anywhere else:

$dbh = Zend_Registry::get('dbh');


In answer to a similar question on Nabble, Matthew Weier O'Phinney (Mr Zend Framework 1) suggests using this form:

$this->getInvokeArg('bootstrap')->getResource('db'); 

So, in the context of this question, it would be something like:

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // db resource here
        $db = $this->getInvokeArg('bootstrap')->getResource('db'); 
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消