我已经建立了使用CakePHP一个非常简单的登录系统,但现在我有一些问题:CakePHP的登录帮助
1)如何显示setFlash
消息,当用户登录?我尝试在登录方法中添加一个,但是当访问登录页面时它会一直显示它,并且在成功登录时不显示它?但使用注销方法?
2.)当用户登录时,由于loginRedirect他们总是被带到主页,但是如何将它们发送到他们当前所在的页面?由于登录表单位于公共页面的标题中,所以当他们登录时,我希望它们被发回到同一页面。
这是目前在我app_controller.php代码我已经到位
var $components = array('Auth','Session');
function beforeFilter()
{
$this->Auth->authorize = 'actions';
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'home', 'action' => 'index');
$this->Auth->logoutRedirect = array('controller' => 'home', 'action' => 'index');
}
,这里是我的users_controller.php中
/**
* Log in
*/
function login()
{
$this->Session->setFlash('You\'re now logged in');
$this->layout = 'login';
$this->set('title_for_layout', 'Log in');
}
/**
* Log out
*/
function logout()
{
$this->Session->setFlash('<div class="content"><h2>Pow</h2><p>Moo</p></div>');
$this->redirect($this->Auth->logout());
}
这里是登录形式,是在每个公共页面的标题(注意,我也有一个专用的登录页面,并且包含相同的表单):
<?php echo $this->Form->create('User', array('id' => 'loginform', 'type' => 'post',
'url' => array('controller' => 'users', 'action' => 'login'))); ?>
<fieldset id="login">
<ul class="clearfix">
<li id="li-username">
<?php echo $this->Form->input('username', array('label'=>false,'placeholder'=>'Username or email address')); ?>
</li>
<li id="li-password">
<?php echo $this->Form->input('password', array('type'=>'password','label'=>false,'placeholder'=>'Password')); ?>
<span id="iforgot"><?php echo $this->Html->link('?',
array('controller' => 'users', 'action' => 'forgotpassword'), array('title' => 'Forgot your password?')); ?></span>
</li>
<li id="li-submit">
<button type="submit" title="Log in">Log in ►</button>
</li>
</ul>
</fieldset>
<?php echo $this->Form->end(); ?>
你不能在登录方法上设置'flashdata',然后重定向到说'dashboard',它只显示一次flashdata? – Ross 2011-05-06 13:57:23
那么重定向目前是自动完成的,但是如何显示setFlash消息来表明你现在已经登录并欢迎回来! – Cameron 2011-05-06 14:54:56