CakePHP 和 DefaultPasswordHasher 语法错误

CakePHP and DefaultPasswordHasher syntax error

除了这行

我不知道为什么

use Cake\Auth\DefaultPasswordHasher;

我收到一个错误

Error: syntax error, unexpected '?'

和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

请试试这个。请使用 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。希望能帮到您解决这个问题。

使用Cake\ORM\Entity;

使用Cake\Auth\DefaultPasswordHasher; /** * 用户实体 * * @属性 int $id * @属性 字符串 $email * @属性 字符串 $密码 * @属性 字符串 $role * @属性 \Cake\I18n\Time $创建 * @属性 \Cake\I18n\Time $修改 */ 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);
}

}