我正在使用 cakephp 2.6.7。
内部应用控制器:
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'email', //Default is 'username' in the userModel
'password' => 'password' //Default is 'password' in the userModel
),
'passwordHasher' => array(
'className' => 'Simple',
'hashType' => 'sha256'
)
)
)
)
);
ResellersController 内部:
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'Reseller',
)
),
'loginAction' => array(
'controller' => 'resellers',
'action' => 'login'
),
'loginRedirect' => array('controller' => 'resellers', 'action' => 'profile'),
'logoutRedirect' => array('controller' => 'resellers', 'action' => 'login'),
'authError' => "You can't acces that page",
'authorize' => 'Controller'
)
);
public function isAuthorized($user = null) {
return true;
}
function login() {
$this->layout = 'public-login';
$this->loadModel('Reseller');
// if already logged in check this step
if ($this->Auth->loggedIn()) {
return $this->redirect('profile'); //(array('action' => 'deshboard'));
}
// after submit login form check this step
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
} else {
$msg = '<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Incorrect email/password combination. Try Again</strong>
</div>';
$this->set(compact('msg'));
}
}
}
内部经销商/login.ctp:
<?php
echo $this->Form->create('Reseller', array(
'inputDefaults' => array(
'label' => false,
'div' => false
),
'class' => 'login-form',
'url' => array('controller' => 'resellers', 'action' => 'login')
)
);
?>
<?php if(isset($msg)){
echo $msg;
}
?>
<h3 class="form-title">Login to your account</h3>
<div class="alert alert-danger display-hide">
<button class="close" data-close="alert"></button>
<span>
Enter Email and password. </span>
</div>
<div class="form-group">
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<label class="control-label visible-ie8 visible-ie9">Email</label>
<div class="input-icon">
<i class="fa fa-user"></i>
<?php
echo $this->Form->input(
'email', array(
'class' => 'form-control placeholder-no-fix',
'type' => 'text',
'autocomplete' => 'off',
'placeholder' => 'Email'
)
);
?>
</div>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Password</label>
<div class="input-icon">
<i class="fa fa-lock"></i>
<?php
echo $this->Form->input(
'password', array(
'class' => 'form-control placeholder-no-fix',
'type' => 'password',
'autocomplete' => 'off',
'placeholder' => 'Password'
)
);
?>
</div>
</div>
<div class="form-actions">
<?php
echo $this->Form->button(
'Login <i class="m-icon-swapright m-icon-white"></i>', array(
'class' => 'btn blue pull-right',
'type' => 'submit',
'escape' => false
)
);
?>
</div>
<div class="forget-password">
<h4>Forgot your password ?</h4>
<p>
no worries, click <a href="javascript:;" id="forget-password">
here </a>
to reset your password.
</p>
</div>
<div class="create-account">
<p>
Don't have an account yet ? <a style=" text-transform: none; font-size: 12px !important;" class="btn btn-circle blue" href="javascript:;" id="register-btn">
Create an account </a>
</p>
</div>
<?php echo $this->Form->end(); ?>
<!-- END LOGIN FORM -->
内部模型/Reseller.php:
<?php
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
class Reseller extends AppModel {
var $name = "reseller";
// public $belongsTo = array(
// 'Order' => array(
// 'className' => 'Order',
// 'foreignKey' => 'api_key'
// )
// );
public $validate = array(
'email' => array(
'rule' => 'isUnique',
'required' => true,
'message' => 'Email already exist'
),
'password' => array(
'rule' => array('minLength', '4'),
'message' => 'password must be minimum 4 characters long'
)
);
function hashPassword() {
if (!empty($this->data[$this->alias]['password'])) {
$passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
}
function beforeSave($options = array()) {
$this->hashPassword();
return true;
}
}
?>
我已经探索了我的代码两天了。但是不要找出为什么它总是返回错误。有很多和我一样的问题,我每个都研究但没有运气。任何帮助将不胜感激。谢谢