我在这里找到了一条评论: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/