【问题标题】:Cakephp Auth scope error messageCakephp Auth 范围错误消息
【发布时间】:2014-01-11 19:01:59
【问题描述】:

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

通过将active0 更改为1 进行测试时,我可以确认scope 参数有效,但是如果scope 返回false,我将获得与我的login 方法相关联的闪存数据,@ 987654328@.

用户控制器

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.'));
    }
}

应用控制器

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 方法绑定特定的错误消息?

【问题讨论】:

    标签: php cakephp authentication


    【解决方案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);
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 2013-08-29
      • 2013-03-08
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      相关资源
      最近更新 更多