使用 XML 文件将 url 参数传递给 Zend_Navigation

发布于 2024-10-01 07:24:17 字数 1134 浏览 3 评论 0原文

我正在使用 Zend Framework 1.10.8。

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

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

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

在 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 将从中的请求对象中检索)最终版本)

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

                ));
 }

由于添加动态 url 参数似乎是 Zend_Navigation 的基本要求,我非常确定我的解决方案太复杂、太昂贵,并且必须有一个“开箱即用”的更简单的解决方案。

I am using Zend Framework 1.10.8.

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

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.

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

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>

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

                ));
 }

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".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅士风度i 2024-10-08 07:24:17

这很简单。只需在 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>

,这里是根据数据库数据动态执行此操作的示例

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

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 中初始化此插件

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

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

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

Then in you Bootstrap init this plugin

protected function _initNavigation() {
    Zend_Controller_Front::getInstance()->registerPlugin(new Plugin_Navigation());
}
猫弦 2024-10-08 07:24:17

更新:我最终扔掉了 xml 文件。我现在做什么:

  • 我编写了一个插件(请参阅 Daimon 的方法),
  • 在这个插件中我将导航结构配置为数组,
    从 Zend_Request 检索动态参数,
  • 然后我使用该数组初始化导航

An update: I finally ended up throwing away the xml file. What I do now:

  • I wrote a plugin (see Daimon's approach)
  • in this plugin I configure my navigation structure as an array, the
    dynamic parameters are retrieved from Zend_Request
  • then I init the navigation using this array
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文