Cakephp Tutorial PDF
Cakephp Tutorial PDF
CakePHP is based on an MVC-like architecture that is both powerful and easy to grasp.
Models, Views, and Controllers guarantee a strict but natural separation of business logic
from data and presentation layers.
Audience
This tutorial is meant for web developers and students who would like to learn how to
develop websites using CakePHP. It will provide a good understanding of how to use this
framework.
Prerequisites
Before you proceed with this tutorial, we assume that you have knowledge of HTML, Core
PHP, and Advance PHP. We have used CakePHP version 3.2.7 in all the examples.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com
i
CakePHP
Table of Contents
About the Tutorial .................................................................................................................................. i
Audience ................................................................................................................................................ i
Prerequisites .......................................................................................................................................... i
6. CAKEPHP — ROUTING...................................................................................................... 11
Connecting Routes............................................................................................................................... 11
AppController ...................................................................................................................................... 20
Controller Actions................................................................................................................................ 21
ii
CakePHP
Redirecting .......................................................................................................................................... 21
Authentication .................................................................................................................................... 47
Email ................................................................................................................................................... 68
iii
CakePHP
Write Cookie........................................................................................................................................ 80
CSRF .................................................................................................................................................... 86
iv
1. CakePHP — Overview CakePHP
CakePHP is an open source MVC framework. It makes developing, deploying and maintaining
applications much easier. CakePHP has number of libraries to reduce the overload of most
common tasks. Following are the advantages of using CakePHP.
Open Source
MVC Framework
Templating Engine
Caching Operations
Built-in Validation
Localization
5
CakePHP
A typical CakePHP request cycle starts with a user requesting a page or resource in your
application. At a high level, each request goes through the following steps:
Any dispatch filters that are configured can handle the request, and optionally
generate a response.
The dispatcher selects the appropriate controller & action based on routing rules.
The controller’s action is called and the controller interacts with the required Models
and Components.
The controller delegates response creation to the View to generate the output
resulting from the model data.
The view uses Helpers and Cells to generate the response body and headers.
6
2. CakePHP – Installation CakePHP
Installing CakePHP is simple and easy. You can install it from composer or you can download
it from github — https://github.com/cakephp/cakephp/releases. We will further understand
how to install CakePHP in WampServer. After downloading it from github, extract all the files
in a folder called “CakePHP” in WampServer. You can give custom name to folder but we have
used “CakePHP”.
Make sure that the directories logs, tmp and all its subdirectories have write permission
as CakePHP uses these directories for various operations.
After extracting it, let’s check whether it has been installed correctly or not by visiting the
following URL in browser: http://localhost:85/CakePHP/
The above URL will direct you to the screen as shown below. This shows that CakePHP has
successfully been installed.
7
3. CakePHP — Folder Structure CakePHP
Take a look at the following screenshot. It shows the folder structure of CakePHP.
Folder
Description
Name
bin The bin folder holds the Cake console executables.
The config folder holds the (few) configuration files CakePHP uses.
config Database connection details, bootstrapping, core configuration files and
more should be stored here.
The logs folder normally contains your log files, depending on your log
logs
configuration.
plugins The plugins folder is where the Plugins your application uses are stored.
The src folder will be where you work your magic: It is where your
application’s files will be placed. CakePHP’s src folder is where you will do
most of your application development. Let’s look a little closer at the
folders inside src.
Console Contains the console commands and console tasks for your
src
application.
8
CakePHP
9
4. CakePHP – Configuration CakePHP
CakePHP comes with one configuration file by default and we can modify it according to our
needs. There is one dedicated folder “config” for this purpose. CakePHP comes with different
configuration options.
General Configuration
The following table describes the role of various variables and how they affect your CakePHP
application.
10
CakePHP
Databases Configuration
Database can be configured in config/app.php file. This file contains a default connection
with provided parameters which can be modified as per our choice. The below screenshot
shows the default parameters and values which should be modified as per the requirement.
Key Description
The fully namespaced class name of the class that represents the
connection to a database server. This class is responsible for
className
loading the database driver, providing SQL transaction
mechanisms and preparing SQL statements among other things.
The class name of the driver used to implements all specificities
for a database engine. This can either be a short classname using
driver plugin syntax, a fully namespaced name, or a constructed driver
instance. Examples of short classnames are Mysql, Sqlite,
Postgres, and Sqlserver.
persistent Whether or not to use a persistent connection to the database.
host The database server’s hostname (or IP address).
username Database username
password Database password
11
CakePHP
12
5. CakePHP – Email Configuration CakePHP
You can add custom transports (or override existing transports) by adding the appropriate
file to src/Mailer/Transport. Transports should be named YourTransport.php, where
'Your' is the name of the transport. Following is the example of Email configuration transport.
Example
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
],
13
CakePHP
Example
'Email' => [
'default' => [
'transport' => 'default',
'from' => 'you@localhost',
],
],
14
CakePHP
15