regex - CakePHP 用户名 URL 使用正则表达式?

标签 regex cakephp routes friendly-url

我在这里找到了一条评论:http://bakery.cakephp.org/articles/PHPdiddy/2006/10/06/custom-urls-from-the-site-root

那说:

Just change last line.
Router::connect('/', array('controller' => 'members', 'action' => 'show'));
to
Router::connect('(?!admin|items|images)(.
*)', array('controller' => 'members', 'action' => 'show'));



有些人能够让它发挥作用......虽然对我来说看起来不太合适,所以我尝试了以下操作,但仍然没有运气:
Router::connect('(?!/admin|/items|/images)(/.*)',
array('controller' => 'members','action' => 'show'));

无论如何,目标是拥有一个像 http://domainname/username 这样的网址。 , 映射到用户的唯一 ID。它适用于/*,但我宁愿不使用该方法。想法?

更新解决方案:
我使用了下面选择的答案并添加了以下内容。它可能对其他人有帮助。
$misc = array(*your misc webroot, admin route items here...*);
$notList = array_merge(Configure::listObjects('plugin'),Configure::listObjects('controller'));
$notListLowerCase = array();
foreach ($notList as $key=>$item):
    $notListLowerCase[] = strtolower(preg_replace("/(.)([A-Z])/","\\1_\\2",$item));
endforeach;
$notList = array_merge($notList,$misc,$notListLowerCase);
$notList = implode('|', $notList);

Router::connect('/:username', 
        array(
            'controller'=>'users', 
            'action'=>'view'
        ),   
        array(
            'username' => '\b(?:(?!'.$notList.')\w)+\b'
        )
    );

最佳答案

给你。您需要将其捕获为参数,然后在正则表达式中引用它。用户名将在 Controller 操作中的 $this->params['username'] 中可用。

Router::connect('/:username', 
    array(
        'controller'=>'members', 
        'action'=>'show'
    ),   
    array(
        'username' => '\b(?:(?!admin|items|images)\w)+\b'
    )
);

关于regex - CakePHP 用户名 URL 使用正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5766562/

相关文章:

CodeIgniter,一条 route 多个可能的段

routes - 在 Laravel 4 路由中接受编码的 URL

javascript - 帮助正则表达式 - 我怎样才能让一些网址不跟随?

python - 当文本略有不同时替换列名称

mysql - CakePHP 3.x - 如何通过另一个关联的字段过滤一个关联?

Cakephp删除特定语言的翻译

java - 如何修复以下正则表达式?

c# - 正则表达式性能优化

cakephp - 在自定义组件中使用 session 组件

c# - webforms 中的输出缓存 varybyparams 是否理解路由参数?