2011-04-23 29 views
5

Tôi tìm thấy một bình luận ở đây: http://bakery.cakephp.org/articles/PHPdiddy/2006/10/06/custom-urls-from-the-site-rootCakePHP Tên người dùng URL bằng Regex?

Điều đó nói rằng:

Chỉ cần thay đổi dòng cuối cùng.
Bộ định tuyến :: kết nối ('/ ', mảng ('bộ điều khiển' => 'thành viên', 'hành động' => 'hiển thị'));
to
Bộ định tuyến :: kết nối ('(?! Admin | mục | hình ảnh) (.
*)', mảng ('controller' => 'members', 'action' => 'show'));

Một số người đã có thể nhận được rằng để làm việc ... Nó không nhìn hoàn toàn đúng với tôi mặc dù vậy tôi đã cố gắng điều sau đây, nhưng vẫn không có may mắn:

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

Trong mọi trường hợp , mục tiêu là có một url như http://domainname/username, ánh xạ tới một id duy nhất của người dùng. Nó hoạt động với/*, nhưng tôi không sử dụng phương thức đó. Ý tưởng?

Cập nhật giải pháp: Tôi đã sử dụng câu trả lời được chọn bên dưới và thêm vào phần sau. Nó có thể hữu ích cho người khác.

$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' 
     ) 
    ); 

Trả lời

4

Ở đây ya go. Bạn cần phải nắm bắt nó như là một param và sau đó tham chiếu nó trong regex. Tên người dùng sẽ có sẵn trong $ this-> params ['username'] trong hành động điều khiển.

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

Cảm ơn bạn !! Nó hoạt động hoàn hảo. Tôi đã thêm thông tin bổ sung cho câu hỏi bằng cách sử dụng giải pháp của bạn để bạn không cần phải nhập vào tất cả các tên bộ điều khiển vv! – Parris

-1
$misc = array(*your misc webroot, admin route items here...*); 
    $notList = array_merge(App::objects('plugin'),str_replace('Controller','',App::objects('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' => 'members', 'action' => 'show'),array('pass'=>array('username'),'username'=>'\b(?:(?!'.$notList.')\w)+\b')); 
Các vấn đề liên quan