Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework - get front controller from bootstrap?

I'm trying to implement routes into my bootstrap file with this code ;

protected function _initRoutes()
{
    $router = $this->getResource('frontController')->getRouter();

    $router->addRoute(
        'profil',
        new Zend_Controller_Router_Route
        (
            'profil/:username',
            array
            (
                'controller' => 'users',
                'action'    => 'profil'
            )
        )
    );
}

but it doesn't work since I get 'Call to a member function getRouter() on a non-object in...' error.

How can I get the controller from bootstrap ?

like image 429
Kemo Avatar asked Nov 29 '09 17:11

Kemo


2 Answers

I believe that your problem is that where you are calling

$this->getResource('frontController')->getRouter()

the FrontController resource has not yet been initialized.

I called the same method in this fashion (which won't work in Zend Framework 2.0 but works for now):

Zend_Controller_Front::getInstance()->getRouter();

Alternatively you can make certain that your front controller is initialized like this:

$this->bootstrap('FrontController');

$front =  $this->getResource('FrontController');
like image 70
Noah Goodrich Avatar answered Nov 09 '22 09:11

Noah Goodrich


You could try:

$front  = Zend_Controller_Front::getInstance();
$router = $front->getRouter();

And if you run into any issues these are most likely your culprits:

require_once 'Zend/Controller/Front.php';    
require_once 'Zend/Controller/Router/Route.php';
like image 23
Corey Ballou Avatar answered Nov 09 '22 09:11

Corey Ballou