Getting RAD with CakePHP
Ever gotten tired of rewriting a user login system every time you build a website, or database access scripts? Do you wish there were a way to create more reusable modular code? Well get ready to fall head over heels in passionate love with Rapid Application Development (RAD) Frameworks.
Wikipedia defines RAD as
A type of software development methodology which uses minimal planning in favor of rapid prototyping. The “planning” of software developed using RAD is interleaved with writing the software itself. The lack of extensive pre-planning generally allows software to be written much faster, and makes it easier to change requirement
Enough about RAD itself, lets get into some of the benefits. Hold on to your terminal cassonova…
- Speed up development
- Write less code
- Focus on functionality, not mundane repetitive tasks
- Create a more modularized and organized project
- The acronym is actually RAD, which, itself, is pretty rad…
There are countless numbers of RAD Frameworks across all platforms. Since this is more of an introductory post on RAD Frameworks we’re only going to go over a few basics. This post will detail some aspects of CakePHP, one of the more popular PHP frameworks. They describe it on their site as follows…
Using commonly known design patterns like MVC and ORM within the convention over configuration paradigm, CakePHP reduces development costs and helps developers write less code.
So lets dive right in, two basic but integral ideas…
MVC – Model View Controller, this is an architectural pattern which helps seperate business logic from presentation. The model handles data. The controller manipulates data. And the view presents the data.
ORM – Object Relational Mapping, in a nutshell, gives you objects which you can play with instead of having to access database data directly.
Now to the nitty gritty… Lets show you how to get CRUD (create, read, update, delete.. sorry for another acronym, hopefully last) up and running in no time flat..
Start with your database table. Lets say you were selling books. A typical book table might look something like this (simplified).
CREATE TABLE books (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Once you have your config file (user, pass, database) set correctly, now you get to ENJOY THE MAGIC OF CODE GENERATION. I can’t stress enough what this next part does for one’s mental health.
From a command line or terminal run the following commands..
- cake bake model book (creates your book model)
- cake bake controller books scaffold admin (creates your CRUD controller scaffolding with admin functionality)
- cake bake view books (creates all your views for CRUD)
Now, after three lines of code pasted into a terminal, you have generated a fully functional, extensible, modular CRUD application, including error reporting, user friendly flash messages, well, I hope you get the point..
The beauty of this is that you don’t waste time on mundane tasks, you can start hacking away on the unique functionality of your next killer app almost immediately.
Keep in mind there are a few more caveats and gotcha’s when working with a framework such as CakePHP, but generally you will find them to be a godsend in your web development world. A world where heinous devilish monsters must be dealt with on a daily basis (IE6).
Please post questions, this is a VERY general overview but I’d love to go into more detail….