Wednesday, November 11, 2009

Learn about Basic Principles of CakePHP - MVC Framework

Now that we have done some works with CakePHP this is right time to understand basic CakePHP principles. Let's take a look at the CakePHP folder structure.

These are the files and folders you should see soon after installation:
app (The folder where you will save your application files - like PHP files, CSS files Javascript files etc)
cake (The folder which contains core CakePHP files. You need to do nothing at this location)
vendors (The folder where you will install third party applications.)
.htaccess
index.php
README


Now take a close look at the folders under 'app' folder.

config : Holds the configuration files - like core.php, database.php, bootstrap.php, etc. 
You have already made changes in core.php and database.php files (if you are following my tutorial). So, you may have some idea about what they do.
models : Contains your application’s models, behaviors, and datasources.
As such CakePHP follows MVC (Model-View-Controller) framework. The Model represents the application data.  Simply, you will need to create a class for your database entities (tables). Using cake, you need not be a brain-scientist to define a class for your database table(s). As such there are different methods you can use to create a workable class on the fly. Believe me - it is amazingly easy. In the next step I'll try to create my own model class for a database table. The said class definition file will also contain field level validations and relationship between different database tables. You just need to follow Cake's convention to make things really easy for you.
controllers : Contains your application’s controllers and their components.
As per MVC framework, the Controller handles and routes requests made by the client. This is the place to save your application logics in the form of ControllerClasses. Like - you can set how to handle user login/logout requests, or, to add a new entry to your database tables. Once again, if you follow CakePHP convention - it is just a matter of few minutes to get these tasks done. 
locale : Stores string files for internationalization.
plugins : Contains plugin packages.
tmp : This is where CakePHP stores temporary data. It must be writable.
vendors : Any third-party classes or libraries should be placed here. 
views : Presentational files are placed here: like views, error pages, layouts etc.
As per MVC framework, the View renders a presentation of model data. Say, you have created a table for your blog post. To display your blog posts, you need to create a file under views. Once again, Cake has set its own conventions which you should follow to create view files. In my actual example - I'll show you how to do or what to do.


webroot : This is the folder to save your CSS stylesheets, images, and JavaScript files.
You may recall the file cake.generic.css resides inside this folder as 'webroot/css/cake.generic.css'. 

A quick summary
In a nut-shell, 
1. Create a database table.
2. Create a new file (model class file) for that table under models folder. (Cake now knows about your database table.)  
3. Create another file (controller class file) under 'controllers' folder to define how you want the various queries to be processed.
4. Create one or more files (views files) under 'views' folder to present the query results for different requests.


Once again, everything will work almost automatically if you follow Cake's Convention.   


Finally, if you follow convention - you can easily take full advantage of CakePHP MVC framework. As such, most coding will be done automatically for you!
So, in my next example - I'll try to present a real example while clearly spelling out basic CakePHP conventions.
(This may be criticized as an OVER simplified representation of CakePHP's basic principle. But I prefer to go with the minimum knowing well there are lot more stuff I preferred not to mention at this point... If you differ, you can always refer to any better authenticated source. Thanks.)

Follow my next post to go through a practical example.

Cheers!

No comments: