在CakePHP

时间:2016-08-27 21:43:25

标签: php cakephp registration

我正在尝试在cakePHP项目中注册用户。我完成了一个完美的登录和注册教程。我已经尝试在我的项目中重用代码,但我没有运气。登录部分工作正常,但每次注册失败。任何帮助都会很棒。

我在AdminController.php中的注册函数

public function register(){
    $user = $this->Admin->newEntity();
    if($this->request->is('post')){
        $user = $this->Admin->patchEntity($user, $this->request->data);
        if($this->Admin->save($user)){
            $this->Flash->success('You are registered');
            return $this->redirect(['action' => 'login']);
        } else {
            $this->Flash->error('You have not registered');
        }
    }
    $this->set(compact('user'));
    $this->set('_serialize', ['user']);
}

我的Admin / register.ctp

<br>
<div class = "index large-4 medium-4 large-offset-4 medium-offset-4">
    <div class = "panel">
        <h2 class = "text-centre">Please Register</h2>
        <?= $this->form->create($user); ?>
            <?= $this ->form->input('first name'); ?>
            <?= $this ->form->input('last name'); ?>
            <?= $this ->form->input('date of birth', array('type' => 'date')); ?>
            <?= $this ->form->input('email'); ?>
            <?= $this ->form->input('password', array('type' => 'password')); ?>
            <?= $this ->form->submit('register', array('class' => 'button')); ?>
        <?= $this->form->end(); ?>
    </div>
</div>

我的桌子我想在

中注册我的用户
CREATE TABLE `admin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fName` varchar(255) NOT NULL,
`lName` varchar(255) NOT NULL,
`dOb` date NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

这是AdminTable.php

<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
* Admin Model
*
* @method \App\Model\Entity\Admin get($primaryKey, $options = [])
* @method \App\Model\Entity\Admin newEntity($data = null, array     $options = [])
* @method \App\Model\Entity\Admin[] newEntities(array $data, array   $options = [])
* @method \App\Model\Entity\Admin|bool     save(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\Admin   patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array   $options = [])
* @method \App\Model\Entity\Admin[] patchEntities($entities, array   $data, array $options = [])
* @method \App\Model\Entity\Admin findOrCreate($search, callable   $callback = null)
*
* @mixin \Cake\ORM\Behavior\TimestampBehavior
*/
class AdminTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('admin');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');
}

/**
 * Default validation rules.
 *
 * @param \Cake\Validation\Validator $validator Validator instance.
 * @return \Cake\Validation\Validator
 */
public function validationDefault(Validator $validator)
{
    $validator
        ->integer('id')
        ->allowEmpty('id', 'create')
        ->add('id', 'unique', ['rule' => 'validateUnique', 'provider'   => 'table']);

    $validator
        ->requirePresence('fName', 'create')
        ->notEmpty('fName');

    $validator
        ->requirePresence('lName', 'create')
        ->notEmpty('lName');

    $validator
        ->date('dOb')
        ->requirePresence('dOb', 'create')
        ->notEmpty('dOb');

    $validator
        ->email('email')
        ->requirePresence('email', 'create')
        ->notEmpty('email');

    $validator
        ->requirePresence('password', 'create')
        ->notEmpty('password');

    return $validator;
}

/**
 * Returns a rules checker object that will be used for validating
 * application integrity.
 *
 * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
 * @return \Cake\ORM\RulesChecker
 */
public function buildRules(RulesChecker $rules)
{
    $rules->add($rules->isUnique(['email']));
    $rules->add($rules->isUnique(['id']));

    return $rules;
}
}

提前感谢您花时间仔细研究。

感谢您的帮助。将代码添加到调试后我得到错误:

[
'fName' => [
    '_required' => 'This field is required'
],
'lName' => [
    '_required' => 'This field is required'
]
]

1 个答案:

答案 0 :(得分:-1)

解决

从阅读cakePHP烹饪书中我发现我不符合cakePHP需要正常工作的严格命名约定。由于我的Admin表列名称格式不正确,因此无法成功传递数据。我将列名称从fName格式更改为first_name。然后,我将表格名称从Admin更改为Admins。最后我发出了命令bin/cake bake all admins。我现在可以成功注册并登录。谢谢你们所有人的帮助。