SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Destacado

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

Destacado (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
 

Similar a 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
 

Similar a 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)
 

Último

Último (20)

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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
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
 

Lecture n