【发布时间】:2012-10-15 21:56:03
【问题描述】:
这是来自ZF manual 的Zend_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