开发者

CakePHP login help

I've built a very simple login system using CakePHP but now I have some questions:

1.) How do I show a setFlash message when the user logs in? I tried adding one to the lo开发者_开发知识库gin method but then it will just show it all the time when visiting the login page and does not show it when successfully logging in? But works with the logout method?

2.) When a users login they are ALWAYS taken to the home page because of the loginRedirect, but how do I send them to the page they are currently on? As the login form is in the header of public pages so when they login I want them to be sent back to that same page.

Here is the code I have in place at the moment in my 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');

}

and here is my 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());

}

and here is the login form that is in the header of every public page (note that I also have a dedicated login page as well that contains the same form):

<?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 &#9658;</button>
        </li>
    </ul>

</fieldset>

<?php echo $this->Form->end(); ?>


Here's a snippet from one of my login methods -

function login() {
    // check it's a valid user
    $this->Session->setFlash('You have successfully logged in');
    $this->redirect(array('controller'=>'dashboard','action'=>'index'));
}

And my dashboard controller uses a template with:

<?php echo $this->Session->flash(); ?> in it.

That does (I believe) exactly what you are asking for in #1

As for #2, you may have some luck using $this->redirect($this->referer());. See Take user back to previous page after logging in? for more information.

If you don't want to redirect automatically, you can use

$this->Auth->autoRedirect = false; in the beforeFilter


Cake's login component is never "simple" u_U

1) I had the same problem.. setFlash will show the message on the next page.. and it seems that the message is getting lost on some redirect that the Auth component does.

Try @Ross method, but if that doesnt work, you might want to overwrite the AuthComponent, and add the setFlash() to the login method on your own AuthComponent.. something like this:

class MyAuthComponent extends AuthComponent {
    function login($data = null,$public = false) {
        $this->Session->setFlash('WELCOME!');
        parent::login($data,$public);
    }
}

2) I'll copy what the Class API says about the redirection

loginAction mixed A URL (defined as a string or array) to the controller action that handles logins.

LoginRedirect mixed Normally, if a user is redirected to the $loginAction page, the location they were redirected from will be stored in the session so that they can be redirected back after a successful login. If this session value is not set, the user will be redirected to the page specified in $loginRedirect.

basically, the Auth component will read the Session looking for $_SESSION['Auth']['redirect'] value and if not found, it will use the LoginRedirect attribute. Since you're using the login page on the header, an easy work around would be to always add the $_SESSION['Auth']['redirect'] to the current page in the appController. You can use $this->here to get the current page.. maybe there's another way.. but i haven't found anything in cake's doc

Good Luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜