How to get a resource in a controller action?

How to get a resource in a controller action? The db resource 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
source share
3 answers

Try and see if this works:

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

UPDATE . Although this solution works, it is NOT a recommended practice. Please read @Brian M.'s comment below.

You can use Zend_Registry . Initialize the database connection in the boot block and save it in the registry:

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

Then you can remove it from another place:

$dbh = Zend_Registry::get('dbh');
+1
source

In response to a similar question in Nabble , Matthew Weyer O'Pinnie (Mr. Zend Framework 1) suggests using this form:

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

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

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
source

Source: https://habr.com/ru/post/1747552/


All Articles