0

我是 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">&nbsp;</td>
    <td>&nbsp;</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');



      } 
}

请给我一个正确的解决方案..在此先感谢

4

1 回答 1

0

登录表的名称无关紧要。CakePHP 没有为此命名。这是一个设定。例如,在配置您AuthComponent的 AppController 时,请执行以下操作:

$this->Auth->authenticate = array('Form' => array('userModel' => 'Registration'));

在您的示例中,您传递了错误的名称:RegistrationsModel.

所以在你的例子中这是非常错误的:

 'authenticate' => array(
         'Registrations' => array(
                    'userModel' => 'RegistrationsModel',
                    'fields' => array(
                        'username' => 'email',
                        'password' => 'password'
                    )
        )

第一级密钥应该是身份验证类型(Form、Basic、Digest)而不是Registrations。然后正如我解释的那样,你可以这样做:

 'authenticate' => array(
         'Form' => array(
                    'userModel' => 'Registration',
                    'fields' => array(
                        'username' => 'email',
                        'password' => 'password'
                    )
        )

这一切都在书中得到了很好的解释——这里这里

您没有做的另一件非常糟糕的事情是遵循 CakePHP 约定!Cake 遵循“约定优于配置”的理念。例如,您的模型应该是单数而不是复数......阅读这本书。从教程开始。

于 2013-06-05T10:06:06.953 回答