动态页面的可编辑面包屑

时间:2012-02-10 09:23:42

标签: zend-framework breadcrumbs zend-navigation

为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 > Spain > Madrid > City > 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 >> 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,它将进行必要的更改。

1 个答案:

答案 0 :(得分:0)

您可以创建自己的页面类来处理以以下内容开头的参数:动态。您可以在导航配置中引用它,如

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

}