What Is CakePHP?
The Official website: http://www.cakephp.org/
CakePHP is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications. 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.
List of features you’ll enjoy when using CakePHP:
- Active, friendly community
- Flexible licensing
- Compatible with versions 4 and 5 of PHP
- Integrated CRUD for database interaction
- Application scaffolding
- Code generation
- MVC architecture
- Request dispatcher with clean, custom URLs and routes
- Built-in validation
- Fast and flexible templating (PHP syntax, with helpers)
- View Helpers for AJAX, JavaScript, HTML Forms and more
- Email, Cookie, Security, Session, and Request Handling Components
- Flexible ACL
- Data Sanitization
- Flexible Caching
- Localization
- Works from any web site directory, with little to no Apache configuration involved
What Is MVC?
MVC stands for Model, View, and Controller. It aims to separate your app in these 3 components:
* The Model: this is your data (and some code to easily manipulate it).
* The View : this is the way you represent you data.
* The Controller : this is the data processing, the glue between the Model and the View.
It’s a very nice way to build application because it’s clear and easy to maintain. It allows you to reuse a lot of code or to change your input source (if your data base change, only your model change, the rest of your application works the same !)
Designers can work on the views without needing to know how your app is built.
AJAX apps fit better because their are few dependencies between you app components.
Input –> Processing –> Output
Controller –> Model –> View
– The Model represents enterprise data and the business rules that govern updates and access to this data. The Model also encapsulates application state, responds to state queries and notifies views of changes.
– The View renders the contents of a model and also requests updates of the models. Another feature is that it can access enterprise data through the model and specifies how that data should be presented.
– The Controller processes and responds to events, typically user actions and may invoke changes on the model.
The following are the list of benefits and drawbacks in MVC architecture.
Benefits of MVC
– Substitutable user interface :
Different views and controllers can be substituted to provide alternate user interfaces for the same model.
– User interface components:
Because MVC demands that the user interface of an application be structured into a hierarchy of objects and defines a standard relationship between these objects, generic versions of these objects are possible. They are usually called user interface components and no modern GUI environment is without a full complement of them usually combining view and controller into a single object.
– Multiple simultaneous views of the same model:
Multiple different views can be active at the same time. Each view simultaneously and independently presents the same information from the model.
– Synchronized views:
The change propagation mechanism insures that all views simultaneously reflect the current state of the model.
– Easier user interface changes:
Changes affecting just the user interface of the application logic become easier to make.
– Easier testing :
With MVC it can be easier to test the core of the application, as encapsulated by the model.
Drawbacks of MVC
– Increased complexity :
If reading this page doesn’t convince of you the complexity of this pattern, consider all of the auxiliary patterns that co-occur with MVC.
– Close coupling of views and controllers to model :
Changes to the model interface require parallel changes in the view and may require additional changes to the controller. certain code changes become more difficult.
– Potential for excessive updates :
The change propagation mechanism can be inefficient when frequent model changes require many change notifications. This is generally not as much of a problem if a passive model is used.
– Close coupling between view and controller:
strict separation is difficult if not impossible.
Till this we are aware of
– What is CakePHP?
– What is MVC?
– The benefits and drawbacks of using MVC architecture?
– The key features of using CakePHP.
Now we are going to
– From where to download the CakePHP files?
– How to install in our system?
– How to run CakePHP application?
– What are modifications require in apache server to run the CakePHP in localhost?
– List out the errors while we integrating the CakePHP.
* From where to download the CakePHP files?
From the URL: http://www.cakephp.org ,download the latest version of CakePHP and unzip in your system, at the appserv->www folder and the folders and file are
App, cake, vendors, one .htaccess file and index.php file.
* How to install in our system?
Now CakePHP files are in your local host folder, at the appserv – www folder .
Create a database for your application.
* How to run CakePHP application?
Run the CakePHP application in your localhost like :
Run URL at the web browser like : http://localhost/CakePHP/
The following screen will be appearing :
– Open the file core.php which is at app/config/ folder and modify the line
Search for this line :
Configure::write(‘Security.salt’,’DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi’);
Modify the line to :
Configure::write(‘Security.salt’, ”);
Then salt error is removed when CakePHP is running.
To enable database configuration like :
Rename config/database.php.default to config/database.php
And do the changes like :
Var $default = array (
‘Driver’ -> ‘mysql’,
‘persistent’ => false,
‘host’ => ‘localhost’, // data base host name
‘login’ => ‘root’, // username for the data base
‘password’ => ‘root’, // password of data base
‘database’ => ‘CakePHP’, // Data base name
‘prefix’ => ”,
);
* What are modifications require in apache server to run the CakePHP in localhost?
Uncomment the line in httpd.conf to run the CakePHP
LoadModule rewrite_module modules/mod_rewrite.so
Add some line of code in this file :
<Directory “C:/AppServ/www”> In from this line search for
* List out the errors while we integrating the CakePHP
The Sample Application Using CakePHP.
– What is scaffolding ?
Application scaffolding is a technique that allows a developer to define and create a basic application that can create, retrieve, update and delete objects. Scaffolding in CakePHP also allows developers to define how objects are related to each other, and to create and break those links.
CakePHP scaffolding is pretty cool. It allows you to get a basic CRUD application up and going in minutes. So cool that you’ll want to use it in production apps. Now, we think its cool too, but please realize that scaffolding is… well… just scaffolding. It’s a loose structure you throw up real quick during the beginning of a project in order to get started. It isn’t meant to be completely flexible, it’s meant as a temporary way to get up and going. If you find yourself really wanting to customize your logic and your views, its time to pull your scaffolding down in order to write some code. CakePHP’s Bake console, covered in the next section, is a great next step: it generates all the code that would produce the same result as the most current scaffold.
To add scaffolding to your application, in the controller, add the $scaffold variable:
<?php
class CategoriesController extends AppController {
var $scaffold;
}
?>
But when we are using scaffold like this we loose the structure of controller, it is difficult to modification or customization of controller.
At in the model :
<?php class User extends AppModel { var $name = 'User'; var $displayField = 'first_name'; } ?>
Run scaffolding of CakePHP in the console:
For The new version of CakePHP :
Command :
Set path like at the path section : c:\appserv\www\cakephp\console\
And run the commands follow this order
– C:\AppServ\www\cakephp\app>cake bake model
– C:\AppServ\www\cakephp\app>cake bake controller
– C:\AppServ\www\cakephp\app>cake bake view
At that time we run the application like :
create a sample table like
CREATE TABLE `users` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 255 ) NOT NULL
)
Use this table for scaffolding.
Then run that file browser like
http://localhost/cakephp/users/
This is latest stable version of CakePHP.
The screenshot like this :
This is sample code using latest version of using CakePHP.
The manual for CakePHP:
– http://groups.google.com/group/cake-php
Hi Anil,
My suggestion write less explain more with images..
Srinivas Tamada
9lessons blog author
And change the template I will tell u how to popularize
Thanq For your suggestion, from next time i will do like that..
Thank you
Anil Kumar
thanks for this writeup. You put together, in an easy to understand way, what I had been searching. I appreciate it
Thank you.
Hi,
Can you explain model associations (hasMany, hasOne, belongsTo, etc.) in easy way? If you will do this means it will be very helpful for all CakePHP beginner.
with example…
Ok thank you for your valuable suggestion, i will try to give the all with examples soon …
Hai, i would save the images on mysql, but i don’t know , how to retrieve an image from mysql and view on page…If any one know send the code on my Mail-id
mail id ([email protected])
thank U…
@ramanan
Its better to store images into folders and save the path of that image into database. There is lot of drawback storing images in the mysql. Hope you understand.
Pingback : Understanding the basic need of MVC architecture - Anil Labs
Thanks so much for good article, I am excited learn more from you.
sir thanq , now i understand what is mvc but the content what u wrote is moreeeeee
Heya i’m for the first time here. I came across this board and I find It really useful & it helped
me out a lot. I hope to give something back and aid others like you aided me.