Zend 路由正则表达式,可选参数 [英] Zend route regex, optional parameters

查看:35
本文介绍了Zend 路由正则表达式,可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些有关 Zend (Zend1) 中路线的帮助...

I need some help about my routes in Zend (Zend1) ...

我需要我可以这样使用我的路线:

I need that I can use my route like that :

http://mywebsite.com/myfolder/region/job/   
http://mywebsite.com/myfolder/region/job/add  
http://mywebsite.com/myfolder/region/job/add/page  
http://mywebsite.com/myfolder/region/job/page   

参数addpage 是可选的...

Parameters add and page are optional ...

这是我做的

$route = new Zend_Controller_Router_Route_Regex(
          'myfolder/([^/]+)/([^/]+)/?([^/]+)?/?([0-9]+)?',
          array('controller'    => 'myfolder','action'  => 'search'), 
          array(1 => 'region',2 => 'job', 3 => 'add', 4 => 'page'), 
          'myfolder/%s/%s/%s/%s'
         ); 

显然,它不起作用......我想要的是?我希望最后两个参数(addpage)是可选的 ...

Obviously, it doesn't work ... What I want? I want that last the two parameters (add and page) are optional ...

你能帮我吗?我的正则表达式有什么问题?

Can you help me? what's wrong with my regex?

好的,所以我试过了,但不行...
我需要参数 addpage 是可选的 ...

Ok, so I tried it, but isn't ok ...
I need that parameters add and page are optional ...

    $route = new Zend_Controller_Router_Route(
'myfolder/:region/:job/:add/:page',
array(
    'controller'    => 'myfolder',
    'action'        => 'search',
    'region'        => 'XX',
    'job'        => '',
    'add'    => '',
    'page'      => 1
),
array(
    'region' => '[a-zA-Z-_0-9-]+',
    'job' => '[a-zA-Z-_0-9-]+',
    'add' => '[a-zA-Z-_]+',
    'page' => '\d+'
)
);  

有了这个,这个 http://mywebsite.com/myfolder/region/job/page 不起作用...

With that, this one http://mywebsite.com/myfolder/region/job/page doesn't work ...

我也尝试过 'myfolder/:region/:job/*',但结果是一样的,不能如我所愿...
我真的想知道是否有可能......

I also tried with 'myfolder/:region/:job/*', but the result is same, doesn't work as I want ...
I really wonder if it is possible ...

$route = new Zend_Controller_Router_Route_Regex('myfolder/([^/]+)/([^/]+)(?:/|$)(?:(?!\d+(?:/|$))([^/]+)(?:/|$))?(?:(\d+)(?:/|$))?$',
      array('controller'    => 'myfolder', 'action' => 'recherche', 'presta' => ''), 
      array(1 => 'region',2 => 'job', 3 => 'presta', 4 => 'page'), 
      'myfolder/%s/%s/%s/%s');

推荐答案

做好准备.

myfolder/([^/]+)/([^/]+)(?:/|$)(?:(?!\d+(?:/|$))([^/]+)(?:/|$))?(?:(\d+)(?:/|$))?$

看到它在 RegExr 上运行(在 RegExr 上,我必须添加 \n\r 到否定类之一,因此它不匹配我所有的换行符,但实际上您可能不会处理换行符.)

See it working on RegExr (on RegExr, I had to add \n\r to one of the negated classes so it didn't match all my line breaks, in practice you probably won't be dealing with line breaks though.)

RegExr 需要注意的重要一点是,在第 4 种情况下,页码在第 4 个捕获组中,而在第 3 个组中没有任何内容.

The important thing to note on RegExr is that in the 4th case, the page number is in the 4th capture group, with nothing in the 3rd group.

myfolder/([^/]+)/([^/]+) 到这里一切都很好,还没有改变.

myfolder/([^/]+)/([^/]+) All looking good up to here, no changes yet.

(?:/|$) 匹配 / 或输入的结尾.

(?:/|$) Match a / or end of input.

接下来,我们有一个可选的非捕获组.这将是 add 部分.

Next, overall we have a non-capturing group that is optional. This would be the add section.

(?:(?!\d+(?:\|$))([^/]+)(?:/|$))?

现在让我们进一步分解:

Now lets break it down further:

(?!\d+(?:/|$)) 确保它不是页码 - 数字后跟 / 或输入结束.(负前瞻)

(?!\d+(?:/|$)) Make sure its not a page number - digits only followed by / or end of input. (Negative lookahead)

([^/]+) 我们的捕获组 - 示例中的 add.

([^/]+) Our capture group - add in the example.

(?:/|$) 匹配 / 或输入的结尾.

(?:/|$) Match a / or end of input.

现在对于我们的页码组,它也是可选的和非捕获的:

Now for our page number group, again it's optional and non-capturing:

(?:(\d+)(?:/|$))? 捕获数字,然后再次匹配/或输入结束.

(?:(\d+)(?:/|$))? Captures the numbers, then matches / or end of input again.

$ 为了防止它尝试匹配实际匹配的子字符串,我在输入锚的另一端(因为你可以匹配任意多的一行),尽管正则表达式没有它的功能.

$ And just in case it tries to match substrings of actual matches, I threw in another end of input anchor (since you can match as many in a row as you like), although the regex functions without it.

你基本上想要的是一种这样做的方式:

What you basically want is a way of doing this:

目前第二个和第三个参数是:

At the moment the 2nd and 3rd parameters are:

array(1 => 'region',2 => 'job', 3 => 'add', 4 => 'page'), 
'myfolder/%s/%s/%s/%s'

你希望它们是这样的:

array(1 => 'region',2 => 'job', 3 => '/'+'add', 4 => '/'+'page'), 
'myfolder/%s/%s%s%s'

如果存在可选组,则仅在其中添加 /.上面的代码不起作用,但也许您可以通过某种方式实现它.

Where you only add the / if the optional group is present. The code above won't work but perhaps there is some way you could implement that.

这篇关于Zend 路由正则表达式,可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆