0

这是AppController代码:

public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'admins', 'action' => 'index'),
        'authorize' => array('Controller')
    )
);

public function beforeFilter()
{
    $this->Auth->loginAction = array('controller' => 'admins', 'action' => 'login');
}

public function beforeSave()
{
    if(isset($this->data['Admin']['password']))
    {
        $this->data['Admin']['password'] = AuthComponent::password($this->data['Admin']['password']);
    }
    return true;
}

这是AdminsController代码:

public $name = 'Admins';

public function beforeFilter()
{
    parent::beforeFilter();

    $this->Auth->fields = array('username' => 'email', 'password' => 'password');
    $this->Auth->allow('signup');
}

public function index()
{
    $this->Admin->recursive = 0;
    $this->set('admins', $this->Admin->find('all'));
}

public function login()
{
    $this->set('title_for_layout', 'Polkadot - Admin Login');

    if($this->request->is('post'))
    {
        if($this->Auth->login())
        {
            $this->Session->setFlash('Login successfull!');
            $this->redirect($this->Auth->redirect());
        }
        else
        {
            $this->Session->setFlash('Login failed!');
        }
    }
}

public function signup()
{
    $this->set('title_for_layout', 'Polkadot - Admin Sign Up');

    if($this->request->is('post'))
    {
        if($this->Admin->save($this->request->data))
        {
            $this->Session->setFlash('Account created successfully!');
            $this->redirect(array('action' => 'index'));
        }
        else
        {
            $this->Session->setFlash('Account could not be created, please try again!');
        }
    }
}

我可以看到该帐户已创建,但数据库中的密码是纯文本。当我尝试登录时,它失败了。我做错了什么?

我希望管理员登录时打开控制器的index操作Dashboard。帐户注册使用四个输入字段:名称(文本),电子邮件(文本),密码(密码)和密码确认(密码)。因此,登录表单使用两个字段:电子邮件(文本)和密码(密码)。

使用的 CakePHP 版本是 2.3.1

所有帮助表示赞赏!

[编辑]:这是错误日志

2013-04-10 19:31:13 Error: [MissingControllerException] Controller class CssController could not be found.
Exception Attributes: array (
  'class' => 'CssController',
  'plugin' => NULL,
)
Request URL: /polkapanel/css/cake.generic.css
Stack Trace:
#0 C:\wamp\www\polkapanel\app\webroot\index.php(109): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#1 {main}
2013-04-10 19:31:13 Error: [MissingControllerException] Controller class ImgController could not be found.
Exception Attributes: array (
  'class' => 'ImgController',
  'plugin' => NULL,
)
Request URL: /polkapanel/img/cake.power.gif
Stack Trace:
#0 C:\wamp\www\polkapanel\app\webroot\index.php(109): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#1 {main}
2013-04-10 19:31:25 Error: [MissingControllerException] Controller class CssController could not be found.
Exception Attributes: array (
  'class' => 'CssController',
  'plugin' => NULL,
)
Request URL: /polkapanel/css/cake.generic.css
Stack Trace:
#0 C:\wamp\www\polkapanel\app\webroot\index.php(109): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#1 {main}
2013-04-10 19:31:25 Error: [MissingControllerException] Controller class ImgController could not be found.
Exception Attributes: array (
  'class' => 'ImgController',
  'plugin' => NULL,
)
Request URL: /polkapanel/img/cake.power.gif
Stack Trace:
#0 C:\wamp\www\polkapanel\app\webroot\index.php(109): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#1 {main}
4

1 回答 1

2

您必须将beforeSave函数放入Admin模型中:

Admin.php(或任何你称之为的):

public function beforeSave() {
    if(isset($this->data['Admin']['password'])) {
        $this->data['Admin']['password'] = AuthComponent::password($this->data['Admin']['password']);
    }
    return true;
}
于 2013-04-10T19:03:14.093 回答