【问题标题】:Editable Breadcrumb for Dynamic Pages动态页面的可编辑面包屑
【发布时间】:2012-03-02 18:49:58
【问题描述】:

为 Zend Framework 项目寻找更好的 Breadcrumb 解决方案。

目前我有一个类似的 navigation.xml

<?xml version="1.0" encoding="UTF-8"?>
<configdata>
    <nav>
        <home>
            <label>Home</label>
            <module>default</module>
            <controller>index</controller>
            <action>index</action>
            <pages>
                <countryurl>
                    <label>Spain</label>
                    <module>default</module>
                    <controller>country</controller>
                    <action>index</action>
                    <route>country_url</route>
                    <params>
                        <country>spain</country>
                    </params>
                    <pages>
                        <provinceurl>
                            <label>Madrid</label>
                            <module>default</module>
                            <controller>country</controller>
                            <action>province</action>
                            <route>province_url</route>
                            <params>
                                <country>spain</country>
                                <province>madrid</province>
                            </params>
                            <pages>
                                <cityurl>
                                    <label>City</label>
                                    <module>default</module>
                                    <controller>country</controller>
                                    <action>city</action>
                                    <route>city_url</route>
                                    <params>
                                        <country>spain</country>
                                        <province>madrid</province>
                                        <city>madrid</city>
                                    </params>
                                    <pages>
                                        <producturl>
                                            <label>Product</label>
                                            <module>default</module>
                                            <controller>country</controller>
                                            <action>product</action>
                                            <route>product_url</route>
                                            <params>
                                                <country>spain</country>
                                                <province>madrid</province>
                                                <city>madrid</city>
                                                <product>product</product>
                                            </params>
                                        </producturl>
                                    </pages>
                                </cityurl>
                            </pages>
                        </provinceurl>
                    </pages>
                </countryurl>
            </pages>
        </home>
    </nav>
</configdata>

和类似的路线

$router->addRoute(
    'product_url',
    new Zend_Controller_Router_Route(':lang/:country/:province/:city/:product', array(
        'controller' => 'country',
        'action' => 'product'
    ))
);

$router->addRoute(
    'city_url',
    new Zend_Controller_Router_Route(':lang/:country/:province/:city', array(
        'controller' => 'country',
        'action' => 'city'
    ))
);

$router->addRoute(
    'province_url',
    new Zend_Controller_Router_Route(':lang/:country/:province', array(
        'controller' => 'country',
        'action' => 'province'
    ))
);

$router->addRoute(
    'country_url',
    new Zend_Controller_Router_Route(':lang/:country', array(
        'controller' => 'country',
        'action' => 'index'
    ))
);

我面临一些问题/正在寻找一些建议。在 Zend_Navigation 的帮助下创建面包屑

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

1 ) http://example.com/en/spain/madrid/madrid/product 的请求显示面包屑,在

的帮助下
$this->navigation()
    ->breadcrumbs()
    ->setMinDepth(0)
    ->setLinkLast(true)
    ->setSeparator(" > ");

作为Home &gt; Spain &gt; Madrid &gt; City &gt; Product

但是指向西班牙、马德里、城市的链接都是http://example.com。应该是 http://example.com/en/spainhttp://example.com/en/spain/madridhttp://example.com/en/spain/madrid/madrid

2) 当前请求http://example.com/en/spain

面包屑将显示Home &gt;&gt; Spain

<label>Spain</label>
<module>default</module>
<controller>country</controller>
<action>index</action>
<route>country_url</route>
<params>
    <country>spain</country>
</params>

但您可以看到 param country 因国家/地区而异。那么我们要为所有国家/地区添加标签吗?

http://example.com/en/spain

Home >> Spain

http://example.com/en/india

Home >> India

我有省份、城市和产品,有什么建议我可以为它构建吗?

这也是一个多语言网站,那么我们如何才能对标签进行必要的更改呢?我想如果我们使用 Zend_Translate,它会做出必要的改变。

【问题讨论】:

  • 首先,您似乎需要动态导航(在引导程序中初始化,获取您所有的国家、省、市;或者如果您使用导航创建面包屑,然后你可以在routeShutdown()之后再做,所以你可以只添加你需要的国家/省/城市路径)。但这仍然不能解决您的核心问题:导航/面包屑似乎正在使用错误的路线组装 url。
  • 谢谢 David Weinraub,目前我只在寻找 Breadcrumb,而且 Bootstrap.php 也有点不同。它根本不是一个类,这实际上是一个用 1.6 编写的遗留项目,我刚刚升级到 1.11 以使用 Breadcrumb 和一些东西。

标签: zend-framework breadcrumbs zend-navigation


【解决方案1】:

您可以创建自己的页面类,将 : 开头的参数视为动态的。您可以在导航配置中引用它,例如

...
<countryurl>
    <label>:country</label> <!-- dynamic -->
    <module>default</module>
    <controller>index</controller>
    <action>index</action>
    <route>country_url</route>
    <type>DynamicNavPage</type> <!-- page classname -->
    <params>
        <country>:country</country> <!-- dynamic -->
    </params>
        <pages>
    ...

例如

<?php

class DynamicNavPage extends Zend_Navigation_Page_Mvc {

    /**
    * Params with ":" are read from request
    * 
    * @param array $params
    * @return Zend_Navigation_Page_Mvc
    */
    public function setParams(array $params = null) {
        $requestParams = Zend_Controller_Front::getInstance()
                        ->getRequest()->getParams();

        //searching for dynamic params (begining with :)
        foreach ($params as $paramKey => $param) {
            if (substr($param, 0, 1) == ':' &&
                    array_key_exists(substr($param, 1), $requestParams)) {
                $params[$paramKey] = $requestParams[substr($param, 1)];
            }
        }

        return parent::setParams($params);
    }

    /**
    * If label begining with : manipulate (for example with Zend_Tanslate)
    */
    public function setLabel($label) {
        if (substr($label, 0, 1) == ':') {
            //label modifications go here
            //as an example reading it from page params and capitalizing
            //can use Zend_Translate here
            $requestParams = Zend_Controller_Front::getInstance()
                            ->getRequest()->getParams();
            $labelParamKey = substr($label, 1);

            if (array_key_exists($labelParamKey, $requestParams)) {
                $label = ucfirst($requestParams[$labelParamKey]);
            }
        }

        return parent::setLabel($label);
    }

}

【讨论】:

  • 感谢您的回答。我需要一些时间来测试它,忙于不同的东西。如果一切正常,我会使其正确。我觉得这应该工作:)。再次感谢您的耐心等待。
  • 我在这里面临的问题是我没有得到请求对象,所以它抛出 Call to a member function getParams() on a non-object 。 $requestParams = Zend_Controller_Front::getInstance()-&gt;getRequest()-&gt;getParams() 。我想知道为什么会这样:(,你有什么见解吗?这里的请求对象为空Zend_Controller_Front::getInstance()-&gt;getRequest()
  • 我不知道你的设置,一个建议是覆盖路由器的 route(Zend_Controller_Request_Abstract $request) 方法并将父 route() 返回的内容设置为静态字段。这样,您可以执行以下操作,而不是向前端控制器询问请求:MyRouterClass::$routedRequest-&gt;getParams(); 例如:public function route(Zend_Controller_Request_Abstract $request){ self::$routedRequest = parent::route($request); return $request; } 试试看。
  • 我使用的方式与快速入门相同,并在 Bootstrap 中使用路由器。路线为 stackoverflow.com/a/9128784/487878 。我会试试你的建议。谢谢。
  • 好吧,我现在明白了,问题是你试图在路由过程完成之前设置依赖于请求的导航 - 可能在引导程序中。正确的做法是注册一个插件(在 Bootstrap 中)并设置您的导航是 routeShutdow() 方法。请求对象肯定已经准备好了。我之前的评论是错误的,忽略它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-28
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
相关资源
最近更新 更多