CakePHP Introduction
In this article, we’ll see CakePHP Introduction.
Table of Contents
What is CakePHP?
It is an open-source framework for PHP intended to make developing, deploying, and maintaining applications much easier.
CakePHP offers many useful design patterns, such as the Model-View-Controller pattern, seen in other popular frameworks like Ruby On Rails.
The CakePHP framework also provides valuable reusable libraries for dealing with common tasks. An example is “Inflector” (a routine that takes a string and handles pluralization).
Why CakePHP?
A significant amount of development time with PHP is spent rewriting common code for routine operations such as database access or returning data to the browser. Of course, all this routine code can quickly become disorganized in traditional PHP applications. What is needed is a framework for PHP that does what Ruby On Rails did for Ruby.
CakePHP has been around for a while and does exactly that. It provides a number of useful libraries in support of common tasks and includes facilities for organizing code in folders and associating code with files. As a result, time spent writing and organizing code becomes greatly reduced.
Below are just a few things It offers to make development easier.
- Free Open-Source MIT License allows you to use CakePHP applications within any of your own projects.
- Compatibility with both PHP4 and PHP5. The minimum version needed is 4.3.2.
- Support for MySQL, PostgreSQL SQLite, PEAR-DB, and wrappers for ADODB, a database abstraction library.
- Model-View-Controller layout.
- Easy CRUD (Create, Read, Update and Delete) database interaction.
- Scaffolding to save production time.
- Search Engine Friendly URLs.
- Input validation and sanitization tools to make your applications much more secure.
- Templating with familiar PHP syntax.
- Caching Operations.
Understanding the File Structure
Once you have your fresh copy of It out of the oven, the next step is to upload the copy to a PHP and MySQL-enabled webspace. I would recommend creating a new directory for CakePHP projects.
Once the upload has finished the directory structure should look something like this:
/path_to_root_folder
/cake/
/docs/
/app/
config/
controllers/
models/
plugins/
tmp/
vendors/
views/
webroot/
index.php
.htaccess
- The cake folder stores all the core functions and internals for CakePHP. You will usually not need to edit anything here.
- The docs folder contains very little but does hold the license information (COPYING.txt), a change log and some other useful files. This directory is not important for CakePHP to run so you can remove it if you wish.
- The app folder is where your application code will go. The app folder will hold your controllers, configuration, templates and much more.
- The config folder contains all the configuration files for the application. This includes database details, access list, inflections and routes (URL rewriting).
- The models folder stores all the sql database functionality for your application.
- The views folder stores all the templates, layouts (header, footer) and helper modules that assist functionality (such as AJAX).
- The controllers folder stores all the controllers for your application. A controller is the part of the application that directs and controls the model and the views by accepting input and deciding what to do with it.
- The plugins folder stores plugins which are a combination of models, views and controllers that can be packaged and used in other applications. Examples are user management modules or an RSS module.
- The tmp folder stores cache files generated by the caching system and also stores debugging logs. This folder will be very valuable during development.
- The vendors folder, can contain other libraries that you want to include in a particular application.
- The webroot folder stores static media such as CSS, images and the JavaScript needed by your application.
- The second vendors directory will allow you to store third-party libraries and hook into them from your CakePHP controllers. For example if we wanted to built a Facebook application with CakePHP we could drop in the Facebook library and configure CakePHP to load it.
Configuration
Configuring It is pretty straightforward. We just need to tell CakePHP our database details and set up how we want the core functionality to work.
For development purposes you should create a new database and a user with the following privileges: ALTER, CREATE TEMPORARY TABLES, CREATE, DELETE, DROP, SELECT, INSERT, UPDATE, REFERENCES, INDEX, LOCK TABLES.
Once the user and database have been created, we can find CakePHP’s database configuration file, located in /app/config/database.php.default
Open and scroll down to the following array
var
$default
=
array
(
'driver'
=>
'mysql'
,
'connect'
=>
'mysql_connect'
,
'host'
=>
'localhost'
,
'login'
=>
'user'
,
'password'
=>
'password'
,
'database'
=>
'project_name'
,
'prefix'
=>
''
);
and fill in your database details as necessary. If you cannot create a new database, or your host does not allow it, you can set a table prefix for all your CakePHP tables by setting a value in the ‘prefix’ index. Make sure to rename this file to /app/config/database.php
More core configuration is located in /app/config/core.php. You can change the level of debugging information, how sessions are stored, session time-outs for security, and the names of cookies. Once we start developing we may need to adjust these, but the defaults are fine for most needs.
Features
- MVC coding pattern
The Model-View-Controller architecture forms the crux of the CakePHP Framework. This coding pattern helps divide the business logic from design & presentation. The pattern has three layers like Model layer for database application, the View layer for the implementation of the graphical user interface, and the Controller layer for business logic. Every layer can be worked independently of the others. This feature helps to make the whole process faster, smoother and less complex. - Security
When it comes to security features, CakePHP is simply the best. Its core security and CRUD (Create, Retrieve, Update, and Delete) features allow for securing the user submission process in less time. Further, the Object Relational Mapping (ORM) framework allows one streamlining SQL queries by presenting tables as classes. It also has its own shell for command-line interface stuff. - Support for PHP 4 and PHP 5
CakePHP, as a web application framework, now fully supports the latest version of PHP Programming language. Using this, developers can easily build next-generation web applications exactly in the manner their clients want. - Templating System
Another great feature offered by CakePHP is that it is an extremely fast and flexible templating system. You can even design your own custom CakePHP template to improve the front-end looks of web applications. There are professional developers available in the market, who can help you develop the best CakePHP website template in a quick time. - Other features
Besides offering these functionalities, CakePHP also has some other brilliant functionalities discussed below- Improve bootstrapping process to allow more developer control and better performance
- Richer query API
- Support for any database type
- Shorter URL Syntax
- Support for more database drivers both PDO and native
- View Helpers for AJAX, JavaScript, RSS, Pagination, XML, Forms, and many more.
- Smarter router prefixes
Advantages And Disadvantages :
Advantages
- Helps reduce web application development costs & time considerably.
- Cakephp is remarkable when it comes to scaffolding code generation.
- While classes can be challenging to work within standard PHP, they are much easier to work with in Cakephp.
- Its automated configuration process auto-detects preferred settings. What does this imply? This means one need not invest considerable time in configuring Linux-Apache-MySQL-PHP (LAMP) setup.
Disadvantages
- The documentation for CakePHP definitely needs some work.
- While using CakePHP, one needs to update the default routes for creating Fancy URLs. If compared with other frameworks such as Symfony, CakePHP loses the battle in this case.
- Many still believe that CakePHP is easier to learn. But they haven’t come across frameworks such as CodeIgniter which make learning all the easier!
- One-way routing in CakePHP often proves to be a disadvantage when compared with frameworks such as Ruby on Rails.
Hope this article helps!