我是 cakephp 的新手,我想使用不同的 MySQL 表名“registrations”登录。根据 cakephp 书,需要一个名为 table 的用户来登录和注册,以及它的用户控制器和模型。但我的问题是。有没有办法用注册表登录?在这里,我为您定义了所有页面。
Controller AppController.php OnlineController.php(这个页面是我网站的主控制器。)
模型 AppModel.php Registrations.php
查看 register.ctp login.ctp
这里是我的登录视图页面
<?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>
和控制器 >> OnlineController.php >> 代码在这里
类 OnlineController 扩展 AppController {
/**
* Controller name
*
* @var string
*/
public $name = 'Online';
//component
public $components=array('Session','Paginator','Auth');
public $uses = array('Registrations','Contacts','User','RegistrationsModel');
function beforeFilter(){
parent::beforeFilter();
}
public function index(){
//$students=$this->Student->find('all');
}
public function login(){
if($this->request->is('post'))
{
// pr($_POST);
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
// Prior to 2.3 use `return $this->redirect($this->Auth->redirect());`
}
else {
$this->Session->setFlash(__('Username or password is incorrect'));
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
public function profile()
{
}
模型部分>> 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');
}
}
请给我一个正确的解决方案..在此先感谢