Zend Navigation和Zend Router问题 - 找不到正确的活动页面

时间:2010-09-02 21:50:16

标签: php zend-framework zend-navigation zend-route

好。我正在用Zend构建CMS。它不像看起来那么简单,但仍然是 - 对我来说最好的解决方案。我有一个带有ID和PARENT的树系统,PARENT标记在孩子所在的页面下。无论如何。简单的东西。

每次创建或排序页面时Nav&路线被重新生成。

我将复制整个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总是显示最深的元素

页面选择工作正常! param pageId传递正确,路由器工作。导航只是搞砸了。

给你一些想法的照片:

管理员方面:   - http://grab.by/6d67

前端方面:

从图片中看到URL更改 - 内容也发生了变化。所以路由器必须工作。

一切都只是“活跃”:http://grab.by/6d6j

我知道我在这里粘贴了很多信息,但请帮助我。我喜欢在这个问题上工作20多个小时 - 没有解决方案帮助。

有点修好。我不认为这是“正确的方式”,但仍然 - 它现在正在运作。我从导航事件中注释了控制器/动作/模块(路由中没有任何变化)。添加了“id”=&gt; “页 - ” $页[ “ID”]

现在在我的Global_Setup中,我做了类似的事情 - &gt;

$navigation = new Zend_Navigation(new Zend_Config(Crcms_Config::getConfig("navigation_".$session->locale)));
$navigation->findBy("id", "page-".$request->getParam("pageId"))
->setActive(true);

1 个答案:

答案 0 :(得分:4)

这是一种猜测,因为通过查看代码很难解决这个问题。

当您构建路线时,您正在设置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中找到该方法,验证它是否被调用,看看你是否能弄明白它出了什么问题。