Agility – In Software Development, having this characteristic is one of the key to success these days. A lot of talented startups are using their flexible business models to quickly adapt to changes and widen their customer base. Developers are now adapting and embracing the “Jack of all trades” on a platform level as the demand for such is increasing day by day. Yet with all those agile web programming platforms, PHP stood out for me. Its simplicity and straight forward approach makes every developer feel the fulfilment of crafting an application. With the addition of several frameworks developed on top of it like cakePHP, yii, codeigniter etc, the capability of it just keeps getting better and better.
Moving on though, I’d like to show you how simple and agile a developer can be with one of the best Agile / Rapid Web Application Development Framework available: CakePHP
What are we going to do: Simple Registration process it has to have:
- Register a new person
- Edit an existing person
- Delete an existing person
- View all of the person
- User Interface for all
Its also note worthy that I will be doing all of these stuff as I write this down. I would say it may take you 15mins including the cakePHP initial code setup.
- I use MAC so this tutorial is solely for MAC users – although the only difference is the scripts and system path configuration which is the same as setting up environment variables in windows.
- I use MYSQL Database
- I use NetBeans PHP Plugin
Set it up: Download the cake and the bakery (its on 1 package only)
- Download MAMP for your Web Server and MySQL Database
- Install MAMP < Straight forward, execute the sh file and it will automatically generate the necessary folders
- Locate htdocs inside MAMP < this is where you will locate your cakePHP and projects as this is the default directory recognised by your web server and php parser (you can actually change it to any custom directory you want, this can be done by changing the DocumentRoot of httpd.conf inside the apache folder).
- Download the latest cake distro here: http://cakephp.org/
- Put it inside your htdocs folder (start your XAMPP to test).
- if you see the screen below, then your on the right track!
- Check the video for the .bash_profile: http://www.youtube.com/watch?v=KOBO-kEakQ0 for the environment variables.
- Export the following variables and values < This is to put the cake library in your classpath. This is important for you to easily access the commands for baking (code generation tool)
- Test it!
Database Setup: Lets setup your database and table objects
- Create a table named persons < This is cakePHP’s naming convention, tables are “recommended” to be plural as this will have multiple entries of a specific business data.
- Create the columns: Id, first_name,last_name,birth_date,email_address. Again, convention wise, words are separated by _ and a sensible column name.
- You are now ready to create your first cakePHP web application!
Cake Bake: Open terminal
- Type cake bake -app
- Type the location on where you want to put the app. In this case, put in htdocs folder.
- Skeleton folder is the default one so just press enter. Type in Y when ask if “Look okay?”.
Create a Database Connection called “default”.
- Database: Mysql
- Persistent Connection: For now, N
- Database Host: 127.0.0.1
- Port: 3306
- Username: <your db username>
- Password: <your db password>
- Database Name: registration_sample
- Press Enter all the way.
This will create a new “bakeable” project source code. Type in cake bake -app <location of the project source code>. This will run the cake bake command and will display a menu of code generators (screenshot below).
Generate Model Codes first: Press M
- Use the “default” datasource
- Choose 1. Persons
- Primary Key: 1. Id
- Display Field: first_name
- Validation Criteria for Fields: Y – this is where you define the validation. This will affect the validations on the user interface.
- Press Enter all the way
Generate Controller Codes: Press C
- Choose 1. People
- Press Enter up until the generation of basic class methods.
- Would you like to create some basic class methods index(), add(), view(), edit())? (y/n) = Y
- Would you like to create the basic class methods for admin routing? (y/n) = N
- Would you like this controller to use other helpers besides HtmlHelper and FormHelper? (y/n) = N
- Would you like this controller to use any components? (y/n) = N
- Would you like to use Session flash messages? (y/n) = Y
- Press Enter all the way
Generate View Codes: Press V
- Choose 1. People
- Press enter all the way
Go to: http://localhost/RegistrationPersons/People
and your done! You may now create, edit, delete and view all persons! All of these generated by the bake command.
Here is the actual source code! Enjoy!