Zend Framework: What is the difference between no

2019-04-17 12:04发布

问题:

I'm new to this framework. I've been searching on how to define a custom route and I found this code:

protected function _initRouter(){
    $front = Zend_Controller_Front::getInstance();
    $router = $front->getRouter();

    $router->addRoute(
            'listOnIndex',     
            new Zend_Controller_Router_Route('/list', array('controller' => 'index', 'action' => 'list'))
        );

return $router;
}

I've tried removing the return value and it still works. Why is that?
Is it really necessary to return the instance? Thank you very much for the help! :)

回答1:

If you return a value from a boostrap method named _initSomeResource(), then the return value is "stored" in the bootstrap, for possible retrieval later as:

$bootstrap->getResource('SomeResource')

Since the bootstrap class is passed as an invoke argument to controllers, you are able to access these resources in controllers using:

$bootstrap = $this->getInvokeArg('bootstrap');
$someResource = $bootstrap->getResource('SomeResource');

In your circumstance, the resource you are configuring is the router and you didn't need to access it later on. So in this case, failing to return it from _initRouter() didn't hurt you any.



回答2:

In this case it is not neccessary to return the $router instance.

Since you are using a singleton pattern to retrieve the router object and configure a new route via the addRoute method, the added routes are stored savely for further processing (as long as only this instance is used).