Cakephp Auth范围错误消息

时间:2014-01-11 19:01:59

标签: php cakephp authentication

我正在使用Cake的Auth组件,在使用scope时似乎无法弄清楚如何设置特定的Flash数据/错误消息。

通过将active0更改为1进行测试,我可以确认scope参数有效,但是如果scope返回false,我会得到闪存与我的login方法Your username or password was incorrect.相关联的数据。

UsersController

public function login(){
    if($this->request->is('post')){
        if($this->Auth->login()){
            $this->Session->setFlash('You are logged in!');
            return $this->redirect($this->Auth->redirect());
        }

        $this->Session->setFlash(__('Your username or password was incorrect.'));
    }
}

AppController的

public $components = array(
    'DebugKit.Toolbar',
    'Acl',
    'Auth' => array(
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        ),
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email'),
                'scope' => array('active' => '1')
            )
        )
    ),
    'Session'
);

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

是否可以为每个scope参数和login方法绑定特定的错误消息?

1 个答案:

答案 0 :(得分:1)

在CakePHP中非常简单:) 您可以在Controller的每个方法中自定义错误消息 很简单,就像我说的那样^ _ ^

Exmple:
<?php
  public function accessSite(){
   //disable default message authError of Auth
   $this->Auth->authError = false; 
   $message = 'You not have permission access here';
   //set new custom message
   $this->Auth->flash($message);
  }