在li3中是否有可能不区分大小写的路由?

时间:2012-11-09 13:04:24

标签: php case-sensitive lithium

我想在Lithium创建一个匹配

的路线
  • / ABC
  • / ABC
  • / ABC

到目前为止,我有这样的事情:

Router::connect('/abc', array('Example::test'));

是否有可能将其更改为不敏感的内容。

感谢您的帮助,我在文档中找不到任何内容。

2 个答案:

答案 0 :(得分:4)

Route对象的模式参数允许您定义不区分大小写的模式(如Nils的其他答案中所述)。

我想指出,您还可以使用Router::formattersDispatcher规则来实现'/{:controller}/{:action}'路由的一般不区分大小写。它并不完美,但您可能会发现它很有用:

use lithium\action\Dispatcher;
use lithium\net\http\Router;
use lithium\util\Inflector;

/**
 * The following Router and Dispatcher formatters keep our
 * urls case-insensitive and nicely formatted using
 * lowercase letters and dashes to separate camel cased
 * controller and action names.
 *
 * Note that actions set in the routes file are also
 * passed through the Dispatcher's rules.  Therefore, we check if
 * there is a dash in the action before lower casing it to make it
 * case-insensitive.  For most of the framework and php, case sensitivity
 * is not an issue.  However, the templates are derived from the action
 * and controller names and case-sensitive file systems will cause
 * differences in case to not find the correct template.
 *
 * This solution is not complete.  It does not account for case sensitivity
 * with controller names (because lithium's default handling doesn't touch
 * the case of it and we're not overriding the default controller handling
 * since it does at least camelize the controller).  It also doesn't account
 * for one word actions because they don't contain a dash.  What probably needs
 * happen is the Dispatcher needs a formatter callback specifically for
 * translating urls.
 */
$slug = function($value) {
    return strtolower(Inflector::slug($value));
};
Router::formatters(array(
    'controller' => $slug,
    'action' => $slug
));
Dispatcher::config(array('rules' => array(
    'action' => array('action' => function($params) {
        if (strpos($params['action'], '-')) {
            $params['action'] = strtolower($params['action']);
        }
        return Inflector::camelize($params['action'], false);
    })
)));

答案 1 :(得分:3)

你应该能够这样做:

Router::connect('/{:dummy:[aA][bB][cC]}', array('Example::test'));

编辑:通过自己创建Route对象

,还有一种更好的方法
Router::connect(new Route(array(
        'pattern' => '@^/ab?$@i',
        'params' => array('controller' => 'example', 'action' => 'test'),
        'options' => array('compile' => false, 'wrap' => false)
)));

如果我把'@ ^ / ab?$ @ i'

上面的模式搞砸了
  • @ ==开始使用正则表达式
  • ^ ==行首
  • / ab ==寻找“/ ab
  • ? ==可选的尾部斜杠
  • $ ==行尾
  • @ ==结束正则表达式
  • i ==使其不区分大小写

您可以在此处找到更多信息: http://li3.me/docs/lithium/net/http/Route