A CakePHP user registration form example

CakePHP user registration FAQ: The cookbook covers the topic of CakePHP user login forms, but the CakePHP Auth module uses SHA1 for passwords, and the cookbook doesn't cover user registration (creating users, and SHA1 passwords); how do I set up a CakePHP user registration form, including all necessary CakePHP model, view, and controller classes?

Setting up a basic CakePHP registration form isn't too hard, er, well, once you know how to do it. Here's how.

Note: This article was originally written in January, 2011. Things have probably changed since then. But hopefully this tutorial can still help point you in the right direction.

CakePHP user registration form - The View

Let's look at the CakePHP user registration form first (the user interface). Keeping this as simple as possible for our example, let's just assume that we just want to get a desired username and password from the person who wants to register. Therefore, we create the following CakePHP user registration form named register.ctp in the app/views/users directory:

<h1>CakePHP Registration Form</h1>

  echo $form->create('User', array('action' => 'register'));
  echo $form->input('username');
  echo $form->input('password', array('type' => 'password'));
  echo $form->input('password_confirm', array('type' => 'password'));
  echo $form->submit();
  echo $form->end();

CakePHP user registration form - The Model

Nothing special is required in our CakePHP model class for a user registration form. It can be as simple as this:

class User extends AppModel
  var $name = 'User';

CakePHP user registration form - The Controller

Skipping anything else our UsersController class might need to do, it can look as simple as this:


class UsersController extends AppController
  var $name = 'Users';
  var $scaffold;

  # needed for 'register'
  var $helpers = array('Html', 'Form');

  function beforeFilter()
    // tell Auth not to check authentication when doing the 'register' action

  function login()

  function logout()

  # TODO do some data validation here
  function register()
    if (!empty($this->data))
      if ($this->data['User']['password'] == $this->Auth->password($this->data['User']['password_confirm']))
          $this->redirect(array('action' => 'index'));


The register function in this class is the important function for the current discussion. It does practically no data validation or error handling as shown; again, this is a simple demo to help get you going.

CakePHP user registration form - The AppController class

The last piece of the puzzle to our CakePHP registration form is that we need to make sure we include the CakePHP Auth component somewhere. For my purposes I've included the Auth component in my AppController class, because there are other things I need it for. Therefore, the simplest possible AppController looks like this:


# note: this class goes in /app, not /app/controllers
class AppController extends Controller
  var $components = array('Auth');


Of course your real-world AppController class is going to have functionality than this, but again, this is the least amount of code I think you'll need for this class.

CakePHP user registration form - The URI

Finally, if you put all of this together, I hope you can see that the URI for this CakePHP user registration form will look like this:


Or, if you prefer URLs, if your website is named example.com, and your application is named "foo", the complete user registration form URL will look like this:


So, just use the URI above as the HREF for your "Register Now" anchor tag, and you should be in business.

CakePHP Auth user information - After the login

I think it's also important to note that after your user logs into your application, the Auth object will be available from all of your CakePHP controller classes, at least in this configuration, where I create the Auth object in the AppController, and then have my controllers extend AppController. Because of this I can get access to the new user information, which is now stored in the Auth object, something like this:

$u = $this->Auth->user();

$name = $u['User']['username'];

$id = $u['User']['id'];

You don't have to take an approach exactly like this, but I wanted to show how you can access the username and id fields after the CakePHP user login.

CakePHP user registration form - Acknowledgements

I pulled this code together from several different sources, including littlehart.net and the CakePHP Cookbook docs. Many thanks to both of those sources for their work.

I hope this CakePHP user registration form example/recipe has been helpful. I'll try to add to it as I learn more, especially about form validation, CAPTCHA integration, and other issues, but until then, I hope this helps.



Very nice tutorial. I'm just getting started with Cake and PHP Frameworks, so I'm still wrapping my head around certain things. This did a good job of making it simple to understand and helped me put a few things I didn't quite understand in the proper context.