使用XML文件将url参数传递给Zend_Navigation [英] Passing url parameters to Zend_Navigation using an XML-file

查看:137
本文介绍了使用XML文件将url参数传递给Zend_Navigation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Zend Framework 1.10.8.

I am using Zend Framework 1.10.8.

我想在我的layout.phtml中创建一个面包屑部分.我的菜单中有些链接具有动态url参数,例如 http://mydomain.com/editor/编辑/id/42

I want to create a breadcrumb section in my layout.phtml. There are some links in my menu that have dynamic url parameters like http://mydomain.com/editor/edit/id/42

我试图弄清楚如何将id = XXX传递给Zend_Navigation,而XXX来自数据库,并且在每个请求中都不同.

I try to figure out how to pass id=XXX to Zend_Navigation, while XXX comes from the database and is different in every request.

我到目前为止发现的一个解决方案是添加一个属性,例如params_id到我的xml声明:

One solution I found so far is adding a property e.g. params_id to my xml declaration:

在configs/navigation.xml中

in configs/navigation.xml

<pages>
   <editor>
     <label>Editor</label>
     <controller>editor</controller>
      <action>edit</action>
     <params_id>id</params_id>
     <route>default</route>  
  </editor>
 </pages>

,然后在控制器中循环浏览页面并动态添加我的参数id = 42(而最终版本中的请求对象将检索到42)

and in the controller looping through the pages and dynamically adding my parameter id = 42 (while 42 would be retrieved from the request object in the final version)

$pages = $this->view->navigation()->getContainer()->findAllBy('params_id','id');
            foreach ($pages as &$page) {
                $page->setParams(array(
                    'id' => 42,
                    'something_else' => 667

                ));
 }

因为添加动态url参数似乎是Zend_Navigation的基本要求,所以我很确定我的解决方案太复杂,太昂贵,而且必须有开箱即用"的简单得多的解决方案.

As adding dynamic url parameters seems such a basic requirement for Zend_Navigation I am quite sure that my solution is too complicate, too expensive and there must be a much simplier solution "out of the box".

推荐答案

这很简单.只需用您的XML编写

It is very simple. Just write in your XML

<pages>
    <editor>
        <label>Editor</label>
        <controller>editor</controller>
        <action>edit</action>
        <params>
            <id>42</id>
            <someting_else>667</something_else>
        </params>
        <route>default</route>  
    </editor>
</pages>

以下是基于数据库数据动态执行此操作的示例

Here is example to do it dynamically based on database data

首先定义导航加载插件.将文件命名为Navigation.php并将其放置在application/plugins/目录中.这是此类插件的示例:

First define Navigation loading plugin. Name the file Navigation.php and place it in application/plugins/ directory. Here's an example of such plugin:

class Plugin_Navigation extends Zend_Controller_Plugin_Abstract
{
    function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $view = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view;

        //load initial navigation from XML
        $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
        $container = new Zend_Navigation($config);

        //get root page
        $rootPage = $container->findOneBy('sth', 'value');

        //get database data
        $data = Model_Sth::getData();

        foreach ($data as $row) {
            $rootPage->addPage(new Zend_Navigation_Page_Mvc(array(
                'module'     => 'default',
                'controller' => 'examplecontroller',
                'action'     => 'exampleaction',
                'route'      => 'exampleroute',
                'label'      => $row['some_field'],
                'params'     => array(
                    'param1' => $row['param1'],
                    'param2' => $row['param1']
                )
            )));
        }

        //pass container to view
        $view->navigation($container);
    }
}

然后在您的Bootstrap中初始化此插件

Then in you Bootstrap init this plugin

protected function _initNavigation() {
    Zend_Controller_Front::getInstance()->registerPlugin(new Plugin_Navigation());
}

这篇关于使用XML文件将url参数传递给Zend_Navigation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆