Редактируемая крошка для динамических страниц
В поисках лучшего решения Breadcrumb для проекта Zend Framework.
В настоящее время у меня есть навигация. 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/spain
, http://example.com/en/spain/madrid
, http://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
отличается в зависимости от страны. Итак, хотим ли мы добавить ярлыки для всех стран?
Home >> Spain
Home >> India
У меня есть провинции, города и продукты, есть предложения, как я могу их построить?
Также это многоязычный сайт, так как мы можем внести необходимые изменения в этикетку? Я думаю, что если мы используем Zend_Translate, он внесет необходимые изменения.
1 ответ
Вы можете создать свой собственный класс страницы, который обрабатывает параметры, начинающиеся с: как динамические. Вы можете ссылаться на него в конфигурации Nav, как
...
<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);
}
}