2013-03-27 59 views
0

變量不會出現在一個視圖,視圖助手不起作用: 比如我在控制器定義變量:爲什麼應用程序以這種方式工作?從控制器

$this->view->title = "Title"; 

,並在視圖中打印:

echo $this->title; 

變量「標題」的內容不會出現在頁面中。

這裏是我的引導文件:

use ZFBootstrap\View\Helper\Navigation\Menu; 

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
protected function _initAutoload() { 
    require_once '../application/views/helpers/LoggedInAs.php';//need to include a view-helper 
    require_once '../application/plugins/AuthPlugin.php'; 
    require_once '../application/configs/configAcl.php'; 
    $frontController = Zend_Controller_Front::getInstance(); 
    $frontController->registerPlugin($auth = new AuthPlugin()); 
    $frontController->getRouter()->addDefaultRoutes(); 
    $autoloader = Zend_Loader_Autoloader::getInstance(); 
    $autoloader->setFallbackAutoloader(true); 

    $this->bootstrap('layout'); 
    $layout=$this->getResource('layout'); 
    $view=$layout->getView(); 
    $config = new Zend_Config_Xml(APPLICATION_PATH. '/configs/navigation.xml', 'nav'); 
    $container = new Zend_Navigation($config); 
    $view->registerHelper(new Menu(), 'menu'); 
    $view->navigation($container); 
    Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl); 
    $auth = Zend_Auth::getInstance(); 
    $identity = $auth->getIdentity(); 
    $role = ($auth->hasIdentity() && !empty($auth->getIdentity()->role)) 
      ? $auth->getIdentity()->role : 'guest'; 
    Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($role); 
    $view->navigation()->menu()->render(); 
    $view->navigation()->menu()->setMinDepth(1)->setUlClass('nav'); 
} 

相反,如果我用這個代碼

use ZFBootstrap\View\Helper\Navigation\Menu; 

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
protected function _initAutoload() { 
    require_once '../application/plugins/AuthPlugin.php'; 
    require_once '../application/configs/configAcl.php'; 
    $frontController = Zend_Controller_Front::getInstance(); 
    $frontController->registerPlugin($auth = new AuthPlugin()); 
    $frontController->getRouter()->addDefaultRoutes(); 
    $autoloader = Zend_Loader_Autoloader::getInstance(); 
    $autoloader->setFallbackAutoloader(true); 

    /*$this->bootstrap('layout'); 
    $layout=$this->getResource('layout'); 
    $view=$layout->getView();*/ 
    $config = new Zend_Config_Xml(APPLICATION_PATH. '/configs/navigation.xml', 'nav'); 
    $container = new Zend_Navigation($config); 
    /*$view = Zend_Layout::startMvc()->getView(); 
    $view->registerHelper(new Menu(), 'menu'); 
    $view->navigation($container);//->setAcl($acl)->setRole($auth->getStorage()->read()->role);  
    */Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl); 
    $auth = Zend_Auth::getInstance(); 
    $identity = $auth->getIdentity(); 
    $role = ($auth->hasIdentity() && !empty($auth->getIdentity()->role)) 
      ? $auth->getIdentity()->role : 'guest'; 
    Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($role); 
    /*$view->navigation()->menu()->render(); 
    $view->navigation()->menu()->setMinDepth(1)->setUlClass('nav');*/ 
} 

一切正常exept導航。問題是,爲什麼我的導航不起作用,我該如何修復它。

回答

1

開始你的Bootstrap.php是一團糟。從一些基本的組織開始。

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    /** 
    * initialize the session 
    */ 
    protected function _initsession() 
    { 
     Zend_Session::start(); 
    } 

    /** 
    * initialize the registry and assign application.ini to config namespace of Zend_Registry 
    */ 
    protected function _initRegistry() 
    { 
     $config = new Zend_Config($this->getOptions()); 
     Zend_Registry::set('config', $config); 
    } 

當你要處理的視圖對象在你的引導,你需要確保返回的新觀點:

/** 
    * initialize the view and return it 
    * @return \Zend_View 
    */ 
    protected function _initView() 
    { 
     //Initialize view 
     $view = new Zend_View(); 
     //add custom view helper path 
     $view->addHelperPath('/../library/My/View/Helper'); 
     //set doctype for default layout 
     $view->doctype(Zend_Registry::get('config')->resources->view->doctype); 
     //set head meta data 
     $view->headMeta()->setCharset(Zend_Registry::get('config')->resources->view->charset); 
     //set css includes 
     $view->headlink()->setStylesheet('/bootstrap/css/bootstrap.min.css'); 
     $view->headLink()->appendStylesheet('/css/main.css'); 
     //add javascript files 
     $view->headScript()->setFile('/bootstrap/js/jquery.min.js'); 
     $view->headScript()->appendFile('/bootstrap/js/bootstrap.min.js'); 
     //add the view to the view renderer 
     $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
       'ViewRenderer'); 
     $viewRenderer->setView($view); 
     //Return it, so that it can be stored by the bootstrap 
     return $view; 
    } 

至於導航而言。您可以初始化導航容器的引導,但實際的視圖組件通常將賭注設置佈局:

然後在您的佈局文件,你可以實現類似導航:

<div id="nav"> 
    <?php echo $this->navigation()->menu() 
       ->renderMenu(null, array(
        'minDepth' => null, 
        'maxDepth' => 1, 
        'onlyActiveBranch' => TRUE 
       )) ?> 
</div> 

一需要注意的事項:當我們在_initRegistry()方法中調用$this->getOptions()時,我們得到的選項來自application.ini文件。大量的選項經常在這裏初始化。

//application.ini as example your settings will be somewhat different 
[production] 
;------------------------------------------------------------------------------- 
;PHP 
;------------------------------------------------------------------------------- 

phpSettings.display_startup_errors = 0 
phpSettings.display_errors = 0 

;------------------------------------------------------------------------------- 
;Paths and Namespaces 
;------------------------------------------------------------------------------- 

includePaths.library = APPLICATION_PATH "/../library" 
bootstrap.path = APPLICATION_PATH "/Bootstrap.php" 
bootstrap.class = "Bootstrap" 
appnamespace = "Application" 
autoloaderNamespaces[] = "My_" 

;------------------------------------------------------------------------------- 
;Front Controller 
;------------------------------------------------------------------------------- 

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" 
resources.frontController.params.displayExceptions = 0 
resources.frontController.moduleControllerDirectoryName = "controllers" 
resources.frontController.params.prefixDefaultModule = "" 
resources.modules = "" 
resources.frontController.baseurl = http://example.com 
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" 

;------------------------------------------------------------------------------- 
;plugins 
;------------------------------------------------------------------------------- 

pluginPaths.My_Application_Resource = APPLICATION_PATH "/../library/My/Resource" 
resources.frontController.actionhelperpaths.My_Controller_Action_Helper = APPLICATION_PATH "/../library/My/Controller/Action/Helper" 

;------------------------------------------------------------------------------- 
;View Settings 
;------------------------------------------------------------------------------- 

resources.view[]= 
resources.view.charset = "UTF-8" 
resources.view.encoding = "UTF-8" 
resources.view.doctype = "HTML5" 
resources.view.language = "en" 

;------------------------------------------------------------------------------- 
;Database Settings 
;------------------------------------------------------------------------------- 

resources.db.adapter = "pdo_Mysql" 
resources.db.params.username = "user" 
resources.db.params.password = "xxxxxxx" 
resources.db.params.dbname = "dbname" 
resources.db.params.charset = "utf8" 
resources.db.isDefaultTableAdapter = true 
resources.db.params.profiler = true 

;------------------------------------------------------------------------------- 
;Layouts 
;------------------------------------------------------------------------------- 

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/" 


[staging : production] 


[testing : production] 

phpSettings.display_startup_errors = 1 
phpSettings.display_errors = 1 

[development : production] 

phpSettings.display_startup_errors = 1 
phpSettings.display_errors = 1 
resources.frontController.params.displayExceptions = 1 

如果你的頁面是空白的,你認爲它不應該是。將您的環境設置爲'開發',或者將錯誤顯示值從'生產'部分的'0'更改爲'1'。如果你這樣做,錯誤將會顯示。

好運

+0

哦,最好的感謝,它真的幫了大忙! – user1956641 2013-04-03 10:47:54

相關問題