xXx - Super Awesome CakePHP
date post
24-Oct-2015Category
Documents
view
22download
0
Embed Size (px)
description
Transcript of xXx - Super Awesome CakePHP
Super Awesome AdvancedCakePHP Tips
This book is available for free at http://www.pseudocoder.com/free-cakephp-book
Version 1.1
By Matt Currypseudocoder.comtwitter.com/mcurry
matt@pseudocoder.com
With Contributions FromMark Story
mark-story.com twitter.com/mark_story
Super Awesome Advanced CakePHP Tips by Matt Curry is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
CakePHP is a registered trademark of the Cake Software Foundation (http://cakefoundation.org)
2
ContentsWho Should Read This Book..........................................................................................................................6
How to Read This Book.............................................................................................................................................6
How to Learn CakePHP..................................................................................................................................7The Paid Way.............................................................................................................................................................7
The Free Way.............................................................................................................................................................8
After youve done Either/Both Of The Above.........................................................................................................8
Models...............................................................................................................................................................9Recursion....................................................................................................................................................................9
Containable Behavior................................................................................................................................................9
Why You Should Use It.............................................................................................................................................9
This Will Catch Everyone At Least Once..............................................................................................................10
Custom Find Types........................................................................................................................................11The Unofficial Cake Way........................................................................................................................................11
My Way....................................................................................................................................................................12
Comparison..............................................................................................................................................................14
App Model................................................................................................................................................................15
Getting the Logged In User from Anywhere...............................................................................................16The User Model .......................................................................................................................................................16
In The AppController..............................................................................................................................................17
Back to the User Model...........................................................................................................................................17
Usage.........................................................................................................................................................................18
What About The Configure Class?........................................................................................................................18
Full Source...............................................................................................................................................................19
Automatically Tracking Created/Modified By............................................................................................20Database...................................................................................................................................................................20
Model Relations.......................................................................................................................................................20
Model beforeValidate Callback..............................................................................................................................20
Behavior beforeValidate Callback.........................................................................................................................20
The Full Behavior....................................................................................................................................................21
Routing............................................................................................................................................................22Case Insensitive........................................................................................................................................................22
Unit Testing....................................................................................................................................................23Views.........................................................................................................................................................................23
3
Setting Up The Files..................................................................................................................................23Setting Up The Test Class.........................................................................................................................23Standard Index View.................................................................................................................................24Creating The View Test.............................................................................................................................25Testing the Rendered View.......................................................................................................................26Controllers.................................................................................................................................................28Doing Things the Hard Way......................................................................................................................28Testing A Controller Method....................................................................................................................30Making assertions......................................................................................................................................31
Mock Objects...........................................................................................................................................................33What is a Mock Object..............................................................................................................................33Where can I get one of these fabulous devices?........................................................................................33Makings expectations with Mock Objects................................................................................................34
Models......................................................................................................................................................................36Test Case...................................................................................................................................................36Fixtures......................................................................................................................................................36
Merging Add and Edit Actions.....................................................................................................................37The Controller.........................................................................................................................................................37
Telling Add And Edit Apart...................................................................................................................................38
The View...................................................................................................................................................................39
Cake Tricks from The Core..........................................................................................................................40Cake Style $options Parameter...............................................................................................................................40
Handling Data Arrays with a Single Record or an Array of Records.................................................................41
Stupid Easy URL Slugs..................................................................................................................................43Making The Slugs A Bit Smarter......................................................................................