可选参数 Zend Route Regex

发布于 2024-09-04 05:47:02 字数 3243 浏览 7 评论 0原文

如何使用 Zend 使路由正则表达式参数可选?

我尝试为搜索过滤器制作格式良好的 URL(“search?s=mp&t=w”而不是“search/index/s/mp/t/w”),例如。 :

受欢迎程度

  • 最受欢迎 (s=mp)
  • 浏览次数最多(s=mv,默认)
  • 评分最高 (s=tr)
  • 评论最多 (s=mc)

时段

  • 所有时段(t=a,默认)
  • 今天 (t=d)
  • 本周(t=w)
  • 这个月 (t=m)

因此,要从今天开始获得所有评分最高的项目,我将拥有: search?s=tr&t=d

使用正则表达式路由,我必须指定默认值,问题是 url视图助手生成带有默认值而不是当前值的链接。

这是我的路线:

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"

和链接:

<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>

例如,如果当前页面是“search?s=tr&t=d”并且我点击“本周”,则由于默认值,链接是:“search?s=mv&t=w”而不是“search?s=tr&t=w”

我必须指定默认值,否则会出现错误。

有什么想法吗?

谢谢, 本杰明.

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.

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

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

发布评论

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

评论(2

木森分化 2024-09-11 05:47:02

我没有使用正则表达式路由,但我看到了这个错误。基本上,defaults.[param] 部分需要值。我的自定义路线,我将它们设置为空:

; 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 = ".*"

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 = ".*"
素染倾城色 2024-09-11 05:47:02

您确定要这样做吗?有几次我尝试“对抗框架”,但发现框架更了解(笑)。我可以建议另一种方式吗? (谷歌可能更喜欢:让你的网址也更加“人性化”)。使用类似

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

将“小部件”替换为网站内容的 URL,例如“视频”、“博客文章”、“独轮车”等。

然后将上述每条路由发送到您的搜索控制器

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文