好的。我正在用 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;
}
所以现在一切都是数据库。在我的助推器中,我加载:
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
前端:
所以从图片中可以看出 URL 的变化——内容也发生了变化。所以路由器必须工作。
一切都只是“活跃”: http: //grab.by/6d6j
我知道我在这里粘贴了很多信息,但请帮助我。我喜欢在这个问题上工作了 20 多个小时——没有解决方案的帮助。
有点修好了。我不认为这是“正确的方式”,但仍然 - 它现在正在工作。我从导航中注释掉了控制器/动作/模块(Routes 中没有任何变化)。添加了“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);