0

控制器中的变量不会出现在视图中,并且视图助手不起作用:例如,我在控制器中定义了一个变量:

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

并在视图中打印:

echo $this->title;

变量“titles”的内容不会出现在页面中。

这是我的引导文件:

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');*/
} 

一切正常除了导航。问题是,为什么我的导航不起作用以及如何修复它。

4

1 回答 1

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;
    }

就导航而言。您可以在引导程序中初始化导航容器,但实际的视图组件通常会在布局中设置:

   /**
     * The bootstrap part as example, yours will likely be similar but different
     */
    protected function _initNavigation() {

        $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/site.xml');
        $container = new Zend_Navigation($config);
        $registry = Zend_Registry::getInstance();
        $registry->set('Zend_Navigation', $container);
    }

然后在您的布局文件中,您可以实现类似于以下内容的导航:

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

需要记住的一件事:我们在调用方法时获得的选项$this->getOptions()来自_initRegistry()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”。如果您这样做,将显示错误。

祝你好运

于 2013-03-28T10:38:51.197 回答