Why CakePHP making the short story long
This story is about you, either now or some time in the past. You have some experience of writing HTML/CSS code and maybe a bit of JavaScript. You've marked up some static web pages and now you want to delve into the world of web application development.
You set forth with an idea for an app, and some time later you've pieced it all together. To your delight, it does exactly what you wanted it to donothing more, nothing less. You release it to the public and, harnessing the momentum and exhilaration, you decide to take a break from e-mails so you can start working on your next project.
As you begin to write your next app, you find yourself saying "Wait a minute, I already wrote a login system..." but when you look at your login code you find that it's so specific to your previous application that you can barely reuse any of it. Writing that system was fun then, but it is tedious now. Now you just want to get to the meat of your app and not worry about any of this!
Having had the wind sucked out of your sails, you decide to get back to your e-mail. Your heart sinks furtherbug reports have started coming in; not just small formatting bugs but "big" bugs involving security. Nobody told you to sanitize your input! And what is a cross-site scripting attack?
You don't have time for this. You want to work on your next app, not spend time fixing this one! You decide to bring another developer on to help, but your code base is so specific to both you and the application that you spend as much time answering his questions and explaining the choices that you made in the code as you would have spent just fixing it yourself.
Finally, the bugs all get fixed, but because there was no testing framework or official definition of the functionality, the bug "fixes" broke other parts of the site, and caused more bugs!
You're not sure what happened, or how this all could have been avoided.
Getting the point?
CakePHP to the rescue!
The benefit of using a framework like CakePHP is that a lot of this work is already done, and a lot of these problems already solved. There are many frameworks like CakePHP, but CakePHP's focus on rapid development and simplicity puts it in a class of it's own. Also, a strong community is important around any technology, and I can say without hesitation or irony that CakePHP's community is unmatched.
Out of the box, Cake provides:
- User session management (via cookies or sessions)
- Request handling (via GET and POST)
- E-mail support
- A well-tested security layer that includes CSRF protection and data sanitation
- Multiple caching mechanisms
- Integrated CRUD (Create, Retrieve, Update, Delete) for your datastore
- Model validation (required fields, valid e-mail, and so on)
- Data sanitation
- Code generation (both PHP and HTML)
- A unit testing framework complete with code coverage support for both your app and the core itself
- A standard coding convention, project structure, and suggested code standard
- A robust view-helper library that handles AJAX, pagination, forms generation, static file inclusion, and more
In addition to that, there is an active community built around the framework to which you can always go for support and inspiration. Check out http://community.cakephp.org.
Model-View-Controller what is that all about?
An entire book could be written on this subject alone, so we'll only broach the subject here.
Model-View-Controller is what is known as a design pattern, and is based around separating components of code based on their responsibilities. Models refer to the data layer, views refer to the presentation layer, and controllers contain your application or business logic.
This concept was invented (among many, many other things) in the 70's at Xerox Parc when they were building the first GUIs. It is one of the (if not the) oldest pattern in GUI programming.
For more information about the MVC design pattern, please see the section titled Top 6 features you need to know about .
Installation
This section is divided into our three main areas of concern: our development environment, our application, and our deployment environment.
We are going to intentionally obfuscate the development environment in these examples, because everybody's development environment is going to be different. Yes, there are typically two or three main environments (PC, Mac, Linux), but everybody's own style and conventions are different. Also, CakePHP is designed to run on any platform, so Windows should be running it just as well as any *nix-based system such as Linux or Mac.
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Step 1 preparing the development environment
Before we begin, let's ensure that a few things are in place: an HTTP server, a database layer, the PHP scripting language, and source control.
As far as our operating system goes, we are going to stay as agnostic as possible. The file paths are Windows-based because we used Windows to write and create the examples in this book, but Unix file paths are perfectly acceptable as well. Do not panic.
The HTTP server (Apache)
The web server software should be set up and running. You should be able to visit http://localhost
or http://127.0.0.1/
in your web browser and see an Apache test page.