【问题标题】:What does a return do in _init methods of ZF Bootstrap class?ZF Bootstrap 类的 _init 方法中的 return 有什么作用?
【发布时间】:2012-10-15 21:56:03
【问题描述】:

这是来自ZF manualZend_Bootstrap_init 方法示例。最后有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?
    }
}

可以通过引导存储

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

更新:

在可用资源插件页面in the section about View,它们显示了Zend_View的以下启动方式:

配置选项来自the Zend_View options

示例 #22 示例视图资源配置

下面是一个示例 INI sn-p 显示如何配置视图 资源。

resources.view.encoding = "UTF-8"

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

application.ini 文件中启动View 似乎既方便又合理,以及他们在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 方式有什么区别?这种差异是否意味着工作中的严重问题?

【问题讨论】:

标签: zend-framework zend-application zend-app-bootstrap


【解决方案1】:

虽然您在阅读 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.bootstrap 将其存储在哪里以及为什么?

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

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

Application 对象通常(在最开始时)调用引导程序,您也可以通过该对象调用各个资源方法。但是如果你在调用 bootstrap 方法时没有指定任何参数,所有的资源方法(例如 _initView(),_initRouters())都会被执行 .

4.如果不返回会怎样。

我认为由 1 回答。

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

希望对你有帮助。

更新:

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

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

一直在做一个项目,在application.ini文件中只定义了基本资源,大部分资源都是从引导加载的……

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

这就是我的感觉。

【讨论】:

  • 非常感谢。这真的很有帮助。 Zend 的手册很难阅读。而我不明白的是为什么要初始化资源两次。请查看我的问题的更新,这些小 cmets 不能完全代表我的意思。
猜你喜欢
  • 2012-03-12
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 2012-07-06
相关资源
最近更新 更多