Zend Framework: What is the difference between no

2019-04-17 11:35发布

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

2条回答
Animai°情兽
2楼-- · 2019-04-17 11:59

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

查看更多
Evening l夕情丶
3楼-- · 2019-04-17 12:02

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.

查看更多
登录 后发表回答