简体   繁体   English

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

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

Here's a sample of _init method of Zend_Bootstrap from ZF manual . 这是ZF manual中Zend_Bootstrap_init方法的Zend_Bootstrap At the end there is return command: 最后是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?
    }
}

can be stored by the bootstrap 可以由引导程序存储

Why to return? 为什么要退货? Where the bootstrap stores it and why? 引导程序存储在哪里,为什么? What object calls this method and who gets the result? 哪个对象调用此方法,谁得到结果? And what will happen if no to return? 如果不返回将会怎样?

UPDATE: 更新:

On Available Resource Plugins page, in the section about View , they show the following way of initiation of Zend_View : 在“可用资源插件”页面上, 在关于View的部分中 ,它们显示了Zend_View的以下初始化方式:

Configuration options are per the Zend_View options . 配置选项是每个Zend_View选项的

Example #22 Sample View resource configuration Example#22示例视图资源配置

Below is a sample INI snippet showing how to configure the view resource. 以下是一个INI片段示例,显示了如何配置视图资源。

resources.view .encoding = "UTF-8" resources.view .encoding =“ UTF-8”

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

And it seems convenient and reasonable to initiate the View this way, from application.ini file, together with all the other resources they write about in the Zend_Application Quick Start page. application.ini文件以及它们在Zend_Application Quick Start页面中编写的所有其他资源开始以这种方式启动View似乎是方便和合理的。 But at the same time on the same Zend_Application Quick Start page they say that the View has to be initiated from the Bootstrap : 但同时在同一Zend_Application快速入门页面上,他们说必须从Bootstrap启动View

Now, we'll add a custom view resource. 现在,我们将添加一个自定义视图资源。 When initializing the view, we'll want to set the HTML DocType and a default value for the title to use in the HTML head. 初始化视图时,我们将要设置HTML DocType和要在HTML头中使用的标题的默认值。 This can be accomplished by editing your Bootstrap class to add a method: 这可以通过编辑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;
    }
}

And event more interesting with other resources, with Request for example here : 其他资源使事件更加有趣,例如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;
    }
}

So it seems that they offer a choice to initiate resources this or that way. 因此,似乎它们提供了一种以这种方式启动资源的选择。 But which one is right then? 但是,哪一个是正确的呢? Why they mix them? 他们为什么要混合它们? Which one is better to use? 哪个更好用?

In fact I can just remove all those lines about FC from my application.ini : 实际上,我可以从application.ini删除所有有关FC行:

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

and rewrite it something like this: 并重写它像这样:

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

What is the difference between application.ini way and _initResource way? application.ini方法和_initResource方法有什么_initResource Does this difference means something serious in work? 这种差异是否意味着工作很认真?

Although you would have got your answers after some time going through the zend manual. 尽管经过一遍zend手册,您可能会得到答案。 However I'll try to answer your questions. 不过,我会尽力回答您的问题。

1.Why to return? 1.为什么要退货?

Although there is no need to return and it is not mandatory.A return is only used to store the 尽管没有必要返回并且不是强制性的,但返回仅用于存储
variable in the zend container which is usually the Zend registry.This stored variable can be accessed by you anywhere the need be.If you don't return the only difference it would make is that you won't be able to fetch the variable anywhere. zend容器中的变量(通常是Zend注册表),您可以在需要的任何地方访问此存储的变量,如果不返回唯一的区别,那就是您将无法在任何地方获取该变量。 Found the following on the zend manual while writing the answer. 编写答案时,在zend手册上找到了以下内容。 It will surely help. 肯定会有所帮助。

As an example, consider a basic view resource: 例如,考虑一个基本的视图资源:

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 the bootstrap stores it and why? 2.引导程序存储在哪里,为什么?

I think the first one answers this. 我认为第一个回答了这个问题。

3.What object calls this method and who gets the result? 3.哪个对象调用此方法,谁得到结果?

An Application object calls the bootstrap usually(at the very beginning) and you can call individual resource methods too through the object. Application对象通常会(在开始时)调用引导程序,您也可以通过该对象调用各个资源方法。 But if you don't specify any parameters while calling the bootstrap method all the resource methods(eg _initView(),_initRouters()) will get executed . 但是,如果在调用引导程序方法时未指定任何参数,则所有资源方法(例如_initView(),_ initRouters())都将执行。

4.What will happen if no return. 4.如果没有回报会发生什么。

Answered by 1 I think. 我认为答案是1。

This contains almost all the answers you are looking for. 它几乎包含您要查找的所有答案。 http://framework.zend.com/manual/1.12/en/zend.application.theory-of-operation.html http://framework.zend.com/manual/1.12/zh/zend.application.theory-of-operation.html

Hope it helps. 希望能帮助到你。

UPDATE: 更新:

Saw your update.. Actually that is a matter of choice I think. 看到您的更新了。实际上,我认为这是选择的问题。

Where you want to define your resources is up to you. 您想在哪里定义资源取决于您。

Been working on a project where only the basic resources are defined in the application.ini file and most of the resources are loaded from the bootstrap... 一直在一个项目中工作,在该项目中,application.ini文件中仅定义了基本资源,并且大多数资源是从引导程序加载的...

Again its your choice however you would feel the comfort and flexibility when using the bootstrap for loading the resources.(eg defining custom routes etc). 同样,它也是您的选择,但是在使用引导程序加载资源时(例如定义自定义路线等),您会感到舒适和灵活。

That is what I feel. 那就是我的感觉。

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

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