简体   繁体   English

Zend_Controller_Router_Exception:未指定“xyz”

[英]Zend_Controller_Router_Exception: “xyz” is not specified

I have a problem in my current Zend Framework application. 我在当前的Zend Framework应用程序中遇到了问题。

In my Bootstrap I register these routes: 在我的Bootstrap中,我注册了以下路线:

protected function _initRouter()
{
    $this->bootstrap("FrontController");
    $frontController = $this->getResource("FrontController");

    $route = new Zend_Controller_Router_Route(
        ":module/:id",
        array(
            "controller" => "index",
            "action" => "index"
            ),
        array("id" => "\d+")
        );
    $frontController->getRouter()->addRoute('shortcutOne', $route);

    $route = new Zend_Controller_Router_Route(
        ":module/:controller/:id",
        array("action" => "index"),
        array("id" => "\d+")
        );
    $frontController->getRouter()->addRoute('shortcutTwo', $route);

    $route = new Zend_Controller_Router_Route(
        ":module/:controller/:action/:id",
        null,
        array("id" => "\d+", "action" => "\w+")
        );
    $frontController->getRouter()->addRoute('shortcutThree', $route);
}

Now later I added Zend_Navigation to my project. 后来我将Zend_Navigation添加到我的项目中。 I have several modules, that register navigation elements in the module bootstrap: 我有几个模块,在模块引导程序中注册导航元素:

<?php

class Contact_Bootstrap extends Zend_Application_Module_Bootstrap
{
    protected function _initNavigation()
    {
        $layout = $this->getApplication()->getResource("layout");
        $view = $layout->getView();

        $config = new Zend_Config_Xml(dirname(__FILE__)."/config/navigation.xml", "nav");

        $view->navigation()->addPage($config);
    }
}

When I open the app in my browser everything works fine. 当我在浏览器中打开应用程序时,一切正常。 That means I can see the navigation and click on its links to view the specified page. 这意味着我可以看到导航并单击其链接以查看指定的页面。 But when I hit a page that uses the :module/:action/:id route, the Navigation Helper throws a Zend_Controller_Router_Route exception: 但当我点击使用:module /:action /:id路由的页面时,Navigation Helper会抛出Zend_Controller_Router_Route异常:

Fatal error: Zend_Controller_Router_Exception: id is not specified in C:\\Entwicklung\\kt\\trunk\\src\\library\\Zend\\View\\Helper\\Navigation\\HelperAbstract.php on line 519 致命错误:Zend_Controller_Router_Exception:在第519行的C:\\ Entwicklung \\ kt \\ trunk \\ src \\ library \\ Zend \\ View \\ Helper \\ Navigation \\ HelperAbstract.php中未指定id

Did anyone of you experience this error too? 你们中的任何人都遇到过这个错误吗? It would be great if you could help me or give me advise. 如果你可以帮助我或给我建议,那将是很棒的。 Also if you need more information on my setup etc., just tell me. 如果您需要有关我的设置等的更多信息,请告诉我。

Thank you! 谢谢!

I found the solution myself. 我自己找到了解决方案。

This problem was caused due to a not-so-beneficial behavior of the Zend_Navigation elements. 此问题是由于Zend_Navigation元素的不太有益的行为引起的。 In their getHref() method they use the URL helper. 在他们的getHref()方法中,他们使用URL帮助器。 This helper creates the URL the navigation entry has to link to, taking in the arguments specified for the navigation element (module, controller, action, etc.) 此助手创建导航条目必须链接到的URL,接受为导航元素指定的参数(模块,控制器,操作等)

Now, the problem is that, if you have created a custom route in your bootstrap just as I did, for example 现在,问题在于,如果您像我一样在引导程序中创建了自定义路由,例如

":module/:controller/:id"

You run into the problem that when this route is being used, the URL helper uses that route to generate the link for the navigation entry. 您遇到的问题是,当使用此路由时,URL帮助程序使用该路由生成导航条目的链接。 However, I did not pass on an additional "id" parameter, so I got the exception. 但是,我没有传递额外的“id”参数,所以我得到了例外。

So one solution is to pass an additional parameter for each Zend_Navigation_Page_Mvc instance "route" which is set to "default". 因此,一种解决方案是为每个Zend_Navigation_Page_Mvc实例“route”传递一个附加参数,该实例设置为“default”。

Another solution, which I didn't try yet, is to let the new custom route just re-map itself onto the default route, like: 我还没有尝试的另一个解决方案是让新的自定义路由重新映射到默认路由,例如:

":module/:controller/:action/id/$id"

But I don't know if you have the ability with Zend Framework to do that. 但我不知道你是否有能力使用Zend Framework来做到这一点。

$route = new Zend_Controller_Router_Route( 
    "info/:id", 
    array( 
        "controller" => "index", 
        "action" => "index" 
    ), 
    array("id" => "\d+") 
); 

changed to 变成

$route = new Zend_Controller_Router_Route( 
    "info/:id", 
    array( 
        "controller" => "index", 
        "action" => "index",
        "id" => "default"
    )
); 

worked for me, but probably isn't the best way to go 为我工作,但可能不是最好的方式

I have faced the same problem and here the solution that worked for me: I added route parameter to my all links in my navigation.xml file as below 我遇到了同样的问题,这里的解决方案对我有用:我在我的navigation.xml文件中添加了route参数到我的所有链接,如下所示

    <?xml version="1.0" encoding="UTF-8"?>
    <navigation>
        <mainmenu>
        <home>
                <label>topmenu:homepage</label>
                ===> <route>default</route> <===
                <module>default</module>
                <controller>index</controller>
                <action>index</action>
        </home>
    </mainmenu>
</navigation>

In this route ":module/:controller/:id" you need simply define default value for :id 在此路由“:module /:controller /:id”中,您只需为:id定义默认值

Example: 例:

<content type="Zend_Controller_Router_Route">
    <route>content/:engine</route>
    <defaults module="content" controller="engine" action="index" engine="" />
</content>

Sorry for my English :) 对不起我的英语不好 :)

Regards. 问候。

I've just encountered a problem similar to yours but have found a different workaround for it. 我刚刚遇到了类似于你的问题但是找到了一个不同的解决方法。 I'll share it here just for the sake of information. 我将在这里分享,仅仅是为了提供信息。

Just like you, my problem seemed to be that my navigation was built using the route last used. 就像你一样,我的问题似乎是我的导航是使用上次使用的路线构建的。 This meant that if one of my custom routes had been used, I'd get a stack of exceptions complaining about some custom parameters for this given route not being defined. 这意味着如果我的一个自定义路由已被使用,我会得到一堆异常,抱怨某些自定义参数没有被定义。

Most of my routes' custom parameters' value default to null in my route definition. 我的路由定义中的大多数路由“自定义参数”值默认为null。 I discovered if I changed the default value to '' instead, the exception would actually disappear. 我发现如果我将默认值更改为'',则异常实际上会消失。 I couldn't really understand this so I began googling a bit and stumbled across your post which led me onto the right path - I think. 我真的不明白这一点,所以我开始谷歌搜索,偶然发现你的帖子,这让我走上了正确的道路 - 我想。

My navigational elements are stored in a database so I generate all my Zend_Navigation_Page elements upon request and pass the container to my view helpers (I do serialize my container object to make it cacheable, but that's not relevant right now). 我的导航元素存储在数据库中,因此我根据请求生成所有Zend_Navigation_Page元素,并将容器传递给我的视图助手(我会将容器对象序列化以使其可缓存,但现在不相关)。 I didn't really find that changing the params' default value to '' seeemed like a "good" way of doing it, so hinted by your post I tried to change all my page elements, stored in my database, to use a route named 'default' unless they explicitly was meant to use a custom route and had the params defined for it. 我并没有真正发现将params的默认值更改为''看起来像一个“好”的方式,所以我的帖子暗示我试图更改存储在我的数据库中的所有页面元素,以使用路由命名为'default',除非它们明确意味着使用自定义路由并为其定义了params。 Explicitly defining my navigational points to use the default route seemed to do the trick also and to me it seems as a more clean workaround. 明确地定义我的导航点以使用默认路线似乎也可以做到这一点,对我来说它似乎是一个更干净的解决方法。

Thanks for the hint to what was causing this error - with the exception, I really had no clue about where to start :) 感谢提示导致此错误的原因 - 除了例外,我真的不知道从哪里开始:)

I see here two solutions: 我在这看到两个解决方案:

  1. You can set a default value for your route parameters in the _initRouter(), but it may be not what you want (eg. in this case this route may be used when you do not want) 您可以在_initRouter()中为路由参数设置默认值,但它可能不是您想要的(例如,在这种情况下,您可以在不需要时使用此路由)

  2. You can specify the route parameters in the navigation.xml (the params property). 您可以在navigation.xml中指定路由参数(params属性)。 If you don't want to set them fixed, you will need to write PHP code for the generation of the navigation xml. 如果您不想将它们设置为已修复,则需要编写PHP代码以生成导航xml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM