1

我正在尝试创建一个帐户注册页面CakePHP 2.0,用户需要通过单击插入后收到的电子邮件中的链接来激活其新帐户usernameemail并且password.

我的问题是如何在用户记录中设置激活码。

我想创建一个名为的表字段activation_code,然后存储一个hashed版本,username以确保用户可以通过单击带有激活密钥的电子邮件链接来激活自己。

所有过程都完成了,但我不知道如何设置对象activation_code内部,$data['User']而且我不清楚这是 MVC 框架的一个很好的用法,还是我应该以不同的方式制作它。

在用户注册操作期间,我已完成此操作,但当我尝试动态创建“activation_code”时出现错误:

// from the UserController class
public function register () {
    if (!empty($this->data)) {
        if ($this->data['User']['password'] == $this->data['User']['confirm_password']) {
            // here is where I get the error
            $this->data['User']['activation_key'] = AuthComponent::password($this->data['User']['email']);
            $this->User->create();
            if ($this->User->save($this->data)) {
                // private method
                $this->registrationEmail ($this->data['User']['email'], $this->data['User']['username']);
                $this->redirect(array('controller'=>'users', 'action'=>'registration', 'success'));
            }
        }
    }
}

显然,这activation_key是我的数据库中的一个空字段。

那么如何从控制器动态创建一个文件呢?

4

2 回答 2

0
$this->data['User']['activation_key']

应该:

$this->request->data['User']['activation_key']

(您应该将所有对 $this->data 的引用更改为新的 cakephp2.0 $this->request->data)

于 2011-10-31T11:43:29.330 回答
0

我已经用方法解决了这个问题Model::set(),所以:

public function register () {
    if (!empty($this->data)) {
        if ($this->data['User']['password'] == $this->data['User']['confirm_password']) {
            $this->User->create();
            // I've used set method
            $this->User->set('activation_key', AuthComponent::password($this->data['User']['email']));
            if ($this->User->save($this->data)) {
                $this->registrationEmail ($this->data['User']['email'], $this->data['User']['username']);
                $this->redirect(array('controller'=>'users', 'action'=>'registration', 'success'));
            }
        }
    }
}
于 2011-11-05T11:15:03.337 回答