3

我的成员视图中有一个登录表单 (login.ctp)。但是,当我将 url 提供为 /login 时,而不是显示成员视图的登录,而是显示用户视图的登录页面。

/Views/login.ctp 文件

<div class="members form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Member'); ?>
<fieldset>
    <legend><?php echo __('Please enter your username and password'); ?></legend>
    <?php echo $this->Form->input('username');
    echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div> 

应用控制器文件

class AppController extends Controller {
//...

public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
        'logoutRedirect' => array('controller' => 'members', 'action' => 'index')
    )
);

public function beforeFilter() {
    $this->Auth->allow('index', 'view');
}
//...
}

我的 routes.php 文件

 Router::connect('/', array('controller' => 'members', 'action' => 'index'));
Router::connect('/login', array('controller' => 'members', 'action' => 'login'));

我已经清除了所有的 cookie。错误在哪里?

还有一件事,我什至尝试过删除用户控制器,但是如果我删除用户控制器,我会收到以下错误...

错误:找不到用户控制器。

这是我在 MembersController.php 中的登录功能

         public function login() {
    if ($this->request->is('post')) {

        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}

//成员模型中的代码。

class Member extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
public $validate = array(
    'firstname' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'lastname' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'email' => array(
        'email' => array(
            'rule' => array('email'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'password' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'age' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'address' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'phone' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'created_on' => array(
        'datetime' => array(
            'rule' => array('datetime'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
);

function beforeSave() {
  if(isset($this->data[$this->alias]['password']))
    $this->data[$this->alias]['password'] = Security::hash($this->data[$this->alias]['password'], null, true);
  return true;
}
}
4

2 回答 2

1

编辑

测试代码和流程

应用控制器代码

<?php
class AppController extends Controller
{
    public $components = array
    (
        'Session',
        'Auth' => array
        (
            'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
            'logoutRedirect' => array('controller' => 'members', 'action' => 'index')
        )
    );

    public function beforeFilter()
    {
        $this->Auth->allow('index', 'view');
        $this->Auth->fields = array('username' => 'email', 'password' => 'password');
        $this->Auth->userModel = 'Member';
    }
}

您的所有代码都运行良好,您只需将其设置Auth->userModelMember.

成员控制器

<?php
class MembersController extends AppController
{

    var $name = 'Members';

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

}
?>

并在成员控制器中定义beforeFilter as above.

于 2013-01-25T06:14:29.993 回答
1

尝试将您的组件数组修改为:

public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
        'logoutRedirect' => array('controller' => 'members', 'action' => 'index'),
        'Form' => array(
            'userModel' => 'Member'
        ),
    )
);

或者在 beforeFilter() 中试试这个:

// Pass settings in using 'all'
$this->Auth->authenticate = array(
    AuthComponent::ALL => array('userModel' => 'Member'),
    'Form',
    'Basic'
);
于 2013-01-25T06:21:52.780 回答