Zend 正则表达式路线帮助
我正在尝试创建一个 Zend_Controller_Router_Route_Regex 路由来处理以下形式的 URL:
search?q=chicken/page=2
其中第一个正则表达式子模式是 chicken
,第二个是将是2
。至于第二部分,其中page=2
,如果是第一页,我想将其设为可选,即page=1
。因此,另一个 url(例如 search?q=chicken
)也是有效的,并且等同于 search?q=chicken/page=1
。
这是我的尝试,尽管没有成功,但可以让您更好地了解我正在尝试做的事情。
$route = new Zend_Controller_Router_Route_Regex(
'search\?q=([a-zA-Z0-9]+)(?:/page=(\d+))',
array(
'page'=> '1',
'module' => 'default',
'controller' => 'search',
'action' => 'index' ),
array( 1 => 'query', 2 => 'page' ),
'search?=%s/page=%d');
$router->addRoute('search', $route);
这里的问题是我无法编写正确的正则表达式。
提前致谢。
编辑 #1
正如 MA4 所指出的,正确的正则表达式是 'search\?q=([a-zA-Z0-9]+)(?:/page=(\d+))?'
Darryl 指出了真正的问题。这里有更多信息可以帮助您正确看待事情。
我的搜索文本框和按钮
<form action="/search" method="get">
<input type="text" name="q" />
<input type="submit" value="Search" />
</form>
每次按下 search
按钮时,我都会收到 search?q=[text] 请求。如何强制它通过正则表达式匹配路线?
这是我想做的,但是代码不起作用
if($this->getRequest()->getParam('query')){
// redirect success
} else {
$url = "search?q=" . $this->_getParam('q');
$this->_redirect(route('search'), array('code' => 301 ));
}
I am trying to create a Zend_Controller_Router_Route_Regex route to process URLs in the following form:
search?q=chicken/page=2
where the first regex subpattern would be chicken
and second one would be 2
. As for the second part where page=2
, I want to make it optional if it is the first page, that is page=1
. So another url such as search?q=chicken
would also be valid and is equivalent to search?q=chicken/page=1
.
Here is my attempt albeit without any success, but to give you a better picture of what I am trying to do.
$route = new Zend_Controller_Router_Route_Regex(
'search\?q=([a-zA-Z0-9]+)(?:/page=(\d+))',
array(
'page'=> '1',
'module' => 'default',
'controller' => 'search',
'action' => 'index' ),
array( 1 => 'query', 2 => 'page' ),
'search?=%s/page=%d');
$router->addRoute('search', $route);
The problem here is I cant compose the correct regex.
Thanks in advance.
EDIT #1
The correct regex, as pointed out by MA4, is 'search\?q=([a-zA-Z0-9]+)(?:/page=(\d+))?'
The real problem is pointed by Darryl. Here is a little bit more info to put things into perspective.
My search text box and button
<form action="/search" method="get">
<input type="text" name="q" />
<input type="submit" value="Search" />
</form>
Every time I press the search
button, I get the search?q=[text] request. How do I force it to go through the regex match route?
Here is what i want to do, however the code does not work
if($this->getRequest()->getParam('query')){
// redirect success
} else {
$url = "search?q=" . $this->_getParam('q');
$this->_redirect(route('search'), array('code' => 301 ));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
/search?q=chicken/page=2
无法被 Zend Frameworks 的路由器解析。路由器只会看到/search
。路由器依赖于服务器提供的路径信息以及 ? 之后的任何内容。是查询字符串。
您需要使用这样的路径:
在这种情况下,您的正则表达式会变得更加简单。
/search?q=chicken/page=2
is not parsable by Zend Frameworks' router. The router will only see/search
.The router relies on the Path Info provided by the server and anything after the ? is the query string.
You would need to use a path such as this:
In which case your regex would become much simpler.
通过在其后添加
?
使第二部分成为可选:make the second part optionnal by adding a
?
after it: