开发者

Zend Route XML config does not work as expected

开发者 https://www.devze.com 2023-03-28 22:12 出处:网络
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

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" actio开发者_JAVA百科n="detail" />
    </property_detail>

</routes>


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.


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')
);
0

精彩评论

暂无评论...
验证码 换一张
取 消