此功能有问题。目前,我已经建立了我的网站,因此只有用户登录后才能访问“做什么和要约”页面。如果他们单击这些页面,它们将被重定向到登录页面。
不幸的是,当用户登录时,登录页面只是刷新,而不是登录并重定向到索引页面。
我的用户表具有以下字段:user_id,名字,姓氏,电子邮件,密码,已创建,已修改。
这是我在UsersController中用于登录功能的代码:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
这是我的AppController代码:
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $helpers = array(
'Session',
'Html' => array('className' => 'BoostCake.BoostCakeHtml'),
'Form' => array('className' => 'BoostCake.BoostCakeForm'),
'Paginator' => array('className' => 'BoostCake.BoostCakePaginator'),
);
// CakePHP documentation tutorial
public $components = array(
'Session',
'DebugKit.Toolbar',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'pages',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'flash' => array(
'element' => 'alert',
'key' => 'auth',
'params' => array(
'plugin' => 'BoostCake',
'class' => 'alert-error'
)
)
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'contact');
}
}
这是我的login.ctp代码:
<!-- Sign in Form which is on the left hand side of the screen -->
<div class="col-xs-6">
<?php echo $this->Form->create('User', array(
'inputDefaults' => array(
'action' => 'login',
'div' => 'form-group',
'label' => array(
'class' => 'col col-md-3 control-label'
),
'wrapInput' => 'col col-md-9',
'class' => 'form-control'
),
'class' => 'well form-horizontal'
)); ?>
<?php echo $this->Form->input('email', array(
'placeholder' => 'Email'
)); ?>
<?php echo $this->Form->input('password', array(
'placeholder' => 'Password'
)); ?>
<!-- Sign in button for the sign in form -->
<div class="form-group">
<?php echo $this->Form->submit('Sign in', array(
'div' => 'col col-md-9 col-md-offset-3',
'class' => 'btn btn-default'
));
echo $this->Form->end(); ?>
</div>
<div class="row">
<h5 small><?php echo $this->Html->link("Not registed? Click here to register!", '/users/register') ?></h5>
</div>
希望有人可以指出出了什么问题
您使用的是哪个版本?尝试在此部分使用redirectUrl而不是redirect(),因为您应该在2.3版之后使用它。
return $this->redirect($this->Auth->redirect());
如果没有帮助,请尝试添加一些回显,以查看$ this-> Auth-> login()是否为true,以及$ this-> Auth-> redirect()的值是什么。如果一切看起来正常,请尝试查看视图是否实际输出了闪速消息(以防auth-> login为false)。
代码看起来不错,所以您实际上必须调试一些步骤才能真正找到解决方案。
这将为您提供帮助。请在此处阅读更多信息Configuring Authentication handlers
这是因为您忘记了在“组件”设置上添加的内容-您的公共$ components必须看起来像这样-
您没有将数据发布到控制器和操作。尝试创建: