1

我需要在 CakePHP 3.4 中使用自定义查找查询对用户进行身份验证。我为此遵循了烹饪书,但在我的findAuth方法中$option返回 empty array。我不知道代码中的错误是什么。

public function initialize()
{
    parent::initialize();
    $this->loadComponent('RequestHandler');
    $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'finder' => 'auth',
                'fields' => ['username' => 'username', 'password' => 'password']
            ]
        ],
    ]);
}

用户表.php

public function findAuth(\Cake\ORM\Query $query, array $options)
{
    debug($options);
    $query->select(['sno', 'username', 'password','contact'])
        ->where(['Users.username' => $options['username']]);
    return $query;
}

登录.ctp

<?php
   echo $this->Form->create();
   echo $this->Form->input('username');
   echo $this->Form->input('password');
   echo $this->Form->submit();
   echo $this->Form->end();
?>
4

1 回答 1

0
   public function findAuth(\Cake\ORM\Query $query, array $options)
    {
        debug($options);
        $query->select(['sno', 'username', 'password','contact'])
            ->where(['Users.username' => $options['username']]);
        return $query;
    }

应该 :

public function findAuth(\Cake\ORM\Query $query, array $options)
{
    debug($options);
    $query->select(['sno', 'username', 'password','contact']));
    return $query;
}

参考:https ://book.cakephp.org/3.0/en/controllers/components/authentication.html#creating-custom-authentication-objects

于 2018-10-19T10:07:15.997 回答