Concepts


In this post we are going to add a ‘users’ table to our MySQL schema ‘aa01′ and create a data model, a controller and some views.

Currently, if we load the URL http://localhost/aa01/index.php a Welcome page is displayed and the messages are mainly green, signifying all is well with our ‘aa01′ project. Now, if we type http://localhost/aa01/index.php/users or http://localhost/aa01/index.php/login, all hell breaks loose, the messages turn red, signifying not so well. Read messages carefully as they show a lot of info.

Users table

Let’s add the users table. Get into MySQL and run the following script. The minimum fields we need are id, username and password.

DROP TABLE IF EXISTS `aa01`.`users`;
CREATE TABLE  `aa01`.`users` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `username` varchar(45) NOT NULL,
  `firstname` varchar(45) default NULL,
  `lastname` varchar(45) default NULL,
  `active` tinyint(1) NOT NULL default '0',
  `email` varchar(100) default NULL,
  `password` varchar(40) default NULL,
  `randomsalt` varchar(40) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB;

While you’re in MySQL, add a couple of users like Joe, password ‘joe08′ and Jane with a password of ‘jane08′ just for testing.

Bake Model

Back to our staple comand line. Run cake bake -app aa01 again. A console command line shows asking what next.

Select ‘M’ for model, ‘1′ for the User model. Enter validation ‘alphanumeric’ for username and password. Skip model associations and press OK. The file \aa01\models\user.php will be created.

Note that the Bake console times out after 300 seconds, so if you take too long, you have to start again. Also action any requests for test file generation, so in the future you might test your project.

Bake Controller

Back to our staple comand line. Run cake bake -app aa01 again. A console command line shows asking what next.

Select ‘C’ for controller, ‘1′ for the Users model. Select ‘Interactively’, skip ‘Scaffolding’, include basic class methods. Skip admin routing, other helpers, components. Yes to using Sessions and press OK. The file \aa01\controllers\users_controller.php will be created.

Bake Views

Last in this baking series is to bake the simple views Index, Add, Delete and View. Back to the trusty bake command.

Select ‘V’ for Views, ‘1′ for Users model, Interactively, Yes to scaffolded basic views, No to admin routing, OK to create.

Some local tests

Try the URLs below;

http://localhost/aa01/index.php - Shows Welcome page

http://localhost/aa01/index.php/users – Lists users

http://localhost/aa01/index.php/users/add- Add a user

http://localhost/aa01/index.php/users/delete – fails to delete, lists users

http://localhost/aa01/index.php/users/list – fails

http://localhost/aa01/index.php/users/edit – fails to edit, lists users

http://localhost/aa01/index.php/users/view – fails to view, lists users

http://localhost/aa01/index.php/users/login – fails

http://localhost/aa01/index.php/users/logout – fails

http://localhost/aa01/index.php/users/register – fails

Almost finished

Now we have users loaded in the database, a basic working front end to our project (except login / logout / register) where we can add / delete / edit / list users. Go on, try adding, editing and deleting some users. Try silly things like submitting the add form with no username, watch for error messages.

The whole CakePHP process was simple after all, wasn’t it?
Next post is login / logout /register.

The start to *baking is to download the latest build of the platform. The one I’m using today is 1.2.0.6311 and it is downloaded from the downloads page on the CakePHP website. The file name I grabbed is cake_1.2.0.6311-beta.zip

As my platform is Windows XP, I have a My Documents folder which I refer to as MyDocs, which if not managed with a few sub-folders it can be a dumping ground, where it is hard to find anything. All my code downloads go into MyDocs under a folder called Code. There is a sub-folder called Cake, into which I dump the zipped files of the latest version. That way, when I back up things on the laptop, I might just grab the whole MyDocs folder, or just the Code folder, and everything in it.

Looks a bit like this:

 MyDocs
   ...
   Code
     ...
     ASP
     Cake
       ...
       cake_1.2.0.6311-beta.zip
       ...
     C#
     HTML
     PHP
     ...
   ...

When I open the zip file I am going to be asked where to extract the files to. I am using the free Apache webserver on my laptop, it lives at c:\apache224, so the structure looks like so:

 /
   ...
   Apache224 - this is the folder that contains
               my local Apache webserver
               for development and testing
               (version 224 by the looks of it)
     bin
     cgi-bin
     conf
     error
     htdocs  - this is the folder http://localhost lists
               when entered into a browser
               and also the folder I grab for backups
               of all my web work
       ...
       aa01 -  my various web projects below here
       ...
       cake12
       ...
       project01
       project53
       projectxyz
       ...
       phpinfo.php
       ...
     icons
     logs
     manual
     modules
   ...

Where I put the files is under the htdocs folders in a folder called \cake12. In Apache I have turned off the default auto open feature of index.php so I can see a listing of all my web projects when I type http://localhost into the browser address bar. This is my development machine, not a live public web server, so I want to see all my projects listed.

Some might disagree how I structure my folders. I like it this way, projects are totally separate from the \cake12 folder because when I test publish a project to the live web site, I only want to upload changes from the project folder. When developing and testing, the project files get changed often.

The core cake12 files only get loaded to the live site by me when a new build is released by CakePHP and I actually download it. The core files are in a different folder under htdocs, on both the local and the live websites, so that the files can be easily changed in their respective, but separate, folders. Each site closely mirrors the other – easier to follow – the only thing that changes is the domain name. Either locally I enter say http://localhost/index.php or on the live site I use http://mydomain.com/index.php.

The folder I am going to use for my project is \aa01. It is named with a leading ‘aa’ to present at the top of the list. This folder will be created automatically during the bake project process in the next post.
Note that I also have the file phpinfo.php in the root of my local website – there have been a fair few number of times I have had to open this file locally to check some info about my webserver settings. Do NOT put this file on the live website, it might give a hacker too much info.Keeping it simple initially takes a little thought about the folder structure and it seems to work effectively in practice.

*Baking – writing code / developing an application to run on the CakePHP platform