繁体   English   中英

ZF Bootstrap类的_init方法中的return有什么作用?

[英]What does a return do in _init methods of ZF Bootstrap class?

这是ZF manual中Zend_Bootstrap_init方法的Zend_Bootstrap 最后是return命令:

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?
    }
}

可以由引导程序存储

为什么要退货? 引导程序存储在哪里,为什么? 哪个对象调用此方法,谁得到结果? 如果不返回将会怎样?

更新:

在“可用资源插件”页面上, 在关于View的部分中 ,它们显示了Zend_View的以下初始化方式:

配置选项是每个Zend_View选项的

Example#22示例视图资源配置

以下是一个INI片段示例,显示了如何配置视图资源。

resources.view .encoding =“ UTF-8”

resources.view .basePath = APPLICATION_PATH“ / views /”

application.ini文件以及它们在Zend_Application Quick Start页面中编写的所有其他资源开始以这种方式启动View似乎是方便和合理的。 但同时在同一Zend_Application快速入门页面上,他们说必须从Bootstrap启动View

现在,我们将添加一个自定义视图资源。 初始化视图时,我们将要设置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可以在这里

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方法有什么_initResource 这种差异是否意味着工作很认真?

尽管经过一遍zend手册,您可能会得到答案。 不过,我会尽力回答您的问题。

1.为什么要退货?

尽管没有必要返回并且不是强制性的,但返回仅用于存储
zend容器中的变量(通常是Zend注册表),您可以在需要的任何地方访问此存储的变量,如果不返回唯一的区别,那就是您将无法在任何地方获取该变量。 编写答案时,在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.引导程序存储在哪里,为什么?

我认为第一个回答了这个问题。

3.哪个对象调用此方法,谁得到结果?

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

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

我认为答案是1。

它几乎包含您要查找的所有答案。 http://framework.zend.com/manual/1.12/zh/zend.application.theory-of-operation.html

希望能帮助到你。

更新:

看到您的更新了。实际上,我认为这是选择的问题。

您想在哪里定义资源取决于您。

一直在一个项目中工作,在该项目中,application.ini文件中仅定义了基本资源,并且大多数资源是从引导程序加载的...

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

那就是我的感觉。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM