CakePHP认证->;login()函数不工作
这个函数有问题。目前,我已经设置了我的网站,因此只有在用户登录后才能访问What's On和Offers页面。如果他们点击这些页面,他们将被重定向到登录页面 不幸的是,当用户登录时,登录页面只是刷新,而不是登录并重定向到索引页面 “我的用户”表包含以下字段:用户id、名字、姓氏、电子邮件、密码、已创建、已修改 这是我在UsersController中的登录函数代码:CakePHP认证->;login()函数不工作,php,cakephp,login,authorization,Php,Cakephp,Login,Authorization,这个函数有问题。目前,我已经设置了我的网站,因此只有在用户登录后才能访问What's On和Offers页面。如果他们点击这些页面,他们将被重定向到登录页面 不幸的是,当用户登录时,登录页面只是刷新,而不是登录并重定向到索引页面 “我的用户”表包含以下字段:用户id、名字、姓氏、电子邮件、密码、已创建、已修改 这是我在UsersController中的登录函数代码: public function login() { if ($this->request->is('post
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()是否为真,以及$this->Auth->redirect()的值是多少。如果一切正常,尝试查看您的视图是否实际输出了flash消息(如果auth->login为false)
代码似乎很好,因此您确实需要调试一些步骤才能真正实现解决方案。这将对您有所帮助。请在此阅读更多内容
这是因为您忘记了在组件设置中添加一些内容-您的公共$Components必须如下所示-
public $components = array(
'Session',
'DebugKit.Toolbar',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'pages',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'email'
)
)
),
'flash' => array(
'element' => 'alert',
'key' => 'auth',
'params' => array(
'plugin' => 'BoostCake',
'class' => 'alert-error'
)
)
)
);
您没有将数据发布到控制器和操作。在“创建”上尝试以下操作:
<?php echo $this->Form->create('User',
array('url' => array('controller' => 'users', 'action' => 'login'),
'inputDefaults' => array(
'div' => 'form-group',
'label' => array(
'class' => 'col col-md-3 control-label'
),
'wrapInput' => 'col col-md-9',
'class' => 'form-control'
),
'class' => 'well form-horizontal'
)); ?>
您可能会发现此用途已满
使用CakePHP 2.4.6,我们现在就开始尝试!这也不起作用,如何在CakePHP中调试?只需输入echo$This->Auth->redirect()或print\r($This->Auth->redirect())之类的内容;然后是出口;甚至像echo“我现在在这里”这样的东西;将让您了解脚本的位置。“用户名”和“电子邮件”变量应该是什么?“用户名”是模型吗?通常您使用用户名和密码进行身份验证,在您的门户中,您希望使用电子邮件进行身份验证,因此您需要将“身份验证”字段从用户名更改为电子邮件。因此,我的应为“电子邮件”=>“密码”是吗?还是我弄错了?默认情况下是:
'fields'=>array('username'=>'username')
在您的例子中是'fields'=>array('username'=>'email')
。因为对于登录,您使用电子邮件而不是用户名。您是否在User.php模型上定义了public$primaryKey
?
<?php echo $this->Form->create('User',
array('url' => array('controller' => 'users', 'action' => 'login'),
'inputDefaults' => array(
'div' => 'form-group',
'label' => array(
'class' => 'col col-md-3 control-label'
),
'wrapInput' => 'col col-md-9',
'class' => 'form-control'
),
'class' => 'well form-horizontal'
)); ?>
echo $this->Form->end(__('Login'));