Zend Route XML 配置未按预期工作

发布于 2024-11-29 18:59:40 字数 930 浏览 3 评论 0原文

下面是我的routes.xml,它被加载到我的Zend Framework 应用程序中。有两种路由,一种应该匹配 url /aanbod/tekoop/huis,另一种应该匹配 /aanbod/200/gerenoveerde-woning

问题是这两个例子url 以详细操作结束,而第一个应该以索引操作结束。

谁能澄清这个路由设置有什么问题吗?

<routes>

    <property_overview type="Zend_Controller_Router_Route">
        <route>/aanbod/:category/:type</route>
        <reqs category="(tekoop|tehuur)" />
        <reqs type="[A-Za-z0-9]+" />
        <defaults module="frontend" controller="property" action="index" />
    </property_overview>

    <property_detail type="Zend_Controller_Router_Route">
        <route>/aanbod/:propertyid/:slug</route>
        <reqs propertyid="[0-9]+" />
        <reqs slug="(^\s)+" />
        <defaults module="frontend" controller="property" action="detail" />
    </property_detail>

</routes>

Below is my routes.xml which gets loaded in my Zend Framework app. There are two routes, one should match the url /aanbod/tekoop/huis and the other should match /aanbod/200/gerenoveerde-woning

The problem is that both these example urls end up at the detail action while the first one should end up at the index action.

Can anyone clarify what's wrong with this routing setup?

<routes>

    <property_overview type="Zend_Controller_Router_Route">
        <route>/aanbod/:category/:type</route>
        <reqs category="(tekoop|tehuur)" />
        <reqs type="[A-Za-z0-9]+" />
        <defaults module="frontend" controller="property" action="index" />
    </property_overview>

    <property_detail type="Zend_Controller_Router_Route">
        <route>/aanbod/:propertyid/:slug</route>
        <reqs propertyid="[0-9]+" />
        <reqs slug="(^\s)+" />
        <defaults module="frontend" controller="property" action="detail" />
    </property_detail>

</routes>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能怎样 2024-12-06 18:59:40

试试这个:

<routes>

    <property_overview type="Zend_Controller_Router_Route">
        <route>aanbod/:category/:type</route>
        <reqs category="(tekoop|tehuur)" type="[A-Za-z0-9]+" />
        <defaults module="frontend" controller="property" action="index" />
    </property_overview>

    <property_detail type="Zend_Controller_Router_Route">
        <route>aanbod/:propertyid/:slug</route>
        <reqs propertyid="[0-9]+" slug="[^\s]+" />
        <defaults module="frontend" controller="property" action="detail" />
    </property_detail>

</routes>

我改变了什么:

  • 你应该只有一个“reqs”元素 - 添加不同的要求作为它的属性。这是您的路线无法正常工作的主要原因,因为每条路线中仅使用了一个请求
  • 删除最初的斜杠 - 这没有任何意义
  • 将 slug 模式更改为 [^\s]+ 的意思是“除空格之外的任何字符一次或多次”,我想这就是你的意思。

Try this instead:

<routes>

    <property_overview type="Zend_Controller_Router_Route">
        <route>aanbod/:category/:type</route>
        <reqs category="(tekoop|tehuur)" type="[A-Za-z0-9]+" />
        <defaults module="frontend" controller="property" action="index" />
    </property_overview>

    <property_detail type="Zend_Controller_Router_Route">
        <route>aanbod/:propertyid/:slug</route>
        <reqs propertyid="[0-9]+" slug="[^\s]+" />
        <defaults module="frontend" controller="property" action="detail" />
    </property_detail>

</routes>

What I've changed:

  • You should only have one 'reqs' element - add the different requirements as attributes of this. This is the main reason why your routes weren't working, as only one of the reqs was being used in each route
  • Remove the initial slash - this serves no purpose
  • Changed the slug pattern to [^\s]+ which means 'any character other than a space, one or more times', I think this is what you meant.
夜司空 2024-12-06 18:59:40

我认为您不能使用 reqs 参数来帮助识别 Zend_Controller_Router_Route 的路由。在您的情况下,您的路线是相同的,并且由于路线堆栈是后进先出的,因此“详细”路线优先。

也许尝试使用 Zend_Controller_Router_Route_Regex 相反。

我很难找到正则表达式路由器的配置方法,但在代码中它看起来像

$route = new Zend_Controller_Router_Route_Regex(
    'aanbod/(tekoop|tehuur)/([A-Za-z0-9]+)',
    array('controller' => 'property', 'action' => 'index', 'module' => 'frontend'),
    array(1 => 'category', 2 => 'type')
);

$route = new Zend_Controller_Router_Route_Regex(
    'aanbod/(\d+)/(\S+)',
    array('controller' => 'property', 'action' => 'detail', 'module' => 'frontend'),
    array(1 => 'propertyid', 2 => 'slug')
);

I don't think you can use the reqs parameter to help identify the route for Zend_Controller_Router_Route. In your case, your routes are identical and as the route stack is LIFO, the "detail" one takes precedence.

Perhaps try using Zend_Controller_Router_Route_Regex instead.

I'm having a hard time finding the configuration method for the regex router but in code it would look something like

$route = new Zend_Controller_Router_Route_Regex(
    'aanbod/(tekoop|tehuur)/([A-Za-z0-9]+)',
    array('controller' => 'property', 'action' => 'index', 'module' => 'frontend'),
    array(1 => 'category', 2 => 'type')
);

$route = new Zend_Controller_Router_Route_Regex(
    'aanbod/(\d+)/(\S+)',
    array('controller' => 'property', 'action' => 'detail', 'module' => 'frontend'),
    array(1 => 'propertyid', 2 => 'slug')
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文