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

安稳与你 提交于 2019-12-06 17:04:03

问题


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



来源:https://stackoverflow.com/questions/6744035/zend-framework-what-is-the-difference-between-no-return-value-and-returning-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!