由于zend路线,导航无法正常工作

时间:2011-01-27 14:05:14

标签: zend-framework

修改:: 问题是由于zend路由引起的,请检查更新

我正在使用xml文件进行导航。

编辑::以下代码来自layout.phtml文件

$config = new Zend_Config_Xml(APPLICATION_PATH.'/configs/adminnav.xml', 'nav');
$container = new Zend_Navigation($config);
$this->navigation()->setContainer($container);
echo $this->navigation();

当我在我的编辑页面时,所有菜单都没有链接。所有得到的是菜单项上的/ admin / controller / edit / everywhere。任何想法?

我的编辑操作采用id,如果未提供id,则会生成错误。但是add和list方法工作正常

更新::

<?xml version="1.0"?>
<config>
<nav>
    <home>
        <label>home</label>
        <uri>#</uri>
        <pages>
            <home>
                <label>home</label>
                <module>admin</module>
                <controller>home</controller>
                <action>index</action>
            </home>
            <help>
                <label>help</label>
                <module>admin</module>
                <controller>home</controller>
                <action>help</action>
            </help>
        </pages>
    </home>
    <page>
        <label>pages</label>
        <uri>#</uri>
        <pages>
            <static>
                <label>static pages</label>
                <module>admin</module>
                <controller>page</controller>
                <action>index</action>
            </static>
            <editpage>
                <label>static pages</label>
                <module>admin</module>
                <controller>page</controller>
                <action>edit</action>           
            </editpage>
        </pages>
    </page>

    <destination>
        <label>destinations</label>
        <uri>#</uri>
        <pages>
            <list>
                <label>list all</label>
                <module>admin</module>
                <controller>destination</controller>
                <action>index</action>
            </list>
            <featured>
                <label>featured</label>
                <module>admin</module>
                <controller>destination</controller>
                <action>featured</action>                   
            </featured>
            <add>
                <label>add destination</label>
                <module>admin</module>
                <controller>destination</controller>
                <action>add</action>
            </add>
            <editdest>
                <label>edit destination</label>
                <module>admin</module>
                <controller>destination</controller>
                <action>edit</action>           
            </editdest>
        </pages>
    </destination>

    <tours>         
    <label>tours</label>
        <uri>#</uri>
        <pages>
            <list>
                <label>list tours</label>
                <module>admin</module>
                <controller>tour</controller>
                <action>index</action>
            </list>
            <featured>
                <label>featured tours</label>
                <module>admin</module>
                <controller>tour</controller>
                <action>featured</action>
            </featured>
            <add>
                <label>add tours</label>
                <module>admin</module>
                <controller>tour</controller>
                <action>add</action>
            </add>
            <edittour>
                <label>edit tours</label>
                <module>admin</module>
                <controller>tour</controller>
                <action>add</action>
            </edittour>
        </pages>
    </tours>

    <hotels>            
    <label>hotels and resort</label>
        <uri>#</uri>
        <pages>
            <list>
                <label>list hotel</label>
                <module>admin</module>
                <controller>hotel</controller>
                <action>index</action>
            </list>
            <add>
                <label>add hotel</label>
                <module>admin</module>
                <controller>hotel</controller>
                <action>add</action>
            </add>
            <edithotel>
                <label>add hotel</label>
                <module>admin</module>
                <controller>hotel</controller>
                <action>add</action>
            </edithotel>
        </pages>
    </hotels>

    <message>           
        <label>message</label>
        <uri>#</uri>
        <pages>
            <all>
                <label>all message</label>
                <module>admin</module>
                <controller>message</controller>
                <action>index</action>
            </all>
            <contactus>
                <label>contact</label>
                <module>admin</module>
                <controller>message</controller>
                <action>contact</action>
            </contactus>
            <inquiry>
                <label>inquiry</label>
                <module>admin</module>
                <controller>message</controller>
                <action>inquiry</action>
            </inquiry>
            <reservation>
                <label>reservation</label>
                <module>admin</module>
                <controller>message</controller>
                <action>reservation</action>
            </reservation>
        </pages>
    </message>

    <advertisement>         
        <label>advertisement</label>
        <uri>#</uri>
        <pages>
            <list>
                <label>list ads</label>
                <module>admin</module>
                <controller>advertisement</controller>
                <action>index</action>
            </list>
            <add>
                <label>add ads</label>
                <module>admin</module>
                <controller>advertisement</controller>
                <action>add</action>
            </add>
            <editad>
                <label>edit ads</label>
                <module>admin</module>
                <controller>advertisement</controller>
                <action>edit</action>
            </editad>
        </pages>
    </advertisement>

    <setting>           
        <label>settings</label>
        <uri>#</uri>
        <pages>
            <view>
                <label>view</label>
                <module>admin</module>
                <controller>setting</controller>
                <action>view</action>
            </view>
            <account>
                <label>account setting</label>
                <module>admin</module>
                <controller>setting</controller>
                <action>account</action>
            </account>
            <site>
                <label>site setting</label>
                <module>admin</module>
                <controller>setting</controller>
                <action>site</action>
            </site> 
        </pages>
    </setting>
</nav>
</config>

UPDATE :: bootstrap.php上编辑操作的路由

    $frontcontroller = Zend_Controller_Front::getInstance();
    $router = $frontcontroller->getRouter('router');

    //add route  for edit page so that pageid is not displayed in the url
    $router->addRoute(
        'edit-page',
        new Zend_Controller_Router_Route('admin/page/edit/:pageid', array(
            'module' => 'admin',
            'controller' => 'page',
            'action'    => 'edit',
            'pageid'    => 'pageid'
        ))
    );

UPDATE :: to Phil

<editdest>
    <label>edit destination</label>
    <module>admin</module>
    <controller>destination</controller>
    <action>edit</action>   
    <route>12</route>       
</editdest>

此错误::

Fatal error: Zend_Controller_Router_Exception: Route 12 is not defined in
/usr/share/php/libzend-framework-php/Zend/View/Helper/Navigation/HelperAbstract.php 
on line 522 

2 个答案:

答案 0 :(得分:3)

如果涉及任何静态路由,则需要在所有导航页面上设置路由名称。这是因为导航视图助手使用Url视图助手来创建链接。

如果没有路由名称,则使用当前路由。

如果当前路由是静态路由,它将只解析为一个URL,无论抛出什么参数。

答案 1 :(得分:1)

您应该使用在$router->addRoute中定义的路线。

应该是:

$router->addRoute(
    'edit-page',
    new Zend_Controller_Router_Route('admin/page/edit/:pageid', array(
        'module' => 'admin',
        'controller' => 'destination',
        'action'    => 'edit',
        'pageid'    => 'pageid'
    ))
);


<editdest>
    <label>edit destination</label>
    <module>admin</module>
    <controller>destination</controller>
    <action>edit</action>   
    <route>edit-page</route>
    <params>
       <pageid>12</pageid>
    </params>       
</editdest>

如果每页需要不同的编辑链接,则必须动态更新导航容器中的参数,即:

// (pseudocode, not tested)
$this->navigation()->getContainer()->findOneByLabel('edit destination')->params->pageid = 12