简体   繁体   中英

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 ?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM