0

I am trying to allow users to edit their account details but had the following error. ( I am new to Cake...)

The error is "Fatal error: Call to a member function user() on a non-object in /home/www/7b8dad242e3d067ccc5448180944bdab/web/shop/Controller/UsersController.php on line 79"

VIEW: (edit.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'));?>

*CONTROLLER (UsersController.php) * line 79 is second line of code

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

}

/**

Any suggestions will be more than welcome. Many thanks

4

1 回答 1

0

基本上你在登录时做错了,你应该 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;

}

于 2013-11-06T06:12:12.653 回答