About CakePHP

What Is CakePHP?

logo

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.

mvc

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 :

screen1

– 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

<span style="font-size:10pt;"> </span>

Add some line of code in this file :

<Directory “C:/AppServ/www”> In from this line search for

<span style="font-size:10pt;color:black;"> </span>

<span style="font-family:&quot;color:black;">AllowOverride None </span>

<span style="font-size:10pt;color:black;"> </span>

<span style="font-family:&quot;color:black;">modify this line to </span>

<span style="font-family:&quot;color:black;"> </span>

<span style="font-family:&quot;color:black;">AllowOverride All</span>

<span style="font-size:10pt;color:black;"> </span>

<span style="font-family:&quot;color:black;">Then restart the apache and run the CakePHP in your local system , it is perfectly run the CakePHP application.</span>

<span style="font-family:&quot;color:black;"> </span>

* List out the errors while we integrating the CakePHP

<span style="font-family:&quot;color:black;"> </span>

<strong><span style="font-family:&quot;color:black;">- The application is run perfectly but css is not applied for this application</span></strong>
<span style="font-family:&quot;color:black;"> .</span>

<span style="font-family:&quot;color:black;"> </span>

<span style="font-family:&quot;color:black;">For this we have check for the mod_rewrite option is enable or not in the phpinfo.php<span> </span>, to search for this </span>

<span style="font-family:&quot;color:black;"><span> </span><a href="http://localhost/phpinfo.php">http://localhost/phpinfo.php</a> </span>

<span style="font-family:&quot;color:black;">In this see the line like mod_rewrite at the load modules section.</span>

<span style="font-family:&quot;color:black;"> </span>

<span style="font-family:&quot;color:black;"> </span>

<span style="font-family:&quot;color:black;"> </span>

<span style="font-family:&quot;color:black;"> </span>

<span style="font-family:&quot;color:black;">Till this we are aware of how to install CakePHP in our system, and how to run application are completed, now going to know and create a sample application using our CakePHP.</span>

<span style="text-decoration:underline;"><span style="font-family:&quot;color:black;"><span style="text-decoration:none;"> </span></span></span>

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 :

http://localhost/cakephp/

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 :

screen2

This is sample code using latest version of using CakePHP.

The manual for CakePHP:

http://book.cakephp.org/

http://groups.google.com/group/cake-php

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

You may also like...

15 Responses

  1. Hi Anil,

    My suggestion write less explain more with images..

    Srinivas Tamada

    9lessons blog author

  2. And change the template I will tell u how to popularize

  3. Anil Kumar says:

    Thanq For your suggestion, from next time i will do like that..

    Thank you

    Anil Kumar

  4. danalakshmi says:

    thanks for this writeup. You put together, in an easy to understand way, what I had been searching. I appreciate it

  5. danalakshmi says:

    danalakshmi :
    Hi,
    thanks for this writeup. You put together, in an easy to understand way, what I had been searching. I appreciate it

  6. danalakshmi says:

    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.

  7. danalakshmi says:

    with example…

  8. Ok thank you for your valuable suggestion, i will try to give the all with examples soon …

  9. ramanan says:

    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…

  10. @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.

  11. Linda Riecke says:

    Thanks so much for good article, I am excited learn more from you.

  12. siddhu says:

    sir thanq , now i understand what is mvc but the content what u wrote is moreeeeee

  13. 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.

  1. November 18, 2011

    […] would like to explain abour basic need of MVC architecture while develop the complex applications. What is MVC is explained in my earlier post. I will explain this approach with small […]

Leave a Reply

Your email address will not be published. Required fields are marked *