2011-11-24 33 views
0

不可見的ID連接問題,我想下面的SEO鏈接,如:CakePHP的2.0路由器::在URL

www.example.com/users/profile/webfacer 

我不想使用獨特的用戶從數據庫中獲取。

我嘗試在我的AppController中使用路由器方法connect。但我意識到,這是不可能的(或者不知道它現在使用在他們還routes.php文件中使用不幫助)是這樣的:

//in AppController 
Router::connect('/users/profile/:name', 
    array(
     'controller' => 'users', 
     'action'  => 'profile' 
    ) , 
    array(
     'pass' => array('id', 'name'), 
     'id' => '[0-9]+' 
    ) 
); 

我怎樣才能重現此鏈接(以下例如)與這個網站的鏈接助手發送ID,但在URL中沒有表現出來:

$this->Html->link('webfacer',array(
     'controller'=>'users', 
     'action'=>'profile', 
     'id'=>1, 
     'name'=>'webfacer' 
)); 

這將輸出www.example.com/users/profile/username:webfacer意味着我的路由器沒有出現我的路線選擇。

有沒有人有同樣的問題,並解決了這個問題?

回答

3

因爲你沒有在你的路由字符串中放入:id參數,所以當你在助手中傳遞它時,Cake不知道該做什麼,這就是爲什麼它只是將它作爲普通參數附加到URL中。沒有辦法通過URL傳遞一個「隱藏」的ID,最好的辦法是將其公開或在應用程序的另一端寫入一些基於您傳遞的用戶名獲取ID的內容(確保此列已編入索引且網址安全)。

我只想簡化你的路線,這一點:

//in AppController 
Router::connect('/users/profile/:name', 
    array(
     'controller' => 'users', 
     'action'  => 'profile' 
    ) , 
    array('pass' => array('name'), 
    ) 
); 

也懶得傳遞ID的幫手。在你的個人資料動作中,你只是有這樣的事情:

public function profile($name) { 
    $user = $this->User->find('first', array('conditions' => array('name' => $name))); 
} 
+0

好吧......我想我可以處理它自我。這是我第一次找到最後的解決方案,但我認爲獲取(int)作爲唯一用戶varchar更快或不是? – webfacer

+0

我不擔心在項目開發的早期優化速度。如果你真的擔心這種差異,最好在數據庫的'name'列中添加一個INDEX。 – brism

+0

你的權利非常感謝你! – webfacer