SlideShare ist ein Scribd-Unternehmen logo
1 von 26
CakePHP
CakePHP
• A framework for developing applications in
PHP
• Inspired by Ruby on Rails
• Follows MVC design pattern
• Convention over configuration
– No wheel reinventing required!
MVC
• Model
– Data layer

• View
– Presentation layer

• Controller
– Logic layer
CakePHP Framework
• app/
•
•
•
•
•
•
•
•

config/
controllers/
models/
plugins/
tmp/
vendors/
views/
webroot/

• cake/
• config/
• docs/
• libs/

• vendors/
Naming conventions
• http://book.cakephp.org/view/328/CakeConventions
• Table names: “notes”, “my_notes”
• Model: “mynote.php”->“MyNote”
• Controller: “my_notes_controller.php”->
“MyNotesController”
• Views named after actions, organised in folders
according to the related controller:
– views/my_notes/index.thtml
– views/my_notes/add.thtml
Paths + parameters
• Cake uses url to pass parameters
• Apache mod_rewrite converts url into
scriptname and parameters
• http://www.example.com
/controllername/action/param1/param2/…
• Uses paths to figure out views
• Views stored in “controllername” folder
OOP in PHP
• Limited support in PHP <5
• Much better support in PHP >=5
• Simpler than Java OOP
class SomeClass {
function func() {
….
}
}
SomeClass s = new someClass();
s->func();
Hello world… again
• Remember application is separated into
model / view / controller
• Model:
<?php
/* /app/model/hello.php */
class Hello extends AppModel {
var $name

= 'Hello';

var $useTable = false;
}
?>
Hello world… again
• View:
<!-/* /app/views/index.thtml */
-->
<hr size=1/>
<h1><?php echo $data ?></h1>
<hr size=1/>
• Controller:
<?php
/* app/controller/hello_controller.php */
class HelloController extends AppController {
var $name = "Hello";
var $uses = 'Hello';
function index() {
$data = 'Hello world!';
$this->set('data', $data);
}
}
?>
Simple DB table app
• An online contact list
• We want to add, edit, view and delete
names and phone numbers
• Uses a single table
Model
• Add table to DB:
CREATE TABLE cake_contacts (
id INT UNSIGNED AUTO_INCREMENT
PRIMARY KEY,
name VARCHAR(50),
number VARCHAR(50),
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL
);
Model
• Add a script called contact.php to models/
<?php
class Contact extends AppModel
{
var $name = ‘Contact';
}
?>
View
• views/contacts/index.thtml
<h1>Contact list</h1>
<p>
<?php echo $html->link('Add Contact',
'contacts/add') ?>
</p>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Number</th>
</tr>
View

• views/contacts/index.thtml cntd…

<?php foreach ($contacts as $contact): ?>
<tr>
<td><?php echo $contact['Contact']['id']; ?></td>
<td>
<?php
echo $html->link($contact['Contact'][name'],
"contacts/view/{$contact['Contact']['id']}")?>
[<?php echo $html->link('Edit',
"contacts/edit/{$contact['Contact']['id']}")?>,
<?php echo $html->link('Delete',
"contacts/delete/{$contact['Contact']['id']}",
null, 'Sure?')?>]
</td>
<td><?php echo $contact['Contact']['created']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
View
• views/contacts/view.thtml
<h1><?php echo $data['Contact']
['name']?></h1>
<p><small>
Created: <?php echo $data['Contact']
['created']?>
</small></p>
<p><?php echo $data['Contact']
['number']?></p>
View
• views/contacts/add.thtml
<h1>Add Contact</h1>
<form action="<?php echo $html->url("contacts/add"); ?
>" method="post">
<p>Name:
<?php echo $html->input('Contact/name',
array('size' => '40')) ?>
</p>
<p>Number:
<?php echo $html->input('Contact/number',
array('size' => '40')) ?>
</p>
<p><?php echo $html->submit('Save') ?>
</p>
</form>
View

• views/contacts/edit.thtml
<h1>Edit Contact</h1>
<form action="<?php echo $html->url('/contacts/edit')?
>" method="post">
<?php echo $html->hidden('Contact/id'); ?>
<p>Name:
<?php echo $html->input('Contact/name',
array('size' => '40')) ?>
</p>
<p>Number:
<?php echo $html->input('Contact/number',
array('size' => '40')) ?>
</p>
<p>
<?php echo $html->submit('Save') ?>
</p>
</form>
Controller
• /app/controllers/notes_controller.php:
<?php
class ContactsController extends AppController
{
var $name = 'Contacts';
function index() {
$this->set('contacts', $this->Contact>findAll());
}
function view($id) {
$this->Contact->id = $id;
$this->set('data', $this->Contact->read());
}
Controller
•

/app/controllers/notes_controller.php:
function add() {
if (!empty($this->data['Contact'])) {
if($this->Contact->save($this->data['Contact'])) {
$this->flash('Your contact has been added.',
‘/contacts/');
}
}
}
function delete($id) {
if ($this->Contact->del($id)) {
$this->flash('The contact with id: '.$id.' has been
deleted.', ‘/contacts/');
}
}
Controller
•

/app/controllers/notes_controller.php:
function edit($id = null) {
if (empty($this->data['Contact'])) {
$this->Contact->id = $id;
$this->data = $this->Contact->read();
} else {
if($this->Contact->save($this->data['Contact'])) {
$this->flash('Your contact has been
updated.',‘/contacts/');
}
}
}

}
?>
Resulting application

…../cake/contacts/add

…../cake/contacts/edit/1

…../cake/contacts/view/4
Other benefits
• Bake script – command line script generator
• Uses LAMP common web platform
– (Linux, Apache, MySQL and PHP)

• Helpers for HTML, Forms, Pagination,
AJAX, Javascript, XML, RSS
• Scaffolding (no need for views)
– Create controller with var $scaffold;
Disadvantages
• Mainly due to the limitations of PHP
– Clumsy OOP
– Access data through arrays not classes (which
RoR does) – more code in view

• Create tables in separate SQL
• Not well documented yet

Weitere ähnliche Inhalte

Was ist angesagt?

OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
Yi-Ting Cheng
 
HTML5 - The 2012 of the Web - Adobe MAX
HTML5 - The 2012 of the Web - Adobe MAXHTML5 - The 2012 of the Web - Adobe MAX
HTML5 - The 2012 of the Web - Adobe MAX
Robert Nyman
 
Spca2014 hillier 3rd party_javascript_libraries
Spca2014 hillier 3rd party_javascript_librariesSpca2014 hillier 3rd party_javascript_libraries
Spca2014 hillier 3rd party_javascript_libraries
NCCOMMS
 
PrimeTime JSF with PrimeFaces - Dec 2014
PrimeTime JSF with PrimeFaces - Dec 2014PrimeTime JSF with PrimeFaces - Dec 2014
PrimeTime JSF with PrimeFaces - Dec 2014
cagataycivici
 
Validation using javascripts by karan chanana
Validation using javascripts by karan chananaValidation using javascripts by karan chanana
Validation using javascripts by karan chanana
karan info
 
JSF 2.0 (JavaEE Webinar)
JSF 2.0 (JavaEE Webinar)JSF 2.0 (JavaEE Webinar)
JSF 2.0 (JavaEE Webinar)
Roger Kitain
 

Was ist angesagt? (20)

Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)
 
Django Bogotá. CBV
Django Bogotá. CBVDjango Bogotá. CBV
Django Bogotá. CBV
 
The Chaos Tools Suite
The Chaos Tools SuiteThe Chaos Tools Suite
The Chaos Tools Suite
 
Ch9 .Best Practices for Class-Based Views
Ch9 .Best Practices  for  Class-Based ViewsCh9 .Best Practices  for  Class-Based Views
Ch9 .Best Practices for Class-Based Views
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
 
Building custom APIs
Building custom APIsBuilding custom APIs
Building custom APIs
 
PHP & MVC
PHP & MVCPHP & MVC
PHP & MVC
 
HTML5 - The 2012 of the Web - Adobe MAX
HTML5 - The 2012 of the Web - Adobe MAXHTML5 - The 2012 of the Web - Adobe MAX
HTML5 - The 2012 of the Web - Adobe MAX
 
Spca2014 hillier 3rd party_javascript_libraries
Spca2014 hillier 3rd party_javascript_librariesSpca2014 hillier 3rd party_javascript_libraries
Spca2014 hillier 3rd party_javascript_libraries
 
PrimeTime JSF with PrimeFaces - Dec 2014
PrimeTime JSF with PrimeFaces - Dec 2014PrimeTime JSF with PrimeFaces - Dec 2014
PrimeTime JSF with PrimeFaces - Dec 2014
 
crtical points for customizing Joomla templates
crtical points for customizing Joomla templatescrtical points for customizing Joomla templates
crtical points for customizing Joomla templates
 
Validation using javascripts by karan chanana
Validation using javascripts by karan chananaValidation using javascripts by karan chanana
Validation using javascripts by karan chanana
 
Leveraging the Chaos tool suite for module development
Leveraging the Chaos tool suite  for module developmentLeveraging the Chaos tool suite  for module development
Leveraging the Chaos tool suite for module development
 
In The Brain of Cagatay Civici: Exploring JavaServer Faces 2.0 and PrimeFaces
In The Brain of Cagatay Civici: Exploring JavaServer Faces 2.0 and PrimeFaces In The Brain of Cagatay Civici: Exploring JavaServer Faces 2.0 and PrimeFaces
In The Brain of Cagatay Civici: Exploring JavaServer Faces 2.0 and PrimeFaces
 
RESTful Web Development with CakePHP
RESTful Web Development with CakePHPRESTful Web Development with CakePHP
RESTful Web Development with CakePHP
 
AngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.jsAngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.js
 
Slimme Joomla! Templating Tips en Truuks
Slimme Joomla! Templating Tips en TruuksSlimme Joomla! Templating Tips en Truuks
Slimme Joomla! Templating Tips en Truuks
 
JSF 2.0 (JavaEE Webinar)
JSF 2.0 (JavaEE Webinar)JSF 2.0 (JavaEE Webinar)
JSF 2.0 (JavaEE Webinar)
 
MVC Frameworks for building PHP Web Applications
MVC Frameworks for building PHP Web ApplicationsMVC Frameworks for building PHP Web Applications
MVC Frameworks for building PHP Web Applications
 
Web development today
Web development todayWeb development today
Web development today
 

Andere mochten auch

1377874234 eeeeeeeeeeeeeeeor more file
1377874234 eeeeeeeeeeeeeeeor more file1377874234 eeeeeeeeeeeeeeeor more file
1377874234 eeeeeeeeeeeeeeeor more file
ganeshpatil1989
 

Andere mochten auch (10)

Legal foundarticle
Legal foundarticleLegal foundarticle
Legal foundarticle
 
Class activitywidacella
Class activitywidacellaClass activitywidacella
Class activitywidacella
 
1377874234 eeeeeeeeeeeeeeeor more file
1377874234 eeeeeeeeeeeeeeeor more file1377874234 eeeeeeeeeeeeeeeor more file
1377874234 eeeeeeeeeeeeeeeor more file
 
Storyboard
StoryboardStoryboard
Storyboard
 
Assessment ch2
Assessment ch2Assessment ch2
Assessment ch2
 
Consent decrpowerpoint
Consent decrpowerpointConsent decrpowerpoint
Consent decrpowerpoint
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming Convention
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting Personal
 
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job? Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
 
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
 

Ähnlich wie Lecture n

MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_Hour
Dilip Patel
 
phpWebApp presentation
phpWebApp presentationphpWebApp presentation
phpWebApp presentation
Dashamir Hoxha
 
django_introduction20141030
django_introduction20141030django_introduction20141030
django_introduction20141030
Kevin Wu
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
funkatron
 

Ähnlich wie Lecture n (20)

Synapseindia reviews sharing intro cakephp
Synapseindia reviews sharing intro cakephpSynapseindia reviews sharing intro cakephp
Synapseindia reviews sharing intro cakephp
 
MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_Hour
 
cake phptutorial
cake phptutorialcake phptutorial
cake phptutorial
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial
 
C# Advanced L09-HTML5+ASP
C# Advanced L09-HTML5+ASPC# Advanced L09-HTML5+ASP
C# Advanced L09-HTML5+ASP
 
Ei cakephp
Ei cakephpEi cakephp
Ei cakephp
 
Cakeph pppt
Cakeph ppptCakeph pppt
Cakeph pppt
 
phpWebApp presentation
phpWebApp presentationphpWebApp presentation
phpWebApp presentation
 
django_introduction20141030
django_introduction20141030django_introduction20141030
django_introduction20141030
 
Language Basics | Coldfusion primer | Chap-1
Language Basics | Coldfusion primer | Chap-1Language Basics | Coldfusion primer | Chap-1
Language Basics | Coldfusion primer | Chap-1
 
Building Web Applications with Zend Framework
Building Web Applications with Zend FrameworkBuilding Web Applications with Zend Framework
Building Web Applications with Zend Framework
 
Building Modern Websites with ASP.NET by Rachel Appel
Building Modern Websites with ASP.NET by Rachel AppelBuilding Modern Websites with ASP.NET by Rachel Appel
Building Modern Websites with ASP.NET by Rachel Appel
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
 
ASP.NET MVC introduction
ASP.NET MVC introductionASP.NET MVC introduction
ASP.NET MVC introduction
 
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
 

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Lecture n