CakePHP和DefaultPasswordHasher语法错误

时间:2016-11-30 14:54:49

标签: cakephp-3.0 password-encryption

我不知道为什么,但为了线

use Cake\Auth\DefaultPasswordHasher;

我收到错误

Error: syntax error, unexpected '?'

enter image description here

和user.php的完整代码

namespace App\Model\Entity;

use Cake\ORM\Entity;
use Cake\Auth\DefaultPasswordHasher;

/**
 * User Entity
 *
 * @property int $id
 * @property string $email
 * @property string $password
 * @property \Cake\I18n\Time $created
 * @property \Cake\I18n\Time $modified
 *
 * @property \App\Model\Entity\Bookmark[] $bookmarks
 */
class User extends Entity
{

    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * Note that when '*' is set to true, this allows all unspecified fields to
     * be mass assigned. For security purposes, it is advised to set '*' to false
     * (or remove it), and explicitly make individual fields accessible as needed.
     *
     * @var array
     */
    protected $_accessible = [
        '*' => true,
        'id' => false
    ];

    /**
     * Fields that are excluded from JSON versions of the entity.
     *
     * @var array
     */
    protected $_hidden = [
        'password'
    ];

    protected function _setPassword($value){

    $hasher = new DefaultPasswordHasher();
        return $hasher->hash($value);
    }
}

有什么想法?我使用最新的CakePHP

2 个答案:

答案 0 :(得分:1)

请试试这个。请使用composer更新CakePHP项目,如果遗漏某些内容,它将更新所有依赖项。

namespace App\Model\Entity;

use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;

class User extends Entity
{

    // Make all fields mass assignable except for primary key field "id".
    protected $_accessible = [
        '*' => true,
        'id' => false
    ];

    // ...

    protected function _setPassword($password)
    {
        return (new DefaultPasswordHasher)->hash($password);
    }

    // ...
}

另请阅读此文档here。希望它能帮助你解决这个问题。

答案 1 :(得分:0)

使用Cake \ ORM \ Entity;

使用Cake \ Auth \ DefaultPasswordHasher; / **  *用户实体  *  * @property int $ id  * @property string $ email  * @property string $ password  * @property string $ role  * @property \ Cake \ I18n \ Time $ created  * @property \ Cake \ I18n \ Time $ modified  * / class用户扩展实体 {

/**
 * Fields that can be mass assigned using newEntity() or patchEntity().
 *
 * Note that when '*' is set to true, this allows all unspecified fields to
 * be mass assigned. For security purposes, it is advised to set '*' to false
 * (or remove it), and explicitly make individual fields accessible as needed.
 *
 * @var array
 */
protected $_accessible = [
    '*' => true,
    'id' => false
];

/**
 * Fields that are excluded from JSON versions of the entity.
 *
 * @var array
 */
protected $_hidden = [
    'password'
];

protected function _setPassword($password) {
    return (new DefaultPasswordHasher)->hash($password);
}

}