Create a simple To Do List in DooPHP – Part 2

This tutorial will guide you through all the steps required to get started using DooPHP through the creation of a simple To Do List Application. The tutorial is split into a number of sections and you will need to read the guide from the begining in order to understand whats going on.

Part 1 – Getting Started
Part 2 – The Signup Form
Part 3 – User Authentication – Coming Soon
Part 4 – The To Do List – Coming Soon
Part 5 – Adding AJAX Functionality – Coming Soon
Creating the Signup Form
The first thing we need to do is to create a new controller which will handel registration tasks. For this we will create a new Controller with the name RegistrationController and this will handel all aspects of registration. Right now this will consist of a single action – Signup. Therefore we must create a new file public_html\protected\controller\RegistrationController.php and the contents of this file will be:

To help make managing our code easier I have opted to place the View’s inside a folder matching the names of the controllers so in this example our controller is called Registration and the action is signup so I have placed the view template in registration\signup.html but you can use what ever location you want (as long as its under the public_html\protected\view\ folder. Also all controllers must be defined as the controllers name followed by ‘Controller’.

Now we have created the controller, the action and a template to be rendered we will configure the routes so that a visit to /signup/ will result in the user visiting out signup page. To do this you first need to open up the following file public_html\protected\config\routes.conf.php. This file defines all the the routing rules used by DooPHP. Edit the file so it contains the following:

You should have already of removed all of the predefined routes earlier.

The above route instructs DooPHP that on any GET request to http://localhost/signup it should process the RegistrationControllers signup function. If the user request the page using a POST request we will not do anything for the time being at least.

We will also ensure that auto routing is turned off (the default). To do this open up public_html\protected\config\common.conf.php and check that the following line is commented out or set to FALSE:

//$config[‘AUTOROUTE’] = FALSE; // Settomg this to TRUE will enable auto routing
It is recommended that you leave auto routing turned off in DooPHP as the Routing engine provides us with a lot more options than a simple mapping of Controller/Action to a controller action (which is what the Auto Routing does). To find out more on DooPHP’s routing support please check out : Routes Guide and Routing Demo

If you have not yet saved all these changes do so. Once the changes have been saved you should now be able to open up your web browser and point it to http://localhost/signup/ and hopefully if you have configured everything correctly you will see the signup form 🙂 If you see the form you can try clicking on the signup form and you will be given a 404 error because we have not yet setup a route to catch the POST /signup/ request.

Please note we have not yet setup a root mapping i.e. http://localhost/ to anything so you must add the /signup/ at the then of the url!

If you try clicking on the submit button you will see an error. Even though the form will send the user back to /signup/ it’s now sending a POST request and we have not set up a route to handel this. To pickup and handel the POST signup request we will add another route into the routes file so reopen public_html\protected\config\routes.conf.php and update it as follows:

/* Original comments here… / $route[‘get’][‘/signup’] = array(‘RegistrationController’, ‘signup’); $route[‘post’][‘/signup’] = array(‘RegistrationController’, ‘signup_action’); ?> Here we have added in a POST request route mapping /signup to the signup_action of the RegistrationController. We have done this so we can seperate the logic of a get request (which requires us to just render a view) from the logic of trying to sign the user up. If we where using the same controller action for both the get and post requests though we could of used the ‘’ option but this would also include PUT and DELETE requests and would look like this

$route[‘*’][‘/signup’] = array(‘RegistrationController’, ‘combined_signup_action’);
Now we have told the routing engine to send users to the signup_action action we need to define it within the RegistrationController. We will need to do the following when the user requests the page:

Make sure the user has sent a username, password and name with the form
Check the username, password and name are not empty (We will be using no other validation for now)
Ensure the username is not “Signup” (this will be a reserved word for later)
Check no one else has already registed the username
If we get this far we will register the user and then inform them it went okay
If we had a problem at an earlier stage then we inform the user of the problem
The code to do this should be added after the signup function in public_html\protected\controller\RegistrationController.php

I have added comments into the code and hopefully you should be able to follow what is going on. I will however provide a bit more information on the database / model part of the code.

Before we carry out any Database related tasks we first ensure the user has provided us with a valid username, password and name. This means we do not have to load up the model related code until its actually needed! Once we know we need to do something with the database we load the User model class (line 41). After we do this we can create an empty User instance (line 44) which will create a default (blank) User object.

Now we have a blank ‘User’ we can specify some of the properties on this user. As we are looking to ensure no 2 users will have the same username we specify the username we have been provided by the user in the $user object (line 47). Now we have specified a parameter we can run a search to find any other users with the same username (line 50). We also limit the result to 1 as we do not care for any other users (also there should only be a maximum of one match anyway).

If no user is found then the find(…) function will return false and we know the username is avalaible for this user to use. Should a match be found then find(…) will return a details on the matching records. If the username is free then we can specify the users other details (lines 54 and 55). We md5 the password to help ensure the security of the users password, you should also use different salt values if you use this in the real world. Once the users settings have been set we can instruct DooPHP to insert the new record (line 58) and we will store the result of this so we can let the user know how things went (lines 61-66).

We will look at some other Model and Database functions later in this tutorial but for more further information you can also refer to the DooPHP Model Guide and the Database Demo.

You will also notice in the action’s code that we have set some extra $this->data[‘…’] values. We will use these inside the template file to let the user know how things went. However, you may have noticed that we do not set default values for all the the variables we plan to expose to our template file. For example only the errorMsg OR successMsg will be set. This means that when we come to render the template and use these variables php will raise a warning that these variables are undefined and this will cause errors to be reported if suc warnings are enabled. Therefore we will wrap access to these potentially undefined variables using the isset function.

We can now update our signup template file (public_html\protected\view\registration\signup.html) to also be used by the signup_action action as we want to show the user the same screen. The only difference being that we now need to inform the user of whats going on. Therefore we will now update the view template just mentioned to be as follows:


03. To Doo List Manager :: {{pagetitle}} 04.


To Doo List Manager :: {{pagetitle}}



Please signup for an account to start using the To Doo List Manager. Registration is free to everyone




Error: {{errorMsg}}





Success: {{successMsg}}



18. 19. Username 20.
21. 22. Password 23.
24. 25. Name 26.
27. 28. 29.


31. Already registed for an account? 32. Login Now! 33.



We have made a number of changes to our signup template. The simplest additions are the new value=”…” for the username and name input controls. Here we are specifing that we want to set the value of each text box to whatever data the user last submitted. When a user visits /signup/ then these values will not have been defined so these will be left blank. If the user has submitted the form though and included data in one or both of these input controls we now have the signup_action function set the values of these variables to those last submitted by the user. This means we can save the user having to reenter them. We do not however send back the password.

The other addition to the template are the and markers. This is just like a simple if statement checking to see if the variable has been defined and if so displaying its contents. We will see some more of these tags as we progress through the tutorial.

Assuming this has all worked out okay then you should now be able to signup to the site by visiting http://localhost/signup/. I would suggest you try submitting the form a couple of times to see how it reacts to different data being entered into the form. This includes submitting:

An empty form
A form with some missing data
The form with all fields completed and a username of signup
The form with all valid data eg. DemoUser, p@ssw0rd, Demo User
The form with a username already used eg. DemoUser and different (or the same) password and name
Assuming this has all worked okay you will have seen a variety of different messages appear depending on the content you submitted. If you are not seeing these messages and getting errors etc please check over what you have done and if your still having problems ask for some help in the fourms.

We will come back to the RegistrationController later but for now we will move onto User Authentication.

Previous PostNextNext Post

Leave a Reply

Your email address will not be published.