返回在ZF Bootstrap类的_init方法中做什么?

问题描述:

下面是Zend_Bootstrap_init方法的示例从ZF manual。在到底有return命令:返回在ZF Bootstrap类的_init方法中做什么?

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    protected function _initView() 
    { 
     // Initialize view 
     $view = new Zend_View(); 
     $view->doctype('XHTML1_STRICT'); 
     $view->headTitle('My First Zend Framework Application'); 

     // Add it to the ViewRenderer 
     $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
      'ViewRenderer' 
     ); 
     $viewRenderer->setView($view); 

     // Return it, so that it can be stored by the bootstrap 
     return $view; // Why return is here? 
    } 
} 

可以通过引导

为什么返回储存在哪里? Bootstrap在哪里存储它,为什么?什么对象调用这个方法,谁得到结果?如果没有返回会发生什么?

UPDATE:

上的可用资源插件页面,in the section about View,他们表现出的Zend_View发起的方式如下:

配置选项是每the Zend_View options

实施例#22样品查看资源配置

下面是一个示例代码段INI示出了如何配置视图 资源。

resources.view .encoding = “UTF-8”

resources.view .basePath = APPLICATION_PATH “/视图/”

而且似乎方便合理,以启动View这种方式,从application.ini文件,以及他们在Zend_Application快速启动页面中写入的所有其他资源。但在同一时间同一Zend_Application快速启动页面上,他们说,View必须从Bootstrap启动:

现在,我们将添加自定义视图的资源。当初始化视图 时,我们需要设置HTML DocType和标题 的缺省值以在HTML头中使用。这可以通过编辑您的 Bootstrap类来完成,以添加一个方法:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    protected function _initView() 
    { 
     // Initialize view 
     $view = new Zend_View(); 
     $view->doctype('XHTML1_STRICT');  // the same operations, I can set this in application.ini 
     $view->headTitle('My First Zend Framework Application'); // and this too 

     // Add it to the ViewRenderer 
     $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
      'ViewRenderer' 
     ); 
     $viewRenderer->setView($view); 

     // Return it, so that it can be stored by the bootstrap 
     return $view; 
    } 
} 

和事件更有趣的与其他资源,以Request例如here

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    protected function _initRequest() 
    { 
     // Ensure the front controller is initialized 

     $this->bootstrap('FrontController'); // why to initialized FC here if it is going to be initialized in application.ini anyway like resource.frontController.etc? 

     // Retrieve the front controller from the bootstrap registry 
     $front = $this->getResource('FrontController'); 

     $request = new Zend_Controller_Request_Http(); 
     $request->setBaseUrl('/foo'); 
     $front->setRequest($request); 

     // Ensure the request is stored in the bootstrap registry 
     return $request; 
    } 
} 

如此看来,他们提供了一种选择来启动这种或那种方式的资源。但是哪一个是对的呢?他们为什么混合使用?哪一个更好用?

其实我可以从我application.ini删除所有这些有关FC线:

resources.frontController.baseUrl = // some base url 
resources.frontController.defaultModule = "Default" 
resources.frontController.params.displayExceptions = 1 

并重写它是这样的:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
    { 
     protected function _initFrontController() 
     { 

      $this->bootstrap('FrontController'); 
      $front = $this->getResource('FrontController'); 
      $front->set ... 
      $front->set ... // and here I set all necessary options 

      return $front; 
     } 
    } 

是什么application.ini方式_initResource方式之间的区别?这种差异是否意味着工作中的严重问题?

+0

另外,请参阅http://stackoverflow.com/a/6744122/131824 –

尽管在经过zend手册一段时间之后你会得到答案。 但是,我会尽力回答你的问题。

1.为什么要退货?

虽然无需返回,它不是mandatory.A回报仅用于存储在Zend容器通常是在Zend registry.This存储的变量可以访问由您随时随地的需要
变量be.If你没有返回唯一的区别就是你将无法在任何地方获取变量。 在写答案时在zend手册中发现了以下内容。它一定会有所帮助。

作为一个例子,考虑一个基本看法资源:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    protected function _initView() 
    { 
     $view = new Zend_View(); 
     // more initialization... 

     return $view; 
    } 
} 
You can then check for it and/or fetch it as follows: 

// Using the has/getResource() pair: 
if ($bootstrap->hasResource('view')) { 
    $view = $bootstrap->getResource('view'); 
} 

// Via the container: 
$container = $bootstrap->getContainer(); 
if (isset($container->view)) { 
    $view = $container->view; 
} 

2.Where引导存储它,为什么?

我认为第一个答案是这样的。

3.什么对象调用这个方法,谁得到结果?

应用程序对象通常会在初始阶段调用引导程序,您也可以通过对象调用各个资源方法。但是,如果在调用引导方法时未指定任何参数,则所有资源方法(例如_initView(),_ initRouters())将执行 。

4.如果没有返回会发生什么。

由回答1我认为。

这包含您正在寻找的几乎所有答案。 http://framework.zend.com/manual/1.12/en/zend.application.theory-of-operation.html

希望它有帮助。

UPDATE:

看到你更新.. 其实这是一个选择的问题,我认为。

你想定义资源的地方取决于你。

一直在那里只有基本的资源在的application.ini文件中定义,大部分资源都从引导加载的项目...

同样的你的选择,但是你会感到舒适和使用引导程序加载资源时的灵活性(例如定义自定义路由等)。

这就是我的感受。

+0

非常感谢。这真的有帮助。来自Zend的手册很难阅读。而我不明白的是为什么要初始化资源两次。请参阅我的问题的更新,这些小的评论不允许完全代表我的意思。 – Green