Zend 框架布局 [英] Zend Framework Layout

查看:37
本文介绍了Zend 框架布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Zend Framework 开始,我想了解 Bootstrap 文件.我已经了解到所有 _init 方法都是默认执行的,但对我来说似乎很困惑.反正这不是我想问的.

I'm beginning with Zend Framework and I would like to understand Bootstrap file. I've learned all _init methods are executed by default but it seems confusing to me. Anyway that is not what I would like to ask.

遇到了 $this->bootstrap('layout'); 操作,我不确定我是否理解这一点.这是 application.ini 文件中的 resource.layout 变量吗?我想真正深入了解引导过程.

A came around the $this->bootstrap('layout'); action and I'm not sure if I understand this. Is this the resource.layout variable in application.ini file? I would like to really understand the bootstrap process in deep.

我要你一步一步地解释.提前致谢!

这是我的引导文件:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoload()
    {
        $moduleLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath' => APPLICATION_PATH
        ));
        return $moduleLoader;
    }

    function _initViewHelpers()
    {
        $this->bootstrap('layout');

        $layout = $this->getResource('layout');
        $view = $layout->getView();
        $view->doctype('XHTML1_STRICT');
        $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
        $view->headTitle()->setSeparator(' - ');
        $view->headTitle('Zend Framework Tutorial');
    }
}

推荐答案

来自 application.ini

resources.layout[] = 

相当于:

_initLayout() {}

Bootstrap.php

两者都在初始化新对象,这个对象被设置为一个bootstrap参数,它是一些应用资源的容器(_init方法返回的值).

Both of them are initializing new object, and this object is set as a bootstrap param, which is a container for some application resources (values returned by _init methods).

这里是一一执行的,所以为了确保一个资源在当前资源之前被初始化,你强制顺序,使用:

There are executed one by one, so to ensure one resource is initialized before the the current one, you force the order, using:

_initSomeResource() {
    $this->bootstrap('otherResource');
    // ..
    return $someValue; 
}

_initOtherResource() {
    // .. 
}

所以实例化资源的顺序是:

So the order of instantiating of the resources is:

  1. otherResource
  2. someResource

现在,您还可以使用:

$bootstrap->getParam('someResource'); // returns $someValue

请注意,当您尝试在每个之前执行彼此时,您可能会遇到 Circular Dependency 错误.

Note, that you may encounter Circular Dependency error, when you try to execute each other before each one.

您可以使用任意数量的 _init 方法,但为了使它们可重用,您可以将它们分离到它们自己的类中,实现 Zend_Application_Resource_Abstract 类.

You may use as many _init methods you need, but to make them reusable, you may separate them to their own class, implementing Zend_Application_Resource_Abstract class.

有一些开箱即用应用程序资源,您可以在 Zend/Application/Resource 目录中找到它们.这些是您从 application.ini 引用的资源,即:

There are some out of the box application resources, which you may find in Zend/Application/Resource directory. These are the resources, you are refering from application.ini, i.e.:

resources.view.encoding = "utf-8" ; in application.ini
Zend/Application/Resource/View.php (path to the resource class)
Zend_Application_Resource_View::setEncoding('UTF-8'); // equivalent pseudocode

希望现在更清楚了.

这篇关于Zend 框架布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆