PHP-Zend导航和Zend Router问题-找不到正确的活动页面
作者:互联网
好的.我正在使用Zend构建CMS.它并不像看起来那么简单,但仍然是–对我来说最好的解决方案.我有一个带有ID和PARENT,PARENT标记的树型系统,孩子在该页面下.无论如何.简单的东西.
每次创建页面或对导航和排序页面进行排序时路由被重新生成.
我将在此处复制整个Admin_Pages_Model代码,以创建导航和路线.
导航在此处创建:
(我认为不需要模块/控制器/动作信息,因为它是从路由器加载的)
public function createNavigation($locale = false){
$root = $this->getRoot($locale);
$navigation = array();
$router = array();
foreach($root as $row){
$navigation[$row["id"]] = array(
"label" => $row["name"],
"module" => "frontend",
"controller" => "page",
"action" => "show",
"route" => "route_".$row["id"],
"visible" => (boolean) $row["active"],
"lastmod" => ($row["modified"] ? $row["modified"] : $row["created"])
);
$children = $this->getChildren($row["id"]);
if(count($children)){
foreach($children as $child){
$navigation[$row["id"]]["pages"][$child["id"]] = $this->_createNavigation($child["id"]);
}
}
}
$nav = new Zend_Navigation(new Zend_Config($navigation));
$this->createRoutes();
if(!$locale){
Crcms_Config::setConfig("navigation_sitemap", $nav->toArray());
} else {
Crcms_Config::setConfig("navigation_".$locale, $nav->toArray());
}
}
private function _createNavigation($id){
$page = $this->getPage($id);
$navigation = array(
"label" => $page["name"],
"module" => "frontend",
"controller" => "page",
"action" => "show",
"route" => "route_".$page["id"],
"visible" => (boolean) $page["active"],
"lastmod" => ($page["modified"] ? $page["modified"] : $page["created"])
);
$children = $this->getChildren($page["id"]);
if(count($children)){
foreach($children as $child){
$navigation["pages"][$child["id"]] = $this->_createNavigation($child["id"]);
}
}
return $navigation;
}
最后-在将导航保存到数据库之前,它调用$this-> createRoutes();.所以这是代码:
public function createRoutes(){
$root = $this->getRoot($locale);
foreach($root as $row){
$slugPath = "/".$row["slug"]."";
$router["route_".$row["id"]] = array(
"route" => $slugPath.".html",
"defaults" => array(
"pageId" => $row["id"],
"locale" => $row["locale"],
"module" => "frontend",
"controller" => "page",
"action" => "show"
)
);
$children = $this->getChildren($row["id"]);
if(count($children)){
foreach($children as $child){
$router = array_merge($router, $this->_createRoutes($child["id"], $slugPath."/".$child["slug"].""));
}
}
}
$routerConfig = new Zend_Config($router);
Crcms_Config::setConfig("frontend_router", $routerConfig->toArray());
}
private function _createRoutes($id, $slugPath){
$page = $this->getPage($id);
$router["route_".$page["id"]] = array(
"route" => $slugPath.".html",
"defaults" => array(
"pageId" => $page["id"],
"locale" => $page["locale"],
"module" => "frontend",
"controller" => "page",
"action" => "show"
)
);
$children = $this->getChildren($page["id"]);
if(count($children)){
foreach($children as $child){
$router = array_merge($router, $this->_createRoutes($child["id"], $slugPath."/".$child["slug"].""));
}
}
return $router;
}
所以现在一切都变成了数据库.
在我的boostrap中,我加载:
protected function _initPostFrontController(){
$this->bootstrap('frontController');
$front = $this->getResource("FrontController");
$frontendRouterConfig = new Zend_Config(Crcms_Config::getConfig("frontend_router"));
$router = $front->getRouter();
$router->addConfig($frontendRouterConfig);
$front
->setParam("prefixDefaultModule", true)
->registerPlugin(new Global_Setup())
->registerPlugin(new Global_Auth())
->registerPlugin(new Global_Translation())
->registerPlugin(new Global_LayoutLoader());
}
这是我的Global_Setup:
class Global_Setup extends Zend_Controller_Plugin_Abstract {
public function preDispatch (Zend_Controller_Request_Abstract $request){
$front = Zend_Controller_Front::getInstance();
$errorHandler = $front->getPlugin("Zend_Controller_Plugin_ErrorHandler");
$errorHandler->setErrorHandlerModule("frontend");
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
switch($request->getModuleName()){
case "admin":
$session = new Zend_Session_Namespace("Crcms_Admin");
$locale = Zend_Registry::get("Zend_Locale");
$view->doctype("HTML5");
$view->headTitle(Zend_Registry::get("Zend_Config")->system->about->software);
$view->headTitle()->setSeparator(" | ");
$view->headTitle(Crcms_Config::getConfig("site_name"));
$view->headLink()->headLink(array(
"rel" => "shortcut icon",
"href" => Zend_Registry::get("Zend_Config")->system->paths->http->publib."/images/favicon.ico"), "PREPEND");
break;
default:
$session = new Zend_Session_Namespace("Crcms_Frontend");
if(!$session->locale){
$session->locale = Crcms_Config::getConfig("locale_default");
}
$navigation = new Zend_Navigation(new Zend_Config(Crcms_Config::getConfig("navigation_".$session->locale)));
$view->navigation()->setContainer($navigation);
break;
}
}
}
所以基本上一切都很好. LayoutLoader选择默认布局路径和基于admin / frontend的布局.
无论如何.在我的前端布局中,我有这个:
<div id="menu"><?= $this->navigation()->menu(); ?></div>
<div id="breadcrumb"><?= $this->navigation()->breadcrumbs(); ?></div>
<div id="content"><?= $this->layout()->content; ?></div>
菜单创建良好.所有级别均为超级(Y).但一切都是class =“ active” !!!而readcrumb始终显示最深的元素.
页面选择工作正常!参数pageId正确传递,路由器正常工作.导航只是搞砸了.
一些图片给你的想法:
管理员端:
–http://grab.by/6d67
前端方面:
> http://grab.by/6d5Y
> http://grab.by/6d6e
> http://grab.by/6d6g
> http://grab.by/6d6h
因此,从图片中可以看出URL发生了变化-内容也发生了变化.因此路由器必须正常工作.
一切都只是“活跃的”:http://grab.by/6d6j
我知道我在此处粘贴了很多信息,但请帮助我.我已经在这个问题上工作了20个小时-没有解决方案的帮助.
Kinda修复了它.我不认为这是“正确的方法”,但仍然-现在可以使用.我从导航对象中注释掉了控制器/动作/模块(“路线”没有任何变化).添加了一个“ id” => “ page-”.$page [“ id”].
现在在我的Global_Setup中,我做了类似的事情->
$navigation = new Zend_Navigation(new Zend_Config(Crcms_Config::getConfig("navigation_".$session->locale)));
$navigation->findBy("id", "page-".$request->getParam("pageId"))
->setActive(true);
解决方法:
这是一个猜测,因为仅通过查看代码很难解决这个问题.
建立路线时,您要设定pageId:
$router["route_".$row["id"]] = array(
"route" => $slugPath.".html",
"defaults" => array(
"pageId" => $row["id"],
"locale" => $row["locale"],
"module" => "frontend",
"controller" => "page",
"action" => "show"
)
);
大概这是您在控制器中用来确定请求哪个页面的唯一标识符?
Zend Navigation在每个页面上调用isActive()方法来确定要突出显示的页面.对于Mvc页面,这是将您提供的路由参数(控制器,模块,动作和其他参数)与请求对象中的参数进行比较.在您的情况下,所有页面都指向相同的动作,您没有给Zend Navigation指定pageId,因此它所做的只是将模块/控制器/动作与请求中的模块/控制器/动作进行比较,这将始终匹配.
如果我是对的,您要做的就是将pageId添加到您构建的导航对象中,因此在第一个代码示例的循环中:
$navigation[$row["id"]] = array(
"label" => $row["name"],
"module" => "frontend",
"controller" => "page",
"action" => "show",
"route" => "route_".$row["id"],
"params" => array("pageId" => $row["id"]), // this line is new!
"visible" => (boolean) $row["active"],
"lastmod" => ($row["modified"] ? $row["modified"] : $row["created"])
);
如果这不起作用,我希望它至少会为您指明正确的方向.我确实认为isActive()方法是问题所在,因此,如果您不介意调试某些Zend Framework代码(暂时),请在Zend / Navigation / Page / Mvc.php中找到该方法,并验证它是否被调用,看看是否可以找出问题所在.
标签:zend-navigation,zend-route,zend-framework,php 来源: https://codeday.me/bug/20191105/1998091.html