CakePHP - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

CakePHP

Description:

We want to add, edit, view and delete names and phone numbers. Uses a single table ... number VARCHAR(50), created DATETIME DEFAULT NULL, modified DATETIME ... – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 27
Provided by: sandybr8
Category:

less

Transcript and Presenter's Notes

Title: CakePHP


1
CakePHP
2
CakePHP
  • A framework for developing applications in PHP
  • Inspired by Ruby on Rails
  • Follows MVC design pattern
  • Convention over configuration
  • No wheel reinventing required!

3
MVC
  • Model
  • Data layer
  • View
  • Presentation layer
  • Controller
  • Logic layer

4
CakePHP Framework
  • app/
  • config/
  • controllers/
  • models/
  • plugins/
  • tmp/
  • vendors/
  • views/
  • webroot/
  • cake/
  • config/
  • docs/
  • libs/
  • vendors/

5
Naming conventions
  • http//book.cakephp.org/view/328/Cake-Conventions
  • Table names notes, my_notes
  • Model mynote.php-gtMyNote
  • Controller my_notes_controller.php-gt
    MyNotesController
  • Views named after actions, organised in folders
    according to the related controller
  • views/my_notes/index.thtml
  • views/my_notes/add.thtml

6
Paths parameters
  • Cake uses url to pass parameters
  • Apache mod_rewrite converts url into scriptname
    and parameters
  • http//www.example.com /controllername/action/para
    m1/param2/
  • Uses paths to figure out views
  • Views stored in controllername folder

7
(No Transcript)
8
OOP in PHP
  • Limited support in PHP lt5
  • Much better support in PHP gt5
  • Simpler than Java OOP
  • class SomeClass
  • function func()
  • .
  • SomeClass s new someClass()
  • s-gtfunc()

9
Hello world again
  • Remember application is separated into model /
    view / controller
  • Model

lt?php / /app/model/hello.php / class Hello
extends AppModel var name 'Hello'
var useTable false ?gt
10
Hello world again
  • View

lt!-- / /app/views/index.thtml / --gt lthr
size1/gt lth1gtlt?php echo data ?gtlt/h1gt lthr size1/gt
11
  • Controller

lt?php / app/controller/hello_controller.php
/ class HelloController extends AppController
var name "Hello" var uses 'Hello'
function index() data 'Hello world!'
this-gtset('data', data) ?gt
12
(No Transcript)
13
Simple DB table app
  • An online contact list
  • We want to add, edit, view and delete names and
    phone numbers
  • Uses a single table

14
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
  • )

15
Model
  • Add a script called contact.php to models/
  • lt?php
  • class Contact extends AppModel
  • var name Contact'
  • ?gt

16
View
  • views/contacts/index.thtml
  • lth1gtContact listlt/h1gt
  • ltpgt
  • lt?php echo html-gtlink('Add Contact',
    'contacts/add') ?gt
  • lt/pgt
  • lttablegt
  • lttrgt
  • ltthgtIdlt/thgt
  • ltthgtNamelt/thgt
  • ltthgtNumberlt/thgt
  • lt/trgt

17
View
  • views/contacts/index.thtml cntd
  • lt?php foreach (contacts as contact) ?gt
  • lttrgt
  • lttdgtlt?php echo contact'Contact''id'
    ?gtlt/tdgt
  • lttdgt
  • lt?php
  • echo html-gtlink(contact'Contact'name'
    ,
  • "contacts/view/contact'Contact''id'"
    )?gt
  • lt?php echo html-gtlink('Edit',
  • "contacts/edit/contact'Contact''id'"
    )?gt,
  • lt?php echo html-gtlink('Delete',
  • "contacts/delete/contact'Contact''id'
    ",
  • null, 'Sure?')?gt
  • lt/tdgt
  • lttdgtlt?php echo contact'Contact''created'
    ?gt
  • lt/tdgt
  • lt/trgt
  • lt?php endforeach ?gt
  • lt/tablegt

18
View
  • views/contacts/view.thtml
  • lth1gtlt?php echo data'Contact''name'?gtlt/h1gt
  • ltpgtltsmallgt
  • Created lt?php echo data'Contact''created'?gt
  • lt/smallgtlt/pgt
  • ltpgtlt?php echo data'Contact''number'?gtlt/pgt

19
View
  • views/contacts/add.thtml
  • lth1gtAdd Contactlt/h1gt
  • ltform action"lt?php echo html-gturl("contacts/add"
    ) ?gt" method"post"gt
  • ltpgtName
  • lt?php echo html-gtinput('Contact/name',
    array('size' gt '40')) ?gt
  • lt/pgt
  • ltpgtNumber
  • lt?php echo html-gtinput('Contact/number',
    array('size' gt '40')) ?gt
  • lt/pgt
  • ltpgtlt?php echo html-gtsubmit('Save') ?gt
  • lt/pgt
  • lt/formgt

20
View
  • views/contacts/edit.thtml
  • lth1gtEdit Contactlt/h1gt
  • ltform action"lt?php echo html-gturl('/contacts/edi
    t')?gt" method"post"gt
  • lt?php echo html-gthidden('Contact/id') ?gt
  • ltpgtName
  • lt?php echo html-gtinput('Contact/name',
  • array('size' gt '40')) ?gt
  • lt/pgt
  • ltpgtNumber
  • lt?php echo html-gtinput('Contact/number',
  • array('size' gt '40')) ?gt
  • lt/pgt
  • ltpgt
  • lt?php echo html-gtsubmit('Save') ?gt
  • lt/pgt
  • lt/formgt

21
Controller
  • /app/controllers/notes_controller.php
  • lt?php
  • class ContactsController extends AppController
  • var name 'Contacts'
  • function index()
  • this-gtset('contacts', this-gtContact-gtfindAll
    ())
  • function view(id)
  • this-gtContact-gtid id
  • this-gtset('data', this-gtContact-gtread())

22
Controller
  • /app/controllers/notes_controller.php
  • function add()
  • if (!empty(this-gtdata'Contact'))
  • if(this-gtContact-gtsave(this-gtdata'Contact
    '))
  • this-gtflash('Your contact has been
    added.',
  • /contacts/')
  • function delete(id)
  • if (this-gtContact-gtdel(id))
  • this-gtflash('The contact with id '.id.'
    has been deleted.', /contacts/')

23
Controller
  • /app/controllers/notes_controller.php
  • function edit(id null)
  • if (empty(this-gtdata'Contact'))
  • this-gtContact-gtid id
  • this-gtdata this-gtContact-gtread()
  • else
  • if(this-gtContact-gtsave(this-gtdata'Contact
    '))
  • this-gtflash('Your contact has been
    updated.',/contacts/')
  • ?gt

24
Resulting application
../cake/contacts/view/4
../cake/contacts/edit/1
../cake/contacts/add
25
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

26
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
Write a Comment
User Comments (0)
About PowerShow.com