[英]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.