Zend_Controller_Router_Route_Regex 允许 '?'在模式中

发布于 2024-10-17 07:57:37 字数 715 浏览 1 评论 0原文

想象一下情况,当 url 看起来像

/catalog/sectionIdent?page=1

时,页面参数是可选的。

当然,应该定义自定义路由。考虑以下代码:

    $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

/catalog/sectionIdent?page=1

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 技术交流群。

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

发布评论

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

评论(1

誰認得朕 2024-10-24 07:57:37

ZF 路由类在删除了查询字符串的 REQUEST_URI 上运行,因此您可能很难按照您期望的方式使其工作。但是,我相信 GET 参数默认放入请求对象中,因此您不需要在路由中满足它们。我建议更改您的路线以删除查询字符串部分:

$route = new Zend_Controller_Router_Route_Regex(
    'catalog/([-a-z]+)',
    array('controller'=>'catalog','action'=>'list'),
    array(1=>'section'),
    'catalog/%s'
);
$router->addRoute('catalog-section-page',$route); 

您仍然应该能够访问控制器中的参数,就像它们已由路线填充一样:

public function listAction()
{
    echo $this->_getParam('page');
}

并且您可以使用相同的方法设置默认值:

public function listAction()
{
    $page = $this->_getParam('page', 1); // defaults to 1 if no page in URL
}

您只需可能需要在那里清理它们(确保它们是数字)。

编辑:

使用此路由的 URL 帮助器示例:

echo $this->url(array('section' => 'foo', 'page' => 2), 'catalog-section-page') 

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:

$route = new Zend_Controller_Router_Route_Regex(
    'catalog/([-a-z]+)',
    array('controller'=>'catalog','action'=>'list'),
    array(1=>'section'),
    'catalog/%s'
);
$router->addRoute('catalog-section-page',$route); 

You should still be able to access the params in your controller as if they had been populated by the routes:

public function listAction()
{
    echo $this->_getParam('page');
}

and you can use the same method to set a default:

public function listAction()
{
    $page = $this->_getParam('page', 1); // defaults to 1 if no page in URL
}

You just may need to sanitise them there (make sure they are numeric).

Edit:

Example of URL helper with this route:

echo $this->url(array('section' => 'foo', 'page' => 2), 'catalog-section-page') 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文