我正在尝试允许用户编辑其帐户详细信息,但出现以下错误。(我是蛋糕的新手...
错误是"致命错误:在第 79 行的/home/www/7b8dad242e3d067ccc5448180944b/web/shop/Controller/UsersController.php 中的非对象上调用成员函数 user() "
查看: (编辑.ctp)
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php echo __('Edit Details'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('name');
echo $this->Form->input('surname');
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->input('street');
echo $this->Form->input('number');
echo $this->Form->input('zipcode');
echo $this->Form->input('city');
echo $this->Form->input('country');
echo $this->Form->input('tel');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
*控制器(UsersController.php) * 第 79 行是第二行代码
public function edit($id = null) {
if($this->Auth->user() && $this->Auth->user('id') == $id) {
$this->User->read(null, $id);
$this->User->set(array(
'name' => $this->data['User']['name'],
'surname' => $this->data['User']['surname'],
'email' => $this->data['User']['email'],
'street' => $this->data['User']['street'],
'number' => $this->data['User']['number'],
'city' => $this->data['User']['city'],
'zipcode' => $this->data['User']['zipcode'],
'country' => $this->data['User']['country'],
'tel' => $this->data['User']['tel'],
));
$this->User->save();
}
}
* auth method
*
* @return void
*/
public function auth() {
$auth = $this->User->find('first', array(
'conditions' => array('User.email' => $this->request->data['email'], 'User.password' => $this->request->data['password'])));
if($auth == null) {
$this->Session->setFlash('Incorrect user or password', 'default', array(), 'auth');
$this->redirect(array('action' => 'add'));
} else {
$this->Session->write('logged_user', $this->request->data['email']);
//debug($_SESSION['logged_user']);
$this->redirect(array('controller' => 'products', 'action' => 'index'));
}
}
/**
任何建议都将非常受欢迎。非常感谢
基本上你在登录时做错了,你应该AuthComponent::login()进行登录而不是你的自定义登录。
执行以下设置操作1)在类中顶级添加组件
class AppController extends Controller {
public $components = array('Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
),
'autoRedirect' => false,
// 'loginError' => 'Username or password is incorrect',
), 'Session', 'RequestHandler');
2)您的登录操作应如下所示
public function login()
{
if($this->request->is('post')){
if ($this->Auth->login()){
// successful login
}else{
// login failed
}
}
}
3)然后,您就可以在Auth::user()方法中获取登录用户信息,该方法在您的代码中是正确的4)但是,如果不在用户模型中执行以下行,则必须将用户密码保存在哈希中
公共函数 beforeSave($options = array()) {
if (isset($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}