For long time i was thinking of creating a web application since my daily job does not requires me to work on web applications. So i have to keep myself updated with the latest web technologies by doing some hobby or kind of innovation projects.
I had created web applications in the past using java stack i.e. jsp, servlets, EJBs ,Struts and Spring as well as using SAP's application stack of HANA , XS and UI5.
But i have always found that most of the consumer web applications are developed using PHP (For e.g. Facebook and Wordpress). So, i thought it becomes imperative for me to learn PHP and develop a simple web application with CRUD operation.
By CRUD i meant create, read, update and delete operation on a business domain object.
So i decided to write up my application with the combination of PHP and MySQL as backend.
Below post will describe my initial steps from the choice of database, to the stack opted , to the IDE chosen and finally to the application. My initial thought of the application is like the below diagram.
I downloaded Bitnami's WAMP (Windows Apache MySQL PHP ) stack. To download and find more details click
Here. I Used Bitnami stack because it eases out the setting up of the Apache server, adding php as a component in apache, setting up mysql etc. You can also later on install applications as you like into the bitnami stack. You can obviously download them separately and set them up.
After you have setup your bitnami properly you must get the below screenshot if you hit localhost:80
Now coming to the development tools, since i had ample experience with eclipse , i decided to use eclipse and PHP development tools. For download and for more details click
https://eclipse.org/pdt/. PDT gives me all the IDE assistance for developing PHP code like code completion, code formatting etc . Also i am able to be there in the Eclipse envrionment and take advantage of all the other features of Eclipse.
I started with creating a simple php application and it was quite easy to start up a PHP application without db interaction. Since PHP is an interpreted language you don't need to start your server, every time you make changes to your code.
Naturally the second step was to debug my application. For this eclipse provides two options.
- Using zend debugger for debugging
- Using XDebug debugger for debugging
I started with Zend debugger but found few problems like not getting suspended at the breakpoints. More details
here. So i switched to xdebug debugger which is working fine till now. Follow the steps mentioned in the
xdebug installation guide to fall into a seamless path of xdebug based debugging.
So after setting up a basic php application without db, I wanted to write a complete a application with basic CRUD operations on a business/domain object.
In my previous web applications i have coded most of these parts by myself.. Then i found that some web frameworks could have done that job for me. So this time i wanted to be smart and generate boilerplate code for my CRUD operations.
So my requirement was to give a business object or domain object,create them db and then generate the POJOs/Object classes for my model with fields of the db mapped to the object property of my php POJO class. Also the corresponding UI for the model must be generated.
Searching a bit found me CakePHP. Check more details about CakePHP
here. Do give a check for the license of CakePHP before you use it. It perfectly fitted my requirements for a hobby project.
CakePHP provides a model view controller boilerplate code for a db table i.e. ORM class for db table, controller code and UI code for that db table based ORM class. It also generates some basic code for logging ,ORM, exception handling, unit automated tests (very useful) etc. Obviously you can then customize them according to your needs.
So once you have your language (PHP) , your database (MySQL) and your web server (Apache Server) ready you can embark on your journey of using CakePHP.
I followed the bookmarker tutorial
here , just replaced the bookmarker model with my model.
You can find all the steps in detail mention in the tutorial , but i will mention here in broad/easy steps.
- Get your CakePHP first by using Composer.
- Check your installation directory structure to verify if CakePHP has been properly installed.
- Create your database by following CakePHP's conventions
- Create your database configuration file. This file is used by CakePHP to connect to the specific database, and to create scaffolding code for the table given. Please note the username/password you are giving. The user account you have given must be there in the mysql's user list.
- Now execute "cake bake" commands to generate the boilerplate code for your project. (More information on the bake console is here. This is the most intriguing part where plethora of code is generated for you.
- If your commands are successful, then you must code being generated under the project directory.
Now some details into the code generated.
- <Your application>/src/controller: Contains the controller code for your table. For e.g. if your table is bookmarker, so you will find another class named BookmarkerController along with AppController. This forms the "C" of MVC pattern. Job of this class is to route the incoming requests and response and initialize any other framework related classes.
- <Your application>/src/Model: Contains the model code for your table. This forms the "M" of MVC. Further this folder contains three sub folders 'Table' , 'Entity' and 'Behaviour'. Table class extends the CakePHP table class which contains the CRUD methods for reading/writing to Database. Entity class is used to modify any record specific behaviour. I did not felt the need to customize the 'Behaviour' class yet.
- <Your application>/src/Template: Contains the rendering code of your view. This forms the "V" of MVC. You can further modify these files (.ctp) to change the layout of your view.
There are some other classes also got generated, but for my very first basic step i did not need them .So, that's it my basic application with CRUD functionality was ready.
Hit your localhost/<my app>/<your controller name> and see your application running.Once you hit the above application. You actually hit the 'index' action of your controller. Below is the snapshot of my application. ( i have hidden my domain objects in my snapshot).
Have look at the layout below. It autmagically (that's what its called in CakePHP) fetched the content from db ( I had two rows). Filled my UI. Also have UI actions for the other crud operations like view, edit and delete at the right side of the page. So i am relieved of doing the basic stuff of getting the UI up, writing code for UI, adding styling etc. etc.
 |
Initial page of the application |
In the next post i will write up on some of the features of CakePHP and will dig deeper into it. I will also touch upon the customizations which i have done in my application for e.g. adding enum support for my application ,authentication and authorization functionality.
Add a comment