Php Zend导航和Zend路由器问题-找不到正确的活动页面
好的。我正在和Zend建立一个CMS。这并不像看上去那么简单,但对我来说仍然是最好的解决方案。我有一个带有ID和父项的树系统,父项标记是子项所在页面下的标记。无论如何简单的东西 每次创建或排序页面时,都会重新生成导航和路由 我将在这里复制用于创建导航和路线的整个管理页面模型代码 导航在此处创建: (我认为不需要模块/控制器/操作信息,因为它是从路由器加载的) 最后,在将导航保存到数据库之前,它调用$this->createRoutes();代码如下:Php Zend导航和Zend路由器问题-找不到正确的活动页面,php,zend-framework,zend-navigation,zend-route,Php,Zend Framework,Zend Navigation,Zend Route,好的。我正在和Zend建立一个CMS。这并不像看上去那么简单,但对我来说仍然是最好的解决方案。我有一个带有ID和父项的树系统,父项标记是子项所在页面下的标记。无论如何简单的东西 每次创建或排序页面时,都会重新生成导航和路由 我将在这里复制用于创建导航和路线的整个管理页面模型代码 导航在此处创建: (我认为不需要模块/控制器/操作信息,因为它是从路由器加载的) 最后,在将导航保存到数据库之前,它调用$this->createRoutes();代码如下: public function cr
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());
}
这是我的全局设置:
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>
这是一种猜测,因为仅仅通过查看代码很难解决这个问题 建立路由时,您要设置一个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中找到该方法,验证是否调用了该方法,然后看看是否可以找出哪里出了问题。好的。在Global\u Setup var\u dump->current()中,导航对象提供了超级多行。我认为每个人都是“当下”的佼佼者!成功了。现在回想一下——这将是最符合逻辑的解释:)-有时候人们需要一些方向。非常感谢!
$router["route_".$row["id"]] = array(
"route" => $slugPath.".html",
"defaults" => array(
"pageId" => $row["id"],
"locale" => $row["locale"],
"module" => "frontend",
"controller" => "page",
"action" => "show"
)
);
$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"])
);