2012-05-26 16 views
0

我試圖編輯鏈接以編輯用戶的信息。 ID爲1的用戶的網址爲用戶/編輯/ 1用於編輯信息的cakephp驗證組件

echo $this->Html->link('Edit Info', array( 
    'controller' => 'users', 
    'action' => 'edit', 
    AuthComponent::user('id') 
)); 

該URL顯示正確。但是,我試圖限制它,只有具有該ID的用戶才能編輯他們的頁面。因此,假設用戶4嘗試編輯用戶1的信息,它將重定向。

這是應該重定向的UsersController中的編輯操作的一部分。

if($id !== AuthComponent::user('id')){ 
     $this->redirect(array('controller'=>'posts','action'=> 'index')); 
    } 

我收到以下錯誤

Parse error: syntax error, unexpected '=', expecting ')' in /Applications/XAMPP/xamppfiles/htdocs/cake/app/Controller/UsersController.php on line 42 

編輯 - 得到它的工作,感謝您的幫助

回答

1

你可能不想做你所描述的方式。如果我是你,我會製作一個單獨的路線,例如/profile/edit然後讓它轉到user控制器中的特定操作。在該操作中,您將從會話中獲取登錄的用戶userid,查找用戶,然後向他們顯示編輯視圖以獲取其信息。

這將使你鏈接生成看起來像:

echo $this->Html->link('Edit Info', array( 
    'controller' => 'users', 
    'action' => 'edit_profile' 
)); 
+0

我無法弄清楚如何與路線做,但我得到了它使用上述方法來工作。 – user1406951