在Cakephp 3中无法隐藏密码

时间:2017-11-19 11:49:15

标签: php cakephp cakephp-3.0

我已按照本教程https://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html创建了一个注册表单,但密码保存在数据库中而没有加密为纯文本。请帮助解决我的问题。 这是我的代码:

  

Add.ctp

<h1>Register new user </h1>


<div class="users form">
<?= $this->Form->create($user) ?>
    <fieldset>
        <legend><?= __('Add User') ?></legend>
        <?= $this->Form->control('username') ?>
        <?= $this->Form->control('password') ?>

   </fieldset>
<?= $this->Form->button(__('Submit')); ?>
<?= $this->Form->end() ?>
</div>
  

实体/ user.php的

<?php
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".
 // Make all fields mass assignable for now.
protected $_accessible = ['*' => true];

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

    // ...
}

?>
  

UsersTable.php

<?php
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\Auth\DefaultPasswordHasher;

class UsersTable extends Table
{
   public function validationDefault(Validator $validator)
    {
        return $validator
        ->notEmpty('username', 'A username is required')
        ->notEmpty('password', 'A password is required');

    }


}

?>
  

UsersController.php

<?php

namespace PanelAdmin\Controller;

use Cake\Controller\Controller;
use Cake\ORM\TableRegistry;
use Cake\Event\Event;

class UsersController extends AppController

{

 public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        $this->Auth->allow('add');
    }

     public function index()
     {
        $this->set('users', $this->Users->find('all'));
    }

    public function view($id)
    {
        $user = $this->Users->get($id);
        $this->set(compact('user'));
    }

    public function add()
    {
        $user = $this->Users->newEntity();
        if ($this->request->is('post')) {
            // Prior to 3.4.0 $this->request->data() was used.
            $user = $this->Users->patchEntity($user, $this->request->getData());
            //debug($user); die;
            if ($this->Users->save($user)) {
                $this->Flash->success(__('The user has been saved.'));
                return $this->redirect(['action' => 'add']);
            }
            $this->Flash->error(__('Unable to add the user.'));
        }
        $this->set('user', $user);
    }

    }

?>

目录结构: enter image description here

1 个答案:

答案 0 :(得分:0)

您的命名空间错误,或者您将代码放在错误的位置。如果代码应该存在于PanelAdmin插件中,那么您不应该为模型/实体类使用App命名空间,而应使用PanelAdmin。{/ p >