8

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 ?

2 Answers 2

7

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');
5

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';
1
  • Shouldn't you not have to manually include those classes due to Zend's ability to auto-load classes? I'd typically avoid that at all costs. Jun 3, 2012 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.