Build a forum with CakePHP (part 2)

In this tutorial we will build authentication system for CapForum. Authentication is handled by CakePHP's Auth component. This tutorial will teach you how this component works. You will be able to use this component in your other projects if you have not mastered it yet.

Download link of entire source code is provided in part 4 of this series.

Work with controllers

There are some common components we are using throughout this application. Thus we can add them to the AppController.

Copy the codes below to "app/Controller/AppController.php":

<?php
App::uses('Controller', 'Controller');
 
class AppController extends Controller {
     
    public $components = array('DebugKit.Toolbar','Auth','Session');
     
}

As you can see, we included common components 'DebugKit.Toolbar','Auth' and 'Session' to our application.

Next, create file "app/Controller/UsersController.php" with codes below:

<?php
App::uses('Controller', 'AppController');
 
class UsersController extends AppController {
     
    public function beforeFilter() {
        parent::beforeFilter();
         
        $this->Auth->allow('profile');
    }
     
    public function profile($id=null) {
         
    }
     
    public function login() {
        if($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Invalid username or password'));
            }
        }
    }
     
    public function logout() {
        $this->redirect($this->Auth->logout());
    }
}

UsersController is the class for handling authentication. There is one additional action "profile", this is a dummy profile page which you can extend this application later.

Let us take a close look at Auth component's functions:

  1. $this->Auth->allow('profile'): Since profile is public, we call this function in "beforeFilter()" to make it public, This way, visitors do not have to login to view other's profile.
  2. $this->Auth->login(): This function logs user in using the POST data.
  3. $this->Auth->logout(): This function de-authenticate the logged in user. It will destroy the session accordingly.

Work with views

View files are straightforward. There are two view files for this section. They are "app/View/Users/login.ctp" and "app/View/Users/profile.ctp".

Copy following to "app/View/Users/login.ctp":

<div class="row">
          <div class="col-lg-6 col-lg-offset-3">
             
                <h2>Login</h2>
           
                <div class="well">
                     <?php echo $this->Session->flash(); ?>
                    <?php echo $this->Form->create('User',array('class'=>'form-horizontal','inputDefaults'=>array('label'=>false)));?>
                          <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">Username</label>
                            <div class="col-sm-10">
                              <?php echo $this->Form->input('username',array('class'=>'form-control'));?>
                            </div>
                          </div>
                          <div class="form-group">
                            <label for="inputPassword3" class="col-sm-2 control-label">Password</label>
                            <div class="col-sm-10">
                               <?php echo $this->Form->input('password',array('class'=>'form-control'));?>
                            </div>
                          </div>
                          
                          <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                              <?php echo $this->Form->submit('Login',array('class'=>'btn btn-primary'))?>
                            </div>
                          </div>
                    <?php echo $this->Form->end();?>
                </div>
          </div>
</div>

"login.ctp" uses CakePHP's FormHelper to create a POST form. It is also recommended to use CakePHP's built-in helpers to create its view elements. Because CakePHP handles its naming convention on its unique way, view helpers will deal with the convention itself.

Copy following to "app/View/Users/profile.ctp":

This is my profile page

As you can see, this is really just some dummy text, you can put whatever you can. Or even put in some dynamic data later when you are familar with how CapForum works.

The end

This tutorial has been straightward. Now if you access the CapForum page, it should ask you to login similar to below:

CapForum Login

Login with username "caker" and password "password", which we have mentioned in part 1 of this series. You will be redirect to profile page:

CapForum Login

Next tutorial, we are going to build core of CapForum. Which includes building all the model, modifying the routes as well as build the forum index page.

Hopefully this simple tutorial helped you with your development. If you like our post, please follow us on Twitter and help spread the word. We need your support to continue. If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.