Create a simple To Do List in DooPHP – Part 1 « Learn DooPHP

11
Learn DooPHP Log in Home About Contact Forum SUBSCRIBE TO RSS Learn DooPHP : High performance PHP framework 21 Sep 2009 by Richard 16 Comments » Tutorials beginner , database , demo , easy , model , ORM , tutorial Create a simple To Do List in DooPHP – Part 1 Introduction 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 Our Objective for this Tutorial In this tutorial we will be creating a simple To Doo List Manager Web Application and through the course of the tutorial we will be covering the following topics: Obtaining and Deploying the Base DooPHP Application Setting up the MySQL Database

description

doophp

Transcript of Create a simple To Do List in DooPHP – Part 1 « Learn DooPHP

Page 1: Create a simple To Do List in DooPHP – Part 1 « Learn DooPHP

Learn DooPHPLog in

HomeAboutContactForum

SUBSCRIBE TO RSS

Learn DooPHP: High performance PHP framework

21 Sep 2009

by Richard

16 Comments »

Tutorials

beginner, database, demo, easy, model, ORM, tutorial

Create a simple To Do List in DooPHP – Part 1Introduction

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

Part 1 – Getting StartedPart 2 – The Signup FormPart 3 – User Authentication – Coming SoonPart 4 – The To Do List – Coming SoonPart 5 – Adding AJAX Functionality – Coming Soon

Our Objective for this Tutorial

In this tutorial we will be creating a simple To Doo List Manager Web Application and through the course of thetutorial we will be covering the following topics:

Obtaining and Deploying the Base DooPHP ApplicationSetting up the MySQL Database

Page 2: Create a simple To Do List in DooPHP – Part 1 « Learn DooPHP

Creating the ModelsCreating the To Do Manager Application

Signup FormUser AuthenticationUser Home PageTask ActionsEnhancing you application with AJAX

You can view a demo of the finished project here. Some screen shots of the final application are also included below:

TODO: IMAGES TO GO IN HERE WHEN I FINISH!

TODO: UPDATE THE DEMO LINK ABOVE

Obtaining and Deploying the Base DooPHP Application

Before you can begin working on the To Doo List Manager you will need access to a Web Server. This guide assumesyou already have webhosting or better yet running a development server locally which supports PHP5 and a MySQL.If you do not yet have your own server to use then I suggest taking a look at using a WAMP server on your localsystem : http://www.wampserver.com/en/.

You will also be needing a copy of DooPHP. You can download the latest release from here (for this demo I am usingVersion 1.2). IF you want to use the very latest source for DooPHP take a look and download the code from here :DooPHP on Google Code

Once you have downloaded one of the compressed packages open it and copy the dooframework folder into theprojects root folder and the contents of the app folder into your public_html folder. This should then give you a folderstructure similar to the following:

X:\todomanager\dooframework\public_html\

global\protected\tools\.htaccess.exampleindex.php

In the above example apache has been configured to have the DOCUMENT_ROOT set tox:\todomanager\public_html\ with the project root therefore being : x:\todomanager\

At this point we need to make a few changes to the configuration of DooPHP in order for it to work under the filestructure being used for this demo. Therefore you first need to open the filepublic_html\protected\config\common.conf.php and make the following changes:

1. Change the date_default_timezone_set(…) to your own local time zone2. Change the $config[‘SITE_PATH’] and $config[‘BASE_PATH’] to:

1.2.

$config['SITE_PATH'] = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR;$config['BASE_PATH'] = dirname($config['SITE_PATH']) . DIRECTORY_SEPARATOR . 'dooframework' .

DIRECTORY_SEPARATOR;

Page 3: Create a simple To Do List in DooPHP – Part 1 « Learn DooPHP

3. Save the file

The SITE_PATH is the folder containing the index.php file (in our case x:\todomanager\public_html). TheBASE_PATH is the location of the dooframework which in this example is x:\todomanager\dooframework\

If you have set this all up correctly then you should now be able to visit http://localhost/ (of course you should changethis if your site is hosted somewhere other than localhost) and see a welcome page provided with DooPHP telling you“It Works!”. If you do not see this check you apache/php error logs and check everything is correct. If you are stillhaving problems you may find some help in the forums.

Now you are ready to create the database used for the rest of this tutorial.

Setting up the MySQL Database

Our To Doo List Manager will allow users to sign up and register for an account. This will require them to pick ausername and password which we will later use to authenticate the user. In addition we will ask them for their actualname so we can display this to them when they have logged in.

Each registered user will be allowed to have a single To Do List and each users To Do List will be private from all theother users. We will therefore require:

A “User” table to hold:

idusernamepasswordname

And another table “ToDoItem” to hold:

iduser_idtaskcompleted

There will be a simple onetomany relationship between Users and ToDoItems with each user able to have many ToDo Items.

I have also decided to keep this demo as simple as possible so although we could collect other information like the duedate for a task and providing multiple To Do Lists per user we will for now leave such extra features alone although Iencourage you to try adding these features yourself as a learning exercise.

In this example I am using a database called ToDoManager and the SQL to create the tables above is show below:

01.02.03.04.05.06.07.08.09.10.

CREATE TABLE ToDoManager.User (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,username VARCHAR( 20 ) NOT NULL ,password CHAR( 32 ) NOT NULL ,name VARCHAR( 40 ) NOT NULL); CREATE TABLE ToDoManager.ToDoItem (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,user_id INT NOT NULL ,

Page 4: Create a simple To Do List in DooPHP – Part 1 « Learn DooPHP

You will need to run the above SQL statments on your SQL server (I assume here that you know how to do this).Also note that the synxtax above is intended for MySQL and has not been checked against other database engines.Therefore you may need to change it slightly to use it with your database.

Creating the Models

Now that we have our database created we need to let DooPHP know about its existance. To do this we must firstopen the DB configuration file found in public_html\protected\config\db.conf.php.

You will need to uncomment the last two lines starting $dbconfig. You must also specify your databases credentials.

In the above code you will need to set your databases settings. The parameters are: Host Name, Database Name,Username, Password, Database Engine and lastely if you want to use a persistent connection.

DooPHP allows us to define both the development and production settings to be used for accessing the database. Inthis example we will assume the details are the same but in real world applications you will likely have differentdatabase settings for the development and production environments. DooPHP determins which setting to use based onthe $config["APP_MODE"] setting defined in the file public_html\protected\config\common.conf.php. This iscurrently set to default to dev (developer) mode.

We must also inform DooPHP about the relationships which exist in our database. We only have one relationship inthis demo which is a mapping of one User to Many ToDoItem (ie. OneToMany). To tell DooPHP about this youneed to place the following code just above the $dbconfig lines we just defined above.

This is essentially telling DooPHP that a User has many ToDoItem’s where each ToDoItem has a user_id whichbelongs to each User who’s id matches it. e.g. if a User has an ID of 7 then any ToDoItem with a user_id equal to 7belongs to this user. To learn more about defining relationships in DooPHP please refer to the Model Guide.

We must also let DooPHP know that this application is utilising a database and to therefore load the settings to achievethis. Therefore we need to open the public_html\index.php file and uncomment the following lines of code:

Now that DooPHP knows about our databases we can make use of the Model Generator tool supplied with the Baseapplication in the DooPHP download package to generate the models automatically for us. Simply navigate back to theDooPHP website http://localhost/ and click on the “Generate Models” link. At this point you may be asked for ausername and password, its currently set to “admin” and “1234″.

If the model generation works as expected then it should report the creation of 2 php files:

Todoitem.php

11.12.13.

task VARCHAR( 255 ) NOT NULL ,completed BIT( 0 ) NOT NULL);

1.2.

$dbconfig['dev'] = array('localhost', 'ToDoManager', 'username', 'password', 'mysql', true);$dbconfig['prod'] = array('localhost', 'ToDoManager', 'username', 'password', 'mysql', true);

1.2.

$dbmap['User']['has_many']['ToDoItem'] = array('foreign_key'=>'user_id');$dbmap['ToDoItem']['belongs_to']['User'] = array('foreign_key'=> 'id');

1.2.3.4.5.

include './protected/config/db.conf.php';/*...*/Doo::db()>setMap($dbmap);Doo::db()>setDb($dbconfig, $config['APP_MODE']);Doo::db()>sql_tracking = true;

Search... Random Join ChatShare

Page 5: Create a simple To Do List in DooPHP – Part 1 « Learn DooPHP

User.php

These files can be found in public_html\protected\model\ if you wish to take a look at them. If you are not informed ofthe models creation please check over the previous steps and ensure you have done everything listed. If you are stillhaving problems try asking for some help in the forums.

Creating the To Do Manager Application

Now that we have got the Base DooPHP application setup and we have also setup and configured our Models we canstart thinking about how users will interact with our Web Application.

We will need to provide users with a number of possible actions to perform and these will be split between a numberof Controllers which will group similar actions together. Users will be able to carry out the following actions on thesite:

Signup for an accountLoginLogoutView their own To Do ListAdd new tasks to their listUpdate the status of a specified task (Incomple, Completed)Delete a task

We will therefore break this down into 2 controllers. One to handel user registration and authentication activities andanother to manage the users To Do List

Just before we start working on these Controllers we must delete some files provided in the Base Application whichwe no longer need. Therefore delete the following files

public_html\tools – Delete this folderpublic_html\global\css\demo.css – Delete this filepublic_html\protected\config\forms – Delete this folderpublic_html\protected\controller\* – Delete all files in this folderpublic_html\protected\viewc\* – Delete all files in this folder

You can also open up the public_html\protected\config\routes.conf.php file and delete all the lines of code below the$admin = array(’admin’=>’1234′); (and this line as well) but keep the closing ?> php tag.

We are now ready to start creating our signup form Part 2 – Creating the Signup Form

0 4 0

16 Comments

« Previous Post Next Post »

Page 6: Create a simple To Do List in DooPHP – Part 1 « Learn DooPHP

+1

1

+1

+3

mhdex21 Sep, 2009

whoa pretty detailed, nice! Looking forward for the app demo and some screenshots. Will the demo be in thedemo section of http://www.doophp.com ?

zares21 Sep, 2009

Thanks Richard.There is a small remark – the central block in this wptheme initially not wide, and – in addition steals width of acontent, therefore – do conclusions…

Richard21 Sep, 2009

I’ll contact leng and see if he can alter the WP theme or possibly do it myself although I’m not sure if Ihave access to do this at the moment.

I’m hoping leng can put the tutorial demo into the demo section of the site later for people to try out andwill also provide a zip file containing the demo for you to download.

Leng21 Sep, 2009

Hey right, gotta modify this WP theme asap.

Richard

Page 7: Create a simple To Do List in DooPHP – Part 1 « Learn DooPHP

21 Sep, 2009

I think its going to be a width being a % and/or a min/maxwidth property. On my larger monitors I haveno problems with text going over the right hand bar…but on this laptop with smaller screen res the contentspans over the right column.

ai22 Sep, 2009

中文?

Richard22 Sep, 2009

对不起,不会说汉语。如果你是中文版后,会有人需要翻译。

或使用谷歌翻译:http://translate.google.com/translate?prev=hp&hl=en&js=y&u=http%3A%2F%2Flearn.doophp.com%2F2009%2F09%2Fcreateasimpletodolistindoophppart1%2F&sl=en&tl=zhCN&history_state0=

img12 Dec, 2009

真不错的教程! Great works!

thanks a lot!

[email protected] Jan, 2010

Page 8: Create a simple To Do List in DooPHP – Part 1 « Learn DooPHP

+1

when you will post the others?

Richard7 Jan, 2010

I’m afraid that right now I can not give an answer but it will hopefully happen soon.

Carlos8 Jan, 2010

Tag this as tutorial, it doesnt come up in the search for that keyword. It doesnt come up in google(doophptutorial) either.

aboshooq16 Feb, 2010

Dear Richard,

I couldn’t get it work,, related to routing issue!!it say something like this:”ERROR 404 not found

This is handler by an internal Route as defined in common.conf.php $config['ERROR_404_ROUTE']

Your error document needs to be more than 512 bytes in length. If not IE will display its default error page.

Give some helpful comments other than 404 Also check out the links page for a list of URLs available in thisdemo.”here is the url “af_do/app/index.php/gen_model”

Page 9: Create a simple To Do List in DooPHP – Part 1 « Learn DooPHP

+1

aboshooq16 Feb, 2010

Dear *,

my issue is been fixed,it was related to $config['SUBFOLDER'] and $config['APP_URL'].I fix it but not using the proper way..–> $config['SUBFOLDER'] = “/fahad/af_do/app/”;

Thank u *

Farhad Sakhaei18 Apr, 2010

ThanksForTheGuideJust there was a bit mistake:

$dbconfig['prod'] = array(’localhost’, ‘ToDoManager’, ‘password’, ‘password’, ‘mysql’, true);

Should be:

$dbconfig['prod'] = array(’localhost’, ‘ToDoManager’, ‘username’, ‘password’, ‘mysql’, true);

Richard21 Apr, 2010

Thanks, Just corrected them

wrlee16 Jul, 2010

I am looking forward to tips on Ajax usage.

Page 10: Create a simple To Do List in DooPHP – Part 1 « Learn DooPHP

Search...

Our Sponsors

Choose Language

English中文RussianDeutschEspañolArabicPortugese

Popular Tags

Find Tutorial

EasyNormalDifficult

Categories

Demos & Snippets (4)News & Updates (2)Screencast (1)Tutorials (17)

Recent Comments

kukat on Intro to DooPHP slides (PHP Malaysia meetup 2011)Milos Kovacki on Using DooTranslator for translationWeb dev Greece on Using DooTranslator for translationMilos Kovacki on Handling sessions without Apache sessionsT on Create a simple To Do List in DooPHP – Part 2

Page 11: Create a simple To Do List in DooPHP – Part 1 « Learn DooPHP

Blogroll

Development BlogDocumentationForumGoogle Code

Home About Contact Forum

All contents copyright © Learn DooPHP. All rights reserved. Theme design by WebKreation.