views:

371

answers:

2

Hello,

How to make route regex parameters optionals with Zend ?

I try to make well formatted URLs ("search?s=mp&t=w" instead of "search/index/s/mp/t/w") for search filters, ex. :

Popularity

  • Most popular (s=mp)
  • Most viewed (s=mv, default)
  • Top rated (s=tr)
  • Most commented (s=mc)

Period

  • All period (t=a, default)
  • Today (t=d)
  • This week (t=w)
  • This month (t=m)

So, to get all top rated items from today i will have : search?s=tr&t=d

With regex routes i must specify defaults values and the problem is that the url view helper generates links with the default values and not with the current values.

Here is my route :

resources.router.routes.search.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.search.route = "search\?s\=(.+)\&t\=(.+)"
resources.router.routes.search.map.1 = s
resources.router.routes.search.map.2 = t
resources.router.routes.search.defaults.module = front
resources.router.routes.search.defaults.controller = search
resources.router.routes.search.defaults.action = index
resources.router.routes.search.defaults.s = mv
resources.router.routes.search.defaults.t = a
resources.router.routes.search.reverse = "search?s=%s&t=%s"

and links :

<div class="filters note">
    <div class="filters-content">
        <h3>Popularity</h3>
        <ul class="filters-list">
            <li>
                <a href="<?=$this->url(array('s' => 'mp'), 'search')?>">
                    Most popular
                </a>
            </li>
            <li>
                <a href="<?=$this->url(array('s' => 'mv'), 'search')?>">
                    Most viewed
                </a>
            </li>
            <li>
                <a href="<?=$this->url(array('s' => 'tr'), 'search')?>">
                    Top rated
                </a>
            </li>
            <li>
                <a href="<?=$this->url(array('s' => 'mc'), 'search')?>">
                    Most commented
                </a>
            </li>
        </ul>
    </div>
</div>
<div class="filters period">
    <div class="filters-content">
        <h3>Period</h3>
        <ul class="filters-list">
            <li>
                <a href="<?=$this->url(array('t' => 'a'), 'search')?>">
                    All period
                </a>
            </li>
            <li>
                <a href="<?=$this->url(array('t' => 'd'), 'search')?>">
                    Today
                </a>
            </li>
            <li>
                <a href="<?=$this->url(array('t' => 'w'), 'search')?>">
                    This week
                </a>
            </li>
            <li>
                <a href="<?=$this->url(array('t' => 'm'), 'search')?>">
                    This month
                </a>
            </li>
        </ul>
    </div>
</div>

For example, if current page is "search?s=tr&t=d" and i clic on "This week", the link is : "search?s=mv&t=w" instead of "search?s=tr&t=w" because of the default values.

I must specify default values or i get an error.

Any idea ?

Thanks, Benjamin.

A: 

I haven't used the regex routes, but I have seen this error. Basically, the defaults.[param] parts need values. I my custom route, I am setting them to be empty:

; Navigation ID Route (uses navigation id)
resources.router.routes.nav.route = "p/:id/:title/*"
resources.router.routes.nav.defaults.module = "default"
resources.router.routes.nav.defaults.controller = "index"
resources.router.routes.nav.defaults.action = "index"
resources.router.routes.nav.defaults.id = 
resources.router.routes.nav.defaults.title = 
resources.router.routes.nav.reqs.id = "\d+"
resources.router.routes.nav.reqs.title = ".*"
Sonny
Benjamin
A: 

are you sure you want to do it this way? a few times i've tried to "fight the framework" but found out the framework knew better (grin). can i suggest another way? (that Google may like better: makes your URLs more "people friendly" too). Use URLs like

/top-rated-widgets/today
/most-viewed-widgets/this-month
/most-commented-widgets 

where you replace "widgets" with whatever your site is about eg "videos", "blog posts", "unicycles" whatever.

then each of the above routes to your search controller

Steve
Hello, that's what i finally did :)Thanks.
Benjamin