使用Regex的CakePHP用户名URL?

Par*_*ris 5 regex routes cakephp friendly-url

我在这里发表评论:http://bakery.cakephp.org/articles/PHPdiddy/2006/10/06/custom-urls-from-the-site-root

那说:

只需改变最后一行.
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'));
Run Code Online (Sandbox Code Playgroud)

无论如何,目标是拥有一个像http:// domainname/username这样的URL ,映射到用户唯一的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'
        )
    );
Run Code Online (Sandbox Code Playgroud)

小智 4

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

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