Sie sind auf Seite 1von 25

Programming in PHP

Have you Cake?

   
Agenda
 Introduction
 Components needed
 Model­View­Controller
 Cake convention
 Relationship between Models
 Validation of data
 Baking the cake
 Hands on
   Conclusion  
Introduction
 Rapid development framework for PHP
 Using design patterns: MVC and ORM
 Similar to Zend and Ruby on Rails (ROR) 
framework
 Scaffolding support ­ 2 line code for CRUD
 Easier to maintain
 Reduces development costs
 Write less code
 
 Current version (Stable: 1.1.19.6305)
 
Introduction (cont)

 Example of sites done using CakePHP
 http://ping.sg
 http://www.yaledailynews.com
 http://scratch.mit.edu/
 http://store.theonion.com/

   
Components to run Cake

 Database ­ MySQL
 Apache
 PHP

Recommended Installation
 Xampp ­ 10 mins installation plus configuration 
:)

   
Model­view­controller
 In short MVC
 Multi­tier architecture
 Decoupling of program
 Business logic
 Data presentation
 User interaction
 Benefits
 reduce complexity in architectural design
 increase flexibility and reuse.
   
Model­View­Controller (cont)
 Model
 domain­specific representation of the information
 View
 Renders the model into a form suitable for 
interaction
 Controller
 Processes and responds to events

 To make use of this capability, convention 
 
needs to be followed  
Convention ­ Filename
 Controller
 KissesAndHugsController ­> 
kisses_and_hugs_controller.php
 Model
 OptionValue ­> option_value.php   
 Component
 MyHandy ­> my_handy.php
 Helper
 BestHelperEver ­> best_helper_ever.php

   
Convention ­ Model
 Model class names are singular
 Capitalized for single­word models
 Person, Monkey
 UpperCamelCased for multi­word models.
 GlassDoor, LineItem, ReallyNiftyThing
 many­to­many join tables
 tags_users
 Model filenames use a lower­case 
underscored syntax
   person.php, line_item.php, really_nifty_thing.php  
 
Convention ­ Model

 Database tables plural, lower­case 
underscored syntax
 people, monkeys, glass_doors, line_items, 
really_nifty_things

   
Example of model file ­ school.php
<?php
class School extends AppModel {
var $name = 'School';
var $validate = array(
//'id' => VALID_NUMBER,
'schname' => VALID_NOT_EMPTY,
);
}
?>
   
Convention ­ Controller
 Class names are plural
 Capitalized for single­word controllers, and 
UpperCamelCased for multi­word controllers.
 Controller file names use a lower­case 
underscored syntax
 For protected member visibility, controller 
action names should be prepended with '­’.
 For private member visibility, controller action 
names should be prepended with '__'.
   
Example of controller ­ 
schools_controller.php
<?
class SchoolsController extends AppController
{
var $name = 'Schools';
var $scaffold;

function beforeFilter(){
//check if user login and user login type
}
}
 
?>  
Convention (more)

 More convention for cake
 Helpers
 Vendors
 Components

   
Convention ­ Views

 Named after actions they display.
 Name the view file after action name, in 
lowercase.
 You can force an action to render a 
specific view by calling
$this­>render(’create'); at the end of your 
action.

   
Model Relationship

 3 types of relationship between model
 Has One
 Has Many
 Has And Belong To Many (HATBM)

   
Built­in validation for data

 VALID_NOT_EMPTY
 VALID_EMAIL
 VALID_NUMBER
 VALID_YEAR
 Regex allowed

   
Baking cake
 Create class files of model, controller and view 
files effortlessly
 Automate the creation of model class, validation 
for attributes and relationship.
 Able to bake controllers and views.
 Run terminal window to execute bake script.
 Steps
 Go to place where you can to create the application via 
cd command
 /usr/bin/php {cakehome}/scripts/bake.php ­help

   
Setting up cake

 Copy and paste into wwwroot or htdocs 

   
Schools table

CREATE TABLE  `learncake`.`schools` (
   `id` int(11) NOT NULL auto_increment,
   `name` varchar(45) NOT NULL,
   PRIMARY KEY  (`id`)
 ) ENGINE=InnoDB 
AUTO_INCREMENT=4 DEFAULT 
CHARSET=utf8;
   
Students table
CREATE TABLE  `learncake`.`students` (
`id` int(11) NOT NULL auto_increment,
`matric` int(11) NOT NULL,
`nric` int(11) NOT NULL,
`name` varchar(65) NOT NULL,
`yrentered` int(11) NOT NULL,
`password` varchar(20) NOT NULL,
`school_id` int(11) default NULL,
PRIMARY KEY  (`id`),
CONSTRAINT `schoolfk` FOREIGN KEY (`school_id`) 
REFERENCES `schools` (`id`)
ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT 
   
CHARSET=utf8;
Hands On

   
Conclusion

 Easy to use
 Quick implementation
 Quick to deploy
 Free =)
 Once you use it, you want more of it

   
Any Question?

   
Recommended links

 cakephp.org
 cakelive.net
 php.net
 apachefriends.org/en/xampp.html

 Download slides
 plaktoz.com/misc/learncake.zip

   

Das könnte Ihnen auch gefallen