views:

98

answers:

2

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

class IndexController extends Zend_Controller_Action
{

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

    public function indexAction()
    {
        // I want db resource here
    }
}
+3  A: 

Try and see if this works:

$this->getFrontController()->getParam('bootstrap')->getResource('db') 
Alistair
So was rightly:$db = $this->getInvokeArg('bootstrap')->getResource('db');
Ponomarev Dmitry
A: 

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');
nuqqsa
I don't think using the registry like this is a smart idea. ZF gives you a way to access resources, and relying on a global registry can be problematic. If you want easier access to resources, try using the injector approach here: http://weierophinney.net/matthew/archives/235-A-Simple-Resource-Injector-for-ZF-Action-Controllers.html
Bryan M.
This is a far more elegant approach, thanks for the hint :)
nuqqsa