Upload
newbu
View
636
Download
0
Embed Size (px)
Citation preview
Your first bite.
Me
• Quick Solutions, Inc.• Microsoft/.NET Developer by day• PHP Developer “on the weekends”• twitter.com/mgroves• mgroves.com• [email protected]
Why PHP?
• Ubiquitous• Open source• 3rd most popular language in the world*• Gentle learning curve (easy to learn)• Cross-platform
*TIOBE http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
What is CakePHP?
CakePHP is an open source MVC (Model-View-Controller) web application framework for producing web applications. It is written in PHP, and modeled after the concepts of Ruby on Rails.
Why MVC?
• Separation of concerns• Testability, maintainability• Structure and elegance• Convention over configuration• A natural fit for the web
JavaScript
HTML
PHP
SQLCSS
Business Logic
Why CakePHP?
• Great community• Well-written documentation• Looks and feels very similar to other MVC
frameworks (like Ruby on Rails, .NET MVC)• Easy to pick up
Start with the model…
• Cake has its own built-in ORM, similar to ActiveRecord
• And it’s similar to Linq-to-SQL in that the database is basically the model
• Conventions
Model Conventions
• Table names in the DB are plural and underscored (blog_posts)
• Class names are singular, CamelCased (BlogPost)• File names are singular, underscored
(blog_post.php)• Primary key should be called ‘id’ and be an int• Foreign keys should be ‘whatever_id’• All these conventions can be ignored, but require
configuration to do so
CakePHP Directory Structure
Tools
• Netbeans IDE– http://www.netbeans.org/
• WampServer– http://www.wampserver.com/en/
• HeidiSQL– http://www.heidisql.com/
Scaffolding Demo
Scaffolding
• Gets us to a working app very quickly (Agile, prototyping, etc) by creating views automatically
• In some cases, scaffolding an entire Administrative area might be acceptable
• But as you might guess, scaffolding gives us no flexibility or customization
• Just like real scaffolding, it's meant to be temporary
Real Views
• Let’s ditch the scaffolding, and create ‘real’ views:– Index (view all blog posts)– View (view a single blog post)– Add (new blog post)– Edit (an existing blog post)– Delete (an existing blog post)
URLs and routing
• Default routing:– /controller/action/first_parameter
• Example:– /blogposts/view/3 – BlogPostsController class, View method, pass 3 to
the first parameter• Routing is configured in /config/routes.php
Views Demo
Testing
• Unit tests are often an important part of building an application, especially a complex one
• One of the benefits of using an MVC framework is separation of concerns
• Separation of concerns allows us to test individual units easier
Testing with CakePHP
• Based on SimpleTest (doesn’t come with CakePHP) - http://simpletest.org/
• Can test different “cases”, like “controllers”, “helpers”, “models”, etc.
• Test methods must start with “test”
Testing Demo
Other CakePHP features
• Relatively mature framework• Contains a rich feature set:– Helpers (JS, Ajax, caching, pagination, etc)– Authorization/authentication components– Model supports relational DB (1-to-many, many-
to-many, etc)– Best feature is simplicity: not a huge framework
filled with esoteric functionality
The future of Cake
• 3 different versions of Cake:– CakePHP 1.x is for PHP4 and up– CakePHP v2 will be for strict PHP5mode, shedding
deprecated stuff, faster– Cake3 has been ‘forked’ into Lithium
• CakePHP2 is available at http://code.cakephp.org/
• Lithium is available at http://li3.rad-dev.org/
Resources
• http://cakephp.org/• http://bakery.cakephp.org/articles/view/mod
el-based-code-insight-and-completion-in-netbeans (NetBeans code-completion)
• http://en.wikipedia.org/wiki/Model–view–controller#PHP
• http://cakephpdemo.codeplex.com/ - demo source code
• http://www.simpletest.org/
Please rate me
• http://speakerrate.com/mgroves