zend_Config_Xml的zend导航问题

时间:2012-04-15 14:00:12

标签: php zend-framework zend-navigation

我正在尝试实现Zend_Navigation - 通过此tutorial

创建菜单和面包屑

在Bootstrap文件中,

...
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    protected $_config;
    protected $_layout;

    protected function _initConfig() {
        $this->_config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini'); 
        Zend_Registry::set("config", $this->_config);
        if ($this->_config->debug) {
            error_reporting(E_ALL | E_STRICT);
            ini_set('display_errors', 'on');
        }
        $request = new Zend_Controller_Request_Http();
        $uri = $request->getRequestUri(); 
        if (preg_match("/admin/", $uri)) {
            //echo $this->_config->layout->admin->layout; exit;
            $this->_layout = Zend_Layout::startMvc(
                            array(
                                'layoutPath' => $this->_config->layout->layoutPath,
                                'layout' => $this->_config->layout->admin->layout
                            )
            );
        } else { 
            $this->_layout = Zend_Layout::startMvc(
                            array(
                                'layoutPath' => $this->_config->layout->layoutPath,
                                'layout' => $this->_config->layout->layout)
            );
            //echo $this->_view = $this->_layout->getView(); exit;
        }
    }
    /**
     * used for handling top-level navigation
     * @return Zend_Navigation
      */
    protected function _initNavigation()
    {
            $view = $this->_layout->getView();   
           /*
            $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->navigation($container);
    }   
...

以下是application / config文件夹下的navigation.xml

<?xml version="1.0" encoding="UTF-8"?>
<configdata>
    <nav>
        <home>
            <label>Home</label>
            <uri>/</uri>

            <pages>
                <index>
                    <label>Home</label>
                    <uri>/index/index</uri>     
                </index>
                <index>
                    <label>Product</label>
                    <uri>/index/product</uri>       
                </index>

            </pages>        
        </home>
    </nav>
</configdata>

在布局文件

...
            <div class="breadcrumbs">
            <?= $this->navigation()->breadcrumbs()->setMinDepth(0)->setLinkLast(true)->setSeparator(" : "); ?>
        </div>
...

当我运行该网站时,我收到以下错误,

  

致命错误:未捕获异常'Zend_Navigation_Exception'   消息'无效参数:无法确定要实例化的类'   在C:\ xampp \ htdocs \ enit \ library \ Zend \ Navigation \ Page.php:235 Stack   追踪:#0   C:\ XAMPP \ htdocs中\ ENIT \库\ Zend的\导航\ Container.php(117):   Zend_Navigation_Page :: factory(数组)#1   C:\ XAMPP \ htdocs中\ ENIT \库\ Zend的\导航\ Container.php(164):   Zend_Navigation_Container-&gt; addPage(Array)#2   C:\ XAMPP \ htdocs中\ ENIT \库\ Zend的\导航\ Container.php(179):   Zend_Navigation_Container-&gt; addPages(数组)#3   C:\ XAMPP \ htdocs中\ ENIT \库\ Zend的\导航\ page.php文件(852):   Zend_Navigation_Container-&gt; setPages(Array)#4   C:\ XAMPP \ htdocs中\ ENIT \库\ Zend的\导航\ page.php文件(295):   Zend_Navigation_Page-&gt; set('pages',Array)#5   C:\ XAMPP \ htdocs中\ ENIT \库\ Zend的\导航\ page.php文件(250):   Zend_Navigation_Page-&gt; setOptions(Array)#6   C:\ XAMPP \ htdocs中\ ENIT \库\ Zend的\导航\ page.php文件(232):   Zend_Navigation_Page-&gt; __ construct(Array)#7   C:\ XAMPP \ htdocs中\ ENIT \库\ Zend的\导航\ Container.php(117):   Zend_Navigation_P in   第235行的C:\ xampp \ htdocs \ enit \ library \ Zend \ Navigation \ Page.php

我搜索了stackoverflow以及谷歌的一些解决方案,但我找不到它。我做错了什么?请提供建议

1 个答案:

答案 0 :(得分:1)

我已经运行了你的代码。 Bug在配置中。你有两个同名的兄弟姐妹,Zend_Config_Xml将它归结为这种形式

array
  'label' => string 'Home' (length=4)
  'uri' => string '/' (length=1)
  'pages' => 
    array
      'index' => 
        array
          0 => 
            array
              'label' => string 'Home' (length=4)
              'uri' => string '/index/index' (length=12)
          1 => 
            array
              'label' => string 'Product' (length=7)
              'uri' => string '/index/product' (length=14)

当您查看Zend_Mvc_Page第235行时,您可以找出抛出异常的原因。

        $hasUri = isset($options['uri']);
        $hasMvc = isset($options['action']) || isset($options['controller']) ||
                  isset($options['module']) || isset($options['route']);

        if ($hasMvc) {
            require_once 'Zend/Navigation/Page/Mvc.php';
            return new Zend_Navigation_Page_Mvc($options);
        } elseif ($hasUri) {
            require_once 'Zend/Navigation/Page/Uri.php';
            return new Zend_Navigation_Page_Uri($options);
        } else {
            require_once 'Zend/Navigation/Exception.php';
            throw new Zend_Navigation_Exception(
                'Invalid argument: Unable to determine class to instantiate');
        }

所以最后解决方案是改变第二个兄弟姐妹的名字。