我正在尝试创建一个链接来编辑用户信息。 id 为 1 的人的 url 将是 users/edit/1
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
编辑 - 让它工作,感谢您的帮助
最佳答案
您可能不想按照您所描述的方式进行操作。如果我是你,我会创建一条单独的路由到 /profile/edit
之类的东西,然后让它转到 user
Controller 中的特定操作。在该操作中,您将从 session 中获取登录用户的 userid,查找用户,然后向他们显示其信息的编辑 View 。
这将使您的链接生成看起来像:
echo $this->Html->link('Edit Info', array(
'controller' => 'users',
'action' => 'edit_profile'
));
关于php - 用于编辑信息的 cakephp 身份验证组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10770667/