简体   繁体   中英

Zend Framework: What is the difference between no return value and returning the resource instance in a bootstrap init?

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: :)

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.

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).

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