jkulak
jkulak

Reputation: 918

How to define one route to handle request with page parameter and without it?

I would need one route definition (if it's possible), that will handle both requestes:

First one is displaying first 10 elements of the list, second is displaying second "page" with next 10 elements.

IMPORTANT: I would also like (in View):

$this->url(array(), 'list');

To create http://www.exaple.com/list URL, and not http://www.example.com/list/1

What I have already is:

<list type="Zend_Controller_Router_Route_Regex">
    <route>list(?:/(\d+))?</route>
    <defaults>
        <controller>index</controller>
        <action>list</action>
        <page>1</page>
    </defaults>
    <map>
        <page>1</page>
    </map>
    <reverse>list/%d</reverse>
</list>

It handles both request, but creates http://www.example.com/list/1 URL on reverse.

If it's not possible with one route - what is the best way to hadle that kind of situation?

Upvotes: 0

Views: 215

Answers (1)

Tim Fountain
Tim Fountain

Reputation: 33148

You need to provide null as the default value for page instead of 1, but I don't think this is possible using XML routes as Zend_Config_Xml does not support null as a special value (it will interpret as the string 'null').

You can sort of work around this using the standard route class:

<list type="Zend_Controller_Router_Route">
<route>list/:page</route>
    <defaults>
        <controller>index</controller>
        <action>list</action>
        <page>null</page>
    </defaults>
    <reqs>
        <page>\d+</page>
    </reqs>
</list>

The only quirk here is that you will get the string value 'null' as the page param if there is no page variable in the URL.

If you were to use an ini file for routes, or define them in PHP you shouldn't have this issue.

Upvotes: 1

Related Questions