开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜