如何使用cakephp 2.3中的registrations表登录
我是cakephp的新手,我想用不同的MySQL表名“registrations”登录。根据cakephp的书,需要一个名为table的用户来登录和注册,以及它的用户控制器和模型。但我的问题是。有没有办法登录注册表?在这里,我给你定义了它的所有页面 控制器 AppController.php php(此页面是我的站点的主控制器。) 型号 AppModel.php Registrations.php 查看 register.ctp login.ctp 这里是我的登录查看页面如何使用cakephp 2.3中的registrations表登录,cakephp,cakephp-2.1,Cakephp,Cakephp 2.1,我是cakephp的新手,我想用不同的MySQL表名“registrations”登录。根据cakephp的书,需要一个名为table的用户来登录和注册,以及它的用户控制器和模型。但我的问题是。有没有办法登录注册表?在这里,我给你定义了它的所有页面 控制器 AppController.php php(此页面是我的站点的主控制器。) 型号 AppModel.php Registrations.php 查看 register.ctp login.ctp 这里是我的登录查看页面 <?php ec
<?php echo $this->Form->create('Registrations'); ?>
<table width="450" border="0" align="center" id="login_form">
<tr align="center">
<td colspan="2" align="left"><h3 style="padding-top:10px; padding-left:0px;">Login In</h3>
<hr /></td>
</tr>
<tr><td><?php echo $this->Session->flash(); ?></td></tr>
<tr>
<td width="245"><?php echo $this->Form->input('email'); ?></td>
<tr>
<td><?php echo $this->Form->input('password', array('type'=>'password')); ?></td>
</tr>
<tr>
<td align="right"><a href="#">Lost Username/Password</a></td>
</tr>
<tr>
<td align="right"><? echo $this->Form->end('Submit'); ?></td>
</tr>
<tr>
<td align="right"> </td>
<td> </td>
</tr>
</table>
模型部分>>Registrations.php,代码为
class Registrations extends AppModel {
public $name='Registrations';
function beforeSave() {
if(isset($this->data['Registrations']['password']))
$this->data['Registrations']['password'] = Security::hash($this->data['Registrations']['password'], null, true);
return true;
}
public $validate = array(
'first_name'=>array(
'rule'=>'alphaNumeric',
'required'=> true,
'allowEmpty'=>false,
'message'=>'Please Enter Your Name'
),
'last_name'=>array(
'rule'=>'alphaNumeric',
'required'=>true,
'allowEmpty'=>false,
'message'=>"Please Enter Your Last Name."
),
'email'=>array(
'rule'=>'email',
'required'=>true,
'allowEmpty'=>false,
'message'=>"Please Enter Your Email address."
)
,
'password'=>array(
'rule'=>'alphaNumeric',
'required'=>true,
'allowEmpty'=>false,
'message'=>"Please Enter Your Password."
),
'phone'=>array(
'rule'=>'Numeric',
'required'=>true,
'allowEmpty'=>false,
'message'=>"Please Enter Your Phone No.."
)
,
'state'=>array(
'rule'=>'alphaNumeric',
'required'=>true,
'allowEmpty'=>false,
'message'=>"Please Enter Your Sate"
)
,
'city'=>array(
'rule'=>'alphaNumeric',
'required'=>true,
'allowEmpty'=>false,
'message'=>"Please Enter Your City"
)
);
}
最后,我对Appcontroller使用了一些逻辑
class AppController extends Controller {
public $components = array(
'Auth' => array(
'loginAction' => array(
'controller' => 'Online',
'action' => 'login'),
'loginRedirect' => array(
'controller' => 'Online',
'action' => 'profile'),
'logoutRedirect ' => array(
'controller' => 'Online',
'action' => 'login'),
'authenticate' => array(
'Registrations' => array(
'userModel' => 'RegistrationsModel',
'fields' => array(
'username' => 'email',
'password' => 'password'
)
)
)
)
);
function beforeFilter() {
$this->Auth->allow('register','index','contact','quiz_zone','about','packages','online_test','test_gen','login');
}
}
请给我一个正确的解决方案..提前谢谢登录表的名称与此无关。CakePHP并没有为它命名。 这是一个背景。例如,在AppController中配置
AuthComponent
时,请执行以下操作:
$this->Auth->authenticate = array('Form' => array('userModel' => 'Registration'));
在您的示例中,您传递了错误的名称:RegistrationsModel
因此,在您的示例中,这是非常错误的:
'authenticate' => array(
'Registrations' => array(
'userModel' => 'RegistrationsModel',
'fields' => array(
'username' => 'email',
'password' => 'password'
)
)
第一级键应该是(表单、基本、摘要),而不是注册
。然后,正如我所解释的,你可以做:
'authenticate' => array(
'Form' => array(
'userModel' => 'Registration',
'fields' => array(
'username' => 'email',
'password' => 'password'
)
)
这一切都在这本书中得到了很好的解释
你没有做的另一件非常糟糕的事情是!
Cake遵循“约定优先于配置”的理念。
例如,您的模型应该是单数而不是复数。。。读这本书。从turotioals开始
'authenticate' => array(
'Form' => array(
'userModel' => 'Registration',
'fields' => array(
'username' => 'email',
'password' => 'password'
)
)