Last Login Functionality

Hello again,
Thought I’d write another little article about storing a users last logged in date. Very simple to do, first of all you must set the code below in your app_controller.php
1$this->Auth->autoRedirect = false;
What this does is, stops the login function from automatically redirecting after CakePHP has done its login magic!
Initially, when I wrote this code my login function looked something like this:
123456789function login() {
    if ($this->User->Auth) {
        $id = $this->Auth->user ( 'id' );
        $this->User->id = $id;
        $this->User->saveField ( 'last_login', date ( 'Y-m-d H:i:s' ) );
        $this->redirect ( array ('controller' => 'forums', 'action' => 'index' ) );
In trying to keep with the ‘Fat Model Skinny Controller’ idea I decided to re-factor part of the login function into a method in my Model.
1234function lastLogin($id) {
    $this->id = $id;
    $this->saveField('last_login', date('Y-m-d H:i:s'));
Now this is a very simple method, as you can see the user’s ID is passed through the variable argument then the ‘last_login’ field is updated appropriately with the current time.
This is what my login function ended up looking like
12345678    function login() {
        if ($this->Auth->user()) {
            $id = $this->Auth->user('id');
            $this->redirect(array('controller' => 'forums', 'action' => 'index'));

Although this is an extremely small example of ‘Fat Model Skinny Controller’, its best to get into the habit of keeping all business logic out of the controllers, use the controllers for request logic only.