Zend framework 动态页面的可编辑面包屑
正在为Zend Framework项目寻找更好的面包屑解决方案 目前我有一个类似navigation.xml的Zend framework 动态页面的可编辑面包屑,zend-framework,breadcrumbs,zend-navigation,Zend Framework,Breadcrumbs,Zend Navigation,正在为Zend Framework项目寻找更好的面包屑解决方案 目前我有一个类似navigation.xml的 <?xml version="1.0" encoding="UTF-8"?> <configdata> <nav> <home> <label>Home</label> <module>default</module>
<?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>
我正面临一些问题/寻求一些建议。借助Zend_导航创建面包屑
$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(" > ");
作为主页>西班牙>马德里>城市>产品
但是指向西班牙、马德里和曼城的链接都指向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
面包屑将显示主页>>西班牙
<label>Spain</label>
<module>default</module>
<controller>country</controller>
<action>index</action>
<route>country_url</route>
<params>
<country>spain</country>
</params>
我有省、市和产品,有什么建议我可以为它建设吗
此外,这是一个多语言网站,那么我们如何才能对标签进行必要的更改?我想如果我们使用Zend_Translate,它将进行必要的更改。您可以创建自己的页面类,将以:开头的参数视为动态参数。您可以在导航配置中引用它,如
...
<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);
}
}
您可以创建自己的页面类,该类将以:开头的参数视为动态参数。您可以在导航配置中引用它,如
...
<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);
}
}
首先,您似乎需要动态导航(在引导中初始化,获取您所有的国家、省、市;或者如果您仅使用导航创建面包屑,则可以在routeShutdown()
之后执行,这样您就可以只添加所需的国家/省/市路径)。但这仍然不能解决您的核心问题:nav/Breadcrumb似乎使用了错误的路径来组装URL。谢谢David Weinraub,目前我只查找Breadcrumb,Bootstrap.php也有点不同。它根本不是一个类,这实际上是一个用1.6编写的遗留项目,我刚刚升级到1.11以使用面包屑和一些东西(在bootstrap中初始化,获取所有国家、省、市;或者如果您仅使用导航创建面包屑,则可以稍后在routeShutdown()
之后进行操作,以便只添加所需的国家/省/市路径)。但这仍然不能解决您的核心问题:导航/面包屑似乎使用了错误的路径来组装URL。谢谢David Weinraub,目前我只寻找面包屑,而且Bootstrap.php有点不同。它根本不是一个类,这实际上是一个用1.6编写的遗留项目,我刚刚升级到1.11使用面包屑和一些东西。谢谢你的回答。我需要一些时间来测试它,忙于不同的东西。如果一切正常,我会使它正确。我觉得这应该有效:)。再次感谢您的耐心等待。我在这里面临的问题是,我没有获取请求对象,因此它会对非对象上的成员函数getParams()
发出调用<代码>$requestParams=Zend\u Controller\u Front::getInstance()->getRequest()->getParams()
。我想知道为什么是这样:(,你有什么见解吗?请求对象在这里是空的Zend\u Controller\u Front::getInstance()->getRequest()
我不知道你的设置,一个建议是覆盖路由器的路由(Zend\u Controller\u request\u Abstract$request)
方法并设置父路由()以静态字段的形式返回。这样,您就可以不向前端控制器请求请求了:MyRouterClass::$routedRequest->getParams();
例如:public function route(Zend\u controller\u request\u Abstract$request){self:$routedRequest=parent::route($request);return$request;}
试试看。我使用的方法与quick start相同,并且在引导中使用了路由器。路由如下。我将尝试您的建议。谢谢。好的,我现在明白了,问题是您试图在路由过程完成之前设置依赖请求的导航-可能在引导中。正确的方法是注册插件(在引导程序中)和设置您的导航是routeShutdow()方法。请求对象肯定已经准备好了。我之前的评论是错误的,忽略它谢谢您的回答。我需要一些时间来测试它,忙于不同的事情。如果一切正常,我会使它正确。我觉得这应该可以:)。再次感谢您的耐心等待。我在这里面临的问题是,我没有获取请求对象,因此它会对非对象上的成员函数getParams()
发出调用<代码>$requestParams=Zend\u Controller\u Front::getInstance()->getRequest()->getParams()
。我想知道为什么是这样:(,你有什么见解吗?请求对象在这里是空的Zend\u Controller\u Front::getInstance()->getRequest()
我不知道你的设置,一个建议是覆盖路由器的路由(Zend\u Controller\u request\u Abstract$request)
方法并设置父路由()以静态字段的形式返回。这样,您就可以不向前端控制器请求请求了:MyRouterClass::$routedRequest->getParams();
例如:public function route(Zend\u controller\u request\u Abstract$request){self:$routedRequest=parent::route($request);return$request;}
试试看。我使用的方法与quick start相同,并且在引导中使用了router。路由如下。我将尝试您的建议。谢谢。好的,我现在明白了,问题是您正在尝试使用
<?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);
}
}