SlideShare a Scribd company logo
Cake PHP
Version : 3.4 (X)
By : Jyotisankar Pradhan
Requirements
1. PHP 5.6.0 or Greater
2. Webserver (Apache, Nginx) preferably or Microsoft IIS
3. MySQL (5.1.10 or greater), PgSQL, Microsoft SQL Server, SQLite 3
4. PDO is default required.
Installation
1. Install composer
2. Once the installation is done you need to move your composer.phar file to
/usr/local/bin/composer
3. You can also install cakePHP using Oven
4. Oven is a simple PHP script which checks necessary system requirements
and will help install CakePHP with click.. Click.. Click… Please refer
https://github.com/CakeDC/oven to install it.
Create a CakePHP Project
=> If your composer has been installed globally on your server or local machine
please use “composer self-update && composer create-project --prefer-dist
cakephp/app my_app_name” else use “php composer.phar create-project --
prefer-dist cakephp/app my_app_name”
=> my_app_name is the name of the directory/project.
=> Now if you will run localhsot/my_app_name you can see the default
application running on your local machine/server.
Few other things to note
1. Make sure mode_rewrite is enabled
2. If you wish to keep your codebase up to date with latest CakePHP changes,
you need to change the CakePHP version in composer.json. Like wise
“"cakephp/cakephp": "3.4.*" to “"cakephp/cakephp": "3.x.*", so that when
you update your composer by composer update you will get the latest fixes
on your local machine/server.
3. You need to have/install correct PDO extension for database that you are
using.
Database Configuration
=> Go to config/app.php, You can set the host, database, username and
password. The actual connection information is fed in
“CakeDatasourceConnectionManager ”.
=> You can also define as many connection as you want using
“CakeDatasourceConnectionManager”.
=> In this connection manager you can also set timezone, connection type
wheather to keep it persistent or not.
Example
'Datasources' => [
'default' => [
'className' => 'CakeDatabaseConnection',
'driver' => 'CakeDatabaseDriverMysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'my_app',
'password' => 'sekret',
'database' => 'my_app',
Manage Connections
1. Using CakeDatasourceConnectionManager, you can access the
connection available in your application
2. You can get the connection using
“CakeDatasourceConnectionManager::get($name)”. $name can be
default or any other cases that you have declared.
3. You can also create new connection at run time using config() and get()
methods.
4. “ConnectionManager::config('my_connection', $config);
Simple CRUD with and without Bootstrap
1. Create Controller with proper naming convention.(XxxsController.php)
2. Create a Model File (XxxTable.php)
3. Create template file (CTP) inside Template (Xxxs/index.ctp)
4. Change default page in routes.php
5. Using Bootstrap, for sample lets see (http://bootswatch.com)
6. Lets check……..
Middleware
CakePHP provides several middleware few of them are :
1. CakeErrorMiddlewareErrorHandlerMiddleware (
=> It traps exceptions from the wrapped middleware and renders an error
page using the Error & Exception Handling Exception handler.
1. CakeRoutingAssetMiddleware
=> It checks the request are are for cake theme or from plugin assets,
wheateher its on themes webroot or in plugins webroot
Middleware
3. CakeI18nMiddlewareLocaleSelectorMiddleware
=> It automatically switch language from the Accept-Language header sent by
the browsers
Deployment
1. To implement the project in 2 or 3 different server, you have to send the
codebase through git and rest thing can be done by composer (Composer
install or Composer Update). It automatically check the dependencies and
folder permission etc.
2. If anyone working on FTP, he has to upload the library manually and have
to give the folder permission as expected.
3. You can set the debug mode True/False based on server to server by
environment variable in your apache settings.
Deployment
SetEnv CAKEPHP_DEBUG 1 (In your apache configuration)
And then you can set the debug level dynamically in app.php:
$debug = (bool)getenv('CAKEPHP_DEBUG');
return [
'debug' => $debug,
];
Deployment
4. To increase your application performance in production once deployment is
done through
php composer.phar dumpautoload -o
After doing this you need set your js/css/images etc path (webroot) through
symlink
bin/cake plugin assets symlink (This command will symlink your webroot
directory)
Cache
1. Default CakePHP cache flused once in a year, but you can manage it
through config/app.php
2. We can also set the cache for schema description and table listings.
3. We can also flush individual cache if we know the key
Cache::delete() will allow you to completely remove a cached object
Cache::delete('my_key');
Cache
4. If you wish to clear all cache data that may be configured in APC, Memcache
etc
Cache::clear(true); (Will only clear expired keys.)
Cache::clear(false); (Will clear all keys.)
5. We can also use Cache to store/remove common find data. Example :
Cache
public function postSave($event, $entity, $options = [])
{
if ($entity->isNew()) {
Cache::clearGroup(post, 'home');
}
}
Cache
6. Enable or Disable Cache globally
Cache::disable(); (Disable all cache reads, and cache writes.)
Cache::enable(); (Re-enable all cache reads, and cache writes.)
Other Important thing
1. You can create your own data type in CakePHP using toPHP, toDatabase,
toStatement and marshal, once you are done you can map your Custom
data type to SQL expression
(https://book.cakephp.org/3.0/en/orm/database-
basics.html#CakeDatasourceConnectionManager)
2. In CakePHP DRY(Don’t repeat yourself) is used to allow you to code things
once and re-use them across your application.
3. CDN is default configured you can define the path of cdn in configuration
Other Important thing
4. How to write custom sql query
use CakeDatasourceConnectionManager;
$connection = ConnectionManager::get('default');
$results = $connection
->execute('SELECT * FROM articles WHERE id = :id', ['id' => 1])
->fetchAll('assoc');
Other Important thing
$connection->insert('articles', [
'title' => 'A New Article',
'created' => new DateTime('now')
], ['created' => 'datetime']);
5. Create Relationship between Tables
Relationship Association Type Example
one to one hasOne A user has one profile.
one to many hasMany A user can have multiple recipes.
many to one belongsTo Many recipes belong to a user.
many to many hasAndBelongsToMany Recipes have, and belong to, many
ingredients.
Example :
class IngredientsTable extends Table
{
public function initialize(array $config)
{
// have additional data on your IngredientsProducts table
$this->belongsToMany('Products', [
class ProductsTable extends Table
{
public function initialize(array $config)
{
$this->belongsToMany('Ingredients', [
'through' => 'IngredientsProducts',
]);
}
class IngredientsProductsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Ingredients');
$this->belongsTo('Products');
}
}
Migration
Laravel VS Cakephp
Migration needs to be enabled through composer, by default its enabled
through the setup
Else
Plugin::load('Migrations'); in your bootsrap.php file
Once you have created in the config/Migrations folder, you will be able to
execute the following migrations command to create the table in your database:
bin/cake migrations migrate

More Related Content

PDF
Cloud Automation with Opscode Chef
PPTX
Caching & validating
PDF
Google App Engine
PDF
backend
ODP
Database Connection With Mysql
PPTX
Compress and decompress
PPTX
Ex-8-hive.pptx
ODP
Running ms sql stored procedures in mule
Cloud Automation with Opscode Chef
Caching & validating
Google App Engine
backend
Database Connection With Mysql
Compress and decompress
Ex-8-hive.pptx
Running ms sql stored procedures in mule

What's hot (20)

PDF
Mule caching strategy with redis cache
PDF
Apache Web server Complete Guide
PDF
Build Automation 101
PPT
Architecting cloud
PDF
MuleSoft ESB Message Enricher
PDF
Fixing Growing Pains With Puppet Data Patterns
PPT
Drupal Performance - SerBenfiquista.com Case Study
PDF
Using RequireJS with CakePHP
DOC
How to migrate Cakephp 1.x to 2.x
ODP
Box connector Mule ESB Integration
PDF
Apache Web Services
PPT
MYSQL - PHP Database Connectivity
PDF
DOC
Apache hadoop 2_installation
PDF
Automated Java Deployments With Rpm
PPT
Programming Server side with Sevlet
PPTX
Cake PHP 3 Presentaion
PPT
Heavy Web Optimization: Backend
PDF
Ansible : what's ansible & use case by REX
PPTX
Integrate with database by groovy
Mule caching strategy with redis cache
Apache Web server Complete Guide
Build Automation 101
Architecting cloud
MuleSoft ESB Message Enricher
Fixing Growing Pains With Puppet Data Patterns
Drupal Performance - SerBenfiquista.com Case Study
Using RequireJS with CakePHP
How to migrate Cakephp 1.x to 2.x
Box connector Mule ESB Integration
Apache Web Services
MYSQL - PHP Database Connectivity
Apache hadoop 2_installation
Automated Java Deployments With Rpm
Programming Server side with Sevlet
Cake PHP 3 Presentaion
Heavy Web Optimization: Backend
Ansible : what's ansible & use case by REX
Integrate with database by groovy
Ad

Similar to Cake php (20)

PPT
Synapseindia reviews sharing intro cakephp
PDF
CakePHP
PDF
Manual 5
PPTX
Ei cakephp
PPTX
Cakeph pppt
PDF
Ch ch-changes cake php2
PDF
5503 cake php-tutorial-no-1-from-ibm
PDF
Introduction to CakePHP
PDF
Introduction to CakePHP
PPT
Intro to CakePHP 1.3
PDF
Intro to CakePHP
PDF
An Introduction to CakePHP
PDF
4 introduction-php-mvc-cakephp-m4-controllers-slides
PPSX
Baking With Cake Php
PDF
CakePHP 3.0: Embracing the future
PDF
Cakephp's Cache
 
KEY
CakePHP 2.0 - PHP Matsuri 2011
PPTX
PDF
2 introduction-php-mvc-cakephp-m2-installation-slides
PPT
Lecture n
Synapseindia reviews sharing intro cakephp
CakePHP
Manual 5
Ei cakephp
Cakeph pppt
Ch ch-changes cake php2
5503 cake php-tutorial-no-1-from-ibm
Introduction to CakePHP
Introduction to CakePHP
Intro to CakePHP 1.3
Intro to CakePHP
An Introduction to CakePHP
4 introduction-php-mvc-cakephp-m4-controllers-slides
Baking With Cake Php
CakePHP 3.0: Embracing the future
Cakephp's Cache
 
CakePHP 2.0 - PHP Matsuri 2011
2 introduction-php-mvc-cakephp-m2-installation-slides
Lecture n
Ad

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Approach and Philosophy of On baking technology
PDF
KodekX | Application Modernization Development
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
MIND Revenue Release Quarter 2 2025 Press Release
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Electronic commerce courselecture one. Pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
NewMind AI Weekly Chronicles - August'25 Week I
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
20250228 LYD VKU AI Blended-Learning.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation_ Review paper, used for researhc scholars
Dropbox Q2 2025 Financial Results & Investor Presentation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Approach and Philosophy of On baking technology
KodekX | Application Modernization Development
Chapter 3 Spatial Domain Image Processing.pdf
Machine learning based COVID-19 study performance prediction
Programs and apps: productivity, graphics, security and other tools
Building Integrated photovoltaic BIPV_UPV.pdf

Cake php

  • 1. Cake PHP Version : 3.4 (X) By : Jyotisankar Pradhan
  • 2. Requirements 1. PHP 5.6.0 or Greater 2. Webserver (Apache, Nginx) preferably or Microsoft IIS 3. MySQL (5.1.10 or greater), PgSQL, Microsoft SQL Server, SQLite 3 4. PDO is default required.
  • 3. Installation 1. Install composer 2. Once the installation is done you need to move your composer.phar file to /usr/local/bin/composer 3. You can also install cakePHP using Oven 4. Oven is a simple PHP script which checks necessary system requirements and will help install CakePHP with click.. Click.. Click… Please refer https://github.com/CakeDC/oven to install it.
  • 4. Create a CakePHP Project => If your composer has been installed globally on your server or local machine please use “composer self-update && composer create-project --prefer-dist cakephp/app my_app_name” else use “php composer.phar create-project -- prefer-dist cakephp/app my_app_name” => my_app_name is the name of the directory/project. => Now if you will run localhsot/my_app_name you can see the default application running on your local machine/server.
  • 5. Few other things to note 1. Make sure mode_rewrite is enabled 2. If you wish to keep your codebase up to date with latest CakePHP changes, you need to change the CakePHP version in composer.json. Like wise “"cakephp/cakephp": "3.4.*" to “"cakephp/cakephp": "3.x.*", so that when you update your composer by composer update you will get the latest fixes on your local machine/server. 3. You need to have/install correct PDO extension for database that you are using.
  • 6. Database Configuration => Go to config/app.php, You can set the host, database, username and password. The actual connection information is fed in “CakeDatasourceConnectionManager ”. => You can also define as many connection as you want using “CakeDatasourceConnectionManager”. => In this connection manager you can also set timezone, connection type wheather to keep it persistent or not.
  • 7. Example 'Datasources' => [ 'default' => [ 'className' => 'CakeDatabaseConnection', 'driver' => 'CakeDatabaseDriverMysql', 'persistent' => false, 'host' => 'localhost', 'username' => 'my_app', 'password' => 'sekret', 'database' => 'my_app',
  • 8. Manage Connections 1. Using CakeDatasourceConnectionManager, you can access the connection available in your application 2. You can get the connection using “CakeDatasourceConnectionManager::get($name)”. $name can be default or any other cases that you have declared. 3. You can also create new connection at run time using config() and get() methods. 4. “ConnectionManager::config('my_connection', $config);
  • 9. Simple CRUD with and without Bootstrap 1. Create Controller with proper naming convention.(XxxsController.php) 2. Create a Model File (XxxTable.php) 3. Create template file (CTP) inside Template (Xxxs/index.ctp) 4. Change default page in routes.php 5. Using Bootstrap, for sample lets see (http://bootswatch.com) 6. Lets check……..
  • 10. Middleware CakePHP provides several middleware few of them are : 1. CakeErrorMiddlewareErrorHandlerMiddleware ( => It traps exceptions from the wrapped middleware and renders an error page using the Error & Exception Handling Exception handler. 1. CakeRoutingAssetMiddleware => It checks the request are are for cake theme or from plugin assets, wheateher its on themes webroot or in plugins webroot
  • 11. Middleware 3. CakeI18nMiddlewareLocaleSelectorMiddleware => It automatically switch language from the Accept-Language header sent by the browsers
  • 12. Deployment 1. To implement the project in 2 or 3 different server, you have to send the codebase through git and rest thing can be done by composer (Composer install or Composer Update). It automatically check the dependencies and folder permission etc. 2. If anyone working on FTP, he has to upload the library manually and have to give the folder permission as expected. 3. You can set the debug mode True/False based on server to server by environment variable in your apache settings.
  • 13. Deployment SetEnv CAKEPHP_DEBUG 1 (In your apache configuration) And then you can set the debug level dynamically in app.php: $debug = (bool)getenv('CAKEPHP_DEBUG'); return [ 'debug' => $debug, ];
  • 14. Deployment 4. To increase your application performance in production once deployment is done through php composer.phar dumpautoload -o After doing this you need set your js/css/images etc path (webroot) through symlink bin/cake plugin assets symlink (This command will symlink your webroot directory)
  • 15. Cache 1. Default CakePHP cache flused once in a year, but you can manage it through config/app.php 2. We can also set the cache for schema description and table listings. 3. We can also flush individual cache if we know the key Cache::delete() will allow you to completely remove a cached object Cache::delete('my_key');
  • 16. Cache 4. If you wish to clear all cache data that may be configured in APC, Memcache etc Cache::clear(true); (Will only clear expired keys.) Cache::clear(false); (Will clear all keys.) 5. We can also use Cache to store/remove common find data. Example :
  • 17. Cache public function postSave($event, $entity, $options = []) { if ($entity->isNew()) { Cache::clearGroup(post, 'home'); } }
  • 18. Cache 6. Enable or Disable Cache globally Cache::disable(); (Disable all cache reads, and cache writes.) Cache::enable(); (Re-enable all cache reads, and cache writes.)
  • 19. Other Important thing 1. You can create your own data type in CakePHP using toPHP, toDatabase, toStatement and marshal, once you are done you can map your Custom data type to SQL expression (https://book.cakephp.org/3.0/en/orm/database- basics.html#CakeDatasourceConnectionManager) 2. In CakePHP DRY(Don’t repeat yourself) is used to allow you to code things once and re-use them across your application. 3. CDN is default configured you can define the path of cdn in configuration
  • 20. Other Important thing 4. How to write custom sql query use CakeDatasourceConnectionManager; $connection = ConnectionManager::get('default'); $results = $connection ->execute('SELECT * FROM articles WHERE id = :id', ['id' => 1]) ->fetchAll('assoc');
  • 21. Other Important thing $connection->insert('articles', [ 'title' => 'A New Article', 'created' => new DateTime('now') ], ['created' => 'datetime']);
  • 22. 5. Create Relationship between Tables Relationship Association Type Example one to one hasOne A user has one profile. one to many hasMany A user can have multiple recipes. many to one belongsTo Many recipes belong to a user. many to many hasAndBelongsToMany Recipes have, and belong to, many ingredients.
  • 23. Example : class IngredientsTable extends Table { public function initialize(array $config) { // have additional data on your IngredientsProducts table $this->belongsToMany('Products', [
  • 24. class ProductsTable extends Table { public function initialize(array $config) { $this->belongsToMany('Ingredients', [ 'through' => 'IngredientsProducts', ]); }
  • 25. class IngredientsProductsTable extends Table { public function initialize(array $config) { $this->belongsTo('Ingredients'); $this->belongsTo('Products'); } }
  • 26. Migration Laravel VS Cakephp Migration needs to be enabled through composer, by default its enabled through the setup Else Plugin::load('Migrations'); in your bootsrap.php file
  • 27. Once you have created in the config/Migrations folder, you will be able to execute the following migrations command to create the table in your database: bin/cake migrations migrate