PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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中找到该方法,并验证它是否被调用,看看是否可以找出问题所在.

大佬总结

以上是大佬教程为你收集整理的PHP-Zend导航和Zend Router问题-找不到正确的活动页面全部内容,希望文章能够帮你解决PHP-Zend导航和Zend Router问题-找不到正确的活动页面所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: