【问题标题】:Regex Routing - rule not being found正则表达式路由 - 找不到规则
【发布时间】:2012-05-13 15:47:55
【问题描述】:

我正在定义用于清理我的 URL 的正则表达式路由。这个想法是用户添加的所有页面都将使用 URL www.example.com/page-slug,而不是使用实际控制器 www.example.com/userpages/page-slug。其他页面会遵循标准的module:controller:action路由方案。

我正在尝试使用路由器优先级来实现这一点。

我已经定义了下面的方案..

class Default_Bootstrap extends Zend_Application_Module_Bootstrap{  

protected function _initRoute() {

    $front = Zend_Controller_Front::getInstance();
    $router = $front->getRouter(); // returns a rewrite router by default

    $route['index'] = new Zend_Controller_Router_Route_Regex(
        '/',
        array(
        'module'     => 'default',
        'controller' => 'index',
        'action'     => 'index'
        )
    );
    $route['contact'] = new Zend_Controller_Router_Route_Regex(
        'contact/(\d+)',
        array(
        'module'     => 'default',
        'controller' => 'contact',
        'action'     => 'index'
        )
    );
    $route['research'] = new Zend_Controller_Router_Route_Regex(
        'research/(\d+)',
        array(
        'module'     => 'default',
        'controller' => 'research',
        'action'     => 'index'
        )
    );
    $route['account'] = new Zend_Controller_Router_Route_Regex(
        'account/(\d+)',
        array(
        'module'     => 'default',
        'controller' => 'account',
        'action'     => 'index'
        )
    );
    $route['userpages'] = new Zend_Controller_Router_Route_Regex(
        '/(.+)',
        array(
        'module'     => 'default',
        'controller' => 'userpages',
        'action'     => 'index'
        ),
        array(
        'slug' => 1
        ),
        '%s'
    );

    $router->addRoute('userpages',     $route['userpages']);
    $router->addRoute('contact',       $route['contact']);
    $router->addRoute('research',      $route['research']);
    $router->addRoute('account',       $route['account']);
    $router->addRoute('index',         $route['index']);

}

}

在路由器优先级的情况下一切正常,可确保索引/帐户/研究/联系页面选择正确的控制器。但是,当尝试访问“用户页面”路由所涵盖的 URL 时,例如“about-us”,未找到最终捕获所有路线,导致...

Message: Invalid controller specified (about-us) 
.
.
.
Request Parameters:

array (
  'controller' => 'about-us',
  'action' => 'index',
  'module' => 'default',
)  

知道我在哪里出错了吗?在我看来,正则表达式是正确的“/(.+)”应该捕捉到所有不是索引页面的东西。

编辑:@phatfingers,好吧,你是对的,我已将 "\d+" 编辑为 ".+" 以捕捉一个或多个任意字符。问题仍然存在。事实上,在更改正则表达式之前,我尝试了 URL www.example.com/52,并得到了同样的错误 - “指定的控制器无效 (52)”。更改后 - 使用上面编辑的 sn-p 代码,规则仍然无法找到任何匹配项。

【问题讨论】:

  • 正则表达式“(\d+)”只匹配数字。示例网址“www.example.com/page-slug”不以数字结尾。
  • 感谢@phatfingers 的观​​察,请参阅上面的编辑 - 规则仍然与预期不匹配
  • 我认为您必须在“用户页面”正则表达式中删除正斜杠,即 ('.+)
  • @Arian World,谢谢。我显然误解了正则表达式参数的工作方式。您的建议有效,但现在,我不确定为什么。我认为需要再看一遍手册。

标签: zend-framework


【解决方案1】:

在 'userpages' 正则表达式中删除正斜杠,即只是 ('.+)

引用直接来自手册 Zend Router 和 Router_Regex 但它也适用于所有路由。

注意:前导和尾随斜杠从 URL 中的 URL 中删除 比赛前的路由器。结果,匹配 URL http://domain.com/foo/bar/,将涉及 foo/bar 的正则表达式,而不是 /foo/bar.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 2019-01-01
    • 2014-04-14
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多