Zend_Controller_Router_Route_Regex 允许 '?'在模式中
想象一下情况,当 url 看起来像
时,页面参数是可选的。
当然,应该定义自定义路由。考虑以下代码:
$route = new Zend_Controller_Router_Route_Regex(
'catalog/([-a-z]+)(?:\?page=([0-9]*))?',
array('controller'=>'catalog','action'=>'list','page'=>''),
array(1=>'section',2=>'page'),
'catalog/%s?page=%d'
);
$router->addRoute('catalog-section-page',$route);
但是该路由不会用“?”触发url 中的符号。
没有 '?' (例如,通过在模式中添加转义的“!”符号)一切都按其应有的方式工作。 有什么办法可以实现“?”吗?存在于自定义定义的正则表达式路由中吗?也许我在模式上做错了什么?
PS:不要使用“/”代替“?”,问题正是关于 Zend_Controller_Router_Route_Regex 实现中的模式限制。
Imagine situation, when the url should looks like
where page param is optional.
Of course, custom route should be defined. Consider the following code:
$route = new Zend_Controller_Router_Route_Regex(
'catalog/([-a-z]+)(?:\?page=([0-9]*))?',
array('controller'=>'catalog','action'=>'list','page'=>''),
array(1=>'section',2=>'page'),
'catalog/%s?page=%d'
);
$router->addRoute('catalog-section-page',$route);
But this route won't be triggered with '?' symbol in url.
Without '?' (for example, by adding escaped '!' symbol to pattern) everything works as it should.
Is there any way to achieve '?' presence in custom defined regex route? Maybe I'm doing something wrong in pattern?
P.S.: Don't offer to use '/' instead of '?', question is exactly about pattern restrictions in Zend_Controller_Router_Route_Regex implementation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ZF 路由类在删除了查询字符串的 REQUEST_URI 上运行,因此您可能很难按照您期望的方式使其工作。但是,我相信 GET 参数默认放入请求对象中,因此您不需要在路由中满足它们。我建议更改您的路线以删除查询字符串部分:
您仍然应该能够访问控制器中的参数,就像它们已由路线填充一样:
并且您可以使用相同的方法设置默认值:
您只需可能需要在那里清理它们(确保它们是数字)。
编辑:
使用此路由的 URL 帮助器示例:
The ZF routing classes operate on the REQUEST_URI with the query string stripped off, so you may have a hard time get this working in the way you are expecting. However, I believe GET parameters are put into the request object by default, so you shouldn't need to cater for them in your routes. I'd suggest changing your route to remove the query string parts:
You should still be able to access the params in your controller as if they had been populated by the routes:
and you can use the same method to set a default:
You just may need to sanitise them there (make sure they are numeric).
Edit:
Example of URL helper with this route: