Feb 13, 2008 ... curl -s https://getcomposer.org/installer | php ..... Now continue to /tutorials-and-
examples/blog/part-two to start building your first. CakePHP ...
CakePHP Cookbook Documentation Release 3.5
Cake Software Foundation
Aug 21, 2017
Contents
1
CakePHP at a Glance Conventions Over Configuration The Model Layer . . . . . . . . The View Layer . . . . . . . . . The Controller Layer . . . . . . CakePHP Request Cycle . . . . Just the Start . . . . . . . . . . . Additional Reading . . . . . . .
. . . . . . .
1 1 1 2 2 3 4 4
2
Quick Start Guide Bookmarker Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bookmarker Tutorial Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11 11 18
3
3.0 Migration Guide Requirements . . . . . . . . . . . . . . . . . Upgrade Tool . . . . . . . . . . . . . . . . . Application Directory Layout . . . . . . . . . CakePHP should be installed with Composer Namespaces . . . . . . . . . . . . . . . . . . Removed Constants . . . . . . . . . . . . . . Configuration . . . . . . . . . . . . . . . . . New ORM . . . . . . . . . . . . . . . . . . . Basics . . . . . . . . . . . . . . . . . . . . . Debugging . . . . . . . . . . . . . . . . . . . Object settings/configuration . . . . . . . . . Cache . . . . . . . . . . . . . . . . . . . . . Core . . . . . . . . . . . . . . . . . . . . . . Console . . . . . . . . . . . . . . . . . . . . Shell / Task . . . . . . . . . . . . . . . . . . Event . . . . . . . . . . . . . . . . . . . . . Log . . . . . . . . . . . . . . . . . . . . . . Routing . . . . . . . . . . . . . . . . . . . . Network . . . . . . . . . . . . . . . . . . . . Sessions . . . . . . . . . . . . . . . . . . . .
27 27 27 27 28 28 28 28 28 29 29 29 29 30 31 31 32 32 33 34 35
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
i
Network\Http . . . . . Network\Email . . . . Controller . . . . . . . Controller\Components Model . . . . . . . . . TestSuite . . . . . . . . View . . . . . . . . . . View\Helper . . . . . . I18n . . . . . . . . . . L10n . . . . . . . . . . Testing . . . . . . . . . Utility . . . . . . . . . 4
5
6
7
8
ii
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
Tutorials & Examples Bookmarker Tutorial . . . . . . . . . . . . . . . Bookmarker Tutorial Part 2 . . . . . . . . . . . . Blog Tutorial . . . . . . . . . . . . . . . . . . . Blog Tutorial - Part 2 . . . . . . . . . . . . . . . Blog Tutorial - Part 3 . . . . . . . . . . . . . . . Blog Tutorial - Authentication and Authorization Content Management Tutorial . . . . . . . . . . CMS Tutorial - Creating the Database . . . . . . CMS Tutorial - Creating the Articles Controller . CMS Tutorial - Tags and Users . . . . . . . . . . CMS Tutorial - Authentication . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
51 . 51 . 58 . 65 . 68 . 78 . 85 . 92 . 93 . 96 . 105 . 112
Contributing Documentation . . . . . . . . . Tickets . . . . . . . . . . . . . . Code . . . . . . . . . . . . . . . Coding Standards . . . . . . . . Backwards Compatibility Guide
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
119 119 126 127 130 140
Installation Requirements . . . Installing CakePHP Permissions . . . . Development Server Production . . . . . Fire It Up . . . . . URL Rewriting . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
143 143 144 145 146 146 147 147
Configuration Configuring your Application . . . . . Additional Class Paths . . . . . . . . Inflection Configuration . . . . . . . . Environment Variables . . . . . . . . Configure Class . . . . . . . . . . . . Reading and writing configuration files Bootstrapping CakePHP . . . . . . . Disabling Generic Tables . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
153 153 155 156 156 157 158 160 161
. . . . . . .
. . . . . . .
. . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . .
. . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
35 36 36 37 39 40 41 42 46 47 47 48
Routing 163 Quick Tour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Connecting Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Creating RESTful Routes . . . . . . . Passed Arguments . . . . . . . . . . . Generating URLs . . . . . . . . . . . Redirect Routing . . . . . . . . . . . Custom Route Classes . . . . . . . . Creating Persistent URL Parameters . Handling Named Parameters in URLs 9
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
178 181 182 183 183 185 185
Request & Response Objects Request . . . . . . . . . . . . . . . . . . . . . Response . . . . . . . . . . . . . . . . . . . . Setting Cookies . . . . . . . . . . . . . . . . . Setting Cross Origin Request Headers (CORS) . Common Mistakes with Immutable Responses . Cookie Collections . . . . . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
191 191 198 205 205 206 206
10 Controllers The App Controller . . . . . . . . Request Flow . . . . . . . . . . . Controller Actions . . . . . . . . . Interacting with Views . . . . . . Redirecting to Other Pages . . . . Loading Additional Models . . . . Paginating a Model . . . . . . . . Configuring Components to Load . Configuring Helpers to Load . . . Request Life-cycle Callbacks . . . More on Controllers . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
209 209 210 210 211 213 214 215 215 216 216 217
11 Views The App View . . . . . . . . . . View Templates . . . . . . . . . Using View Blocks . . . . . . . Layouts . . . . . . . . . . . . . Elements . . . . . . . . . . . . . View Events . . . . . . . . . . . Creating Your Own View Classes More About Views . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
259 259 260 263 265 267 270 270 271
. . . . . . . .
12 Database Access & ORM 369 Quick Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 13 Bake Console 523 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523 14 Caching Configuring Cache Class . . . . . . . . . . . Writing to a Cache . . . . . . . . . . . . . . Reading From a Cache . . . . . . . . . . . . Deleting From a Cache . . . . . . . . . . . . Clearing Cached Data . . . . . . . . . . . . . Using Cache to Store Counters . . . . . . . . Using Cache to Store Common Query Results Using Groups . . . . . . . . . . . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
533 534 536 537 537 538 538 539 539
iii
Globally Enable or Disable Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540 Creating a Storage Engine for Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540 15 Console Tools, Shells & Tasks The CakePHP Console . . . . . . . . . . Creating a Shell . . . . . . . . . . . . . . Shell Tasks . . . . . . . . . . . . . . . . Using Models in Your Shells . . . . . . . Shell Helpers . . . . . . . . . . . . . . . Invoking Other Shells from Your Shell . . Getting User Input . . . . . . . . . . . . . Creating Files . . . . . . . . . . . . . . . Console Output . . . . . . . . . . . . . . Stopping Shell Execution . . . . . . . . . Hook Methods . . . . . . . . . . . . . . . Configuring Options and Generating Help Renaming Commands . . . . . . . . . . . Routing in Shells / CLI . . . . . . . . . . More Topics . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
543 543 545 546 547 548 548 549 549 549 552 553 553 562 563 563
16 Debugging Basic Debugging . . . . . . . Using the Debugger Class . . . Outputting Values . . . . . . . Logging With Stack Traces . . Generating Stack Traces . . . Getting an Excerpt From a File Using Logging to Debug . . . Debug Kit . . . . . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
575 575 576 576 577 577 577 578 578
17 Deployment Moving files . . . . . . . . . . . . . . . . Adjust config/app.php . . . . . . . . . . . Check Your Security . . . . . . . . . . . Set Document Root . . . . . . . . . . . . Improve Your Application’s Performance Deploying an update . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
579 579 579 580 580 580 581
18 Email Basic Usage . . . . . . . . Configuration . . . . . . . Setting Headers . . . . . . Sending Templated Emails Sending Attachments . . . Using Transports . . . . . Sending Messages Quickly Sending Emails from CLI . Creating Reusable Emails .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
583 583 584 587 587 588 589 590 590 591
19 Error & Exception Handling Error & Exception Configuration . Creating your Own Error Handler Changing Fatal Error Behavior . . Exception Classes . . . . . . . . . Built in Exceptions for CakePHP .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
593 593 594 594 595 595
iv
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
Using HTTP Exceptions in your Controllers . . . . . . . . Exception Renderer . . . . . . . . . . . . . . . . . . . . . Creating your own Application Exceptions . . . . . . . . . Extending and Implementing your own Exception Handlers
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
598 598 598 599
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
603 603 604 605 605 608 611
21 Internationalization & Localization Setting Up Translations . . . . . . . . . . . . . . . . . . . Using Translation Functions . . . . . . . . . . . . . . . . Creating Your Own Translators . . . . . . . . . . . . . . . Localizing Dates and Numbers . . . . . . . . . . . . . . . Automatically Choosing the Locale Based on Request Data
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
613 613 615 619 622 622
22 Logging Logging Configuration . . . Error and Exception Logging Interacting with Log Streams Using the FileLog Adapter . Logging to Syslog . . . . . . Writing to Logs . . . . . . . Log API . . . . . . . . . . . Logging Trait . . . . . . . . Using Monolog . . . . . . .
20 Events System Example Event Usage . . . Accessing Event Managers Core Events . . . . . . . . Registering Listeners . . . Dispatching Events . . . . Additional Reading . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
625 625 627 627 627 628 628 630 631 631
23 Modelless Forms Creating a Form . . . . . . . . . . . . . . . . . . . Processing Request Data . . . . . . . . . . . . . . Setting Form Values . . . . . . . . . . . . . . . . . Getting Form Errors . . . . . . . . . . . . . . . . . Invalidating Individual Form Fields from Controller Creating HTML with FormHelper . . . . . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
633 633 634 635 635 635 636
24 Plugins Installing a Plugin With Composer . Loading a Plugin . . . . . . . . . . Plugin Configuration . . . . . . . . Using Plugins . . . . . . . . . . . . Creating Your Own Plugins . . . . . Plugin Routes . . . . . . . . . . . . Plugin Controllers . . . . . . . . . . Plugin Models . . . . . . . . . . . . Plugin Views . . . . . . . . . . . . Plugin Assets . . . . . . . . . . . . Components, Helpers and Behaviors Expand Your Plugin . . . . . . . . . Publish Your Plugin . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
637 637 638 639 640 640 641 642 643 644 645 646 646 647
25 REST
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
649
v
The Simple Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649 Accepting Input in Other Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651 RESTful Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651 26 Security 653 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 27 Sessions Session Configuration . . . . . . . . . . . Built-in Session Handlers & Configuration Setting ini directives . . . . . . . . . . . . Creating a Custom Session Handler . . . Accessing the Session Object . . . . . . . Reading & Writing Session Data . . . . . Destroying the Session . . . . . . . . . . Rotating Session Identifiers . . . . . . . . Flash Messages . . . . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
657 657 658 660 660 662 662 663 663 663
28 Testing Installing PHPUnit . . . . . . Test Database Setup . . . . . . Checking the Test Setup . . . . Test Case Conventions . . . . Creating Your First Test Case . Running Tests . . . . . . . . . Test Case Lifecycle Callbacks Fixtures . . . . . . . . . . . . Testing Table Classes . . . . . Controller Integration Testing . Console Integration Testing . . Testing Views . . . . . . . . . Testing Components . . . . . . Testing Helpers . . . . . . . . Testing Events . . . . . . . . . Creating Test Suites . . . . . . Creating Tests for Plugins . . . Generating Tests with Bake . . Integration with Jenkins . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
665 665 666 666 667 667 669 670 671 676 677 686 690 691 692 694 695 695 697 697
29 Validation Creating Validators . Validating Data . . . Validating Entities . . Core Validation Rules
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
699 699 706 706 707
30 App Class Finding Classes . . . . . . . Finding Paths to Namespaces Locating Plugins . . . . . . Locating Themes . . . . . . Loading Vendor Files . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
709 709 709 710 710 710
. . . .
. . . .
. . . .
31 Collections 713 Quick Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713 List of Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
vi
Iterating . . . . . . . . Filtering . . . . . . . . Aggregation . . . . . . Sorting . . . . . . . . . Working with Tree Data Other Methods . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
714 718 719 723 724 725
32 Folder & File 733 Basic Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733 Folder API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733 File API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737 33 Hash 741 Hash Path Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741 34 Http Client Doing Requests . . . . . . . . . . . . Creating Multipart Requests with Files Sending Request Bodies . . . . . . . Request Method Options . . . . . . . Authentication . . . . . . . . . . . . . Creating Scoped Clients . . . . . . . . Setting and Managing Cookies . . . . Response Objects . . . . . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
757 757 758 759 759 760 761 762 763
35 Inflector Summary of Inflector Methods and Their Output . Creating Plural & Singular Forms . . . . . . . . Creating CamelCase and under_scored Forms . . Creating Human Readable Forms . . . . . . . . . Creating Table and Class Name Forms . . . . . . Creating Variable Names . . . . . . . . . . . . . Creating URL Safe Strings . . . . . . . . . . . . Inflection Configuration . . . . . . . . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
767 767 768 768 768 768 769 769 769
36 Number Formatting Currency Values . . . . . . Setting the Default Currency . . . . . . Formatting Floating Point Numbers . . Formatting Percentages . . . . . . . . . Interacting with Human Readable Values Formatting Numbers . . . . . . . . . . Format Differences . . . . . . . . . . . Configure formatters . . . . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
771 771 772 772 773 773 773 775 775
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
37 Registry Objects 777 Loading Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777 Triggering Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777 Disabling Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778 38 Text Convert Strings into ASCII Creating URL Safe Strings Generating UUIDs . . . . Simple String Parsing . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
779 779 780 780 781
vii
Formatting Strings . . . . . . . . . . Wrapping Text . . . . . . . . . . . . . Highlighting Substrings . . . . . . . . Removing Links . . . . . . . . . . . . Truncating Text . . . . . . . . . . . . Truncating the Tail of a String . . . . Extracting an Excerpt . . . . . . . . . Converting an Array to Sentence Form
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
781 781 782 783 783 784 785 785
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
787 788 788 789 792 792 792 793 793 794 794
40 Xml Importing Data to Xml Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Transforming a XML String in Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Transforming an Array into a String of XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
795 795 796 796
41 Constants & Functions Global Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Core Definition Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Timing Definition Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
799 799 801 801
42 Debug Kit Installation . . . . . . . . . DebugKit Storage . . . . . . Toolbar Usage . . . . . . . . Using the History Panel . . . Using The Mail Panel . . . . Developing Your Own Panels
39 Date & Time Creating Time Instances . . . . . . Manipulation . . . . . . . . . . . Formatting . . . . . . . . . . . . . Conversion . . . . . . . . . . . . Comparing With the Present . . . Comparing With Intervals . . . . . Dates . . . . . . . . . . . . . . . Immutable Dates and Times . . . Accepting Localized Request Data Supported Timezones . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
803 803 803 804 804 805 805
43 Migrations Installation . . . . . . . . . . . . . . . . . . . . Overview . . . . . . . . . . . . . . . . . . . . . Creating Migrations . . . . . . . . . . . . . . . . Generating migrations from an existing database . Generating a diff between two database states . . The commands . . . . . . . . . . . . . . . . . . Using Migrations In Plugins . . . . . . . . . . . Running Migrations in a non-shell environment . Tips and tricks . . . . . . . . . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
809 809 810 811 816 816 817 821 821 822
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
44 Appendices 827 3.x Migration Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827 General Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863 PHP Namespace Index
viii
867
Index
869
ix
x
CHAPTER 1
CakePHP at a Glance
CakePHP is designed to make common web-development tasks simple, and easy. By providing an all-in-one toolbox to get you started the various parts of CakePHP work well together or separately. The goal of this overview is to introduce the general concepts in CakePHP, and give you a quick overview of how those concepts are implemented in CakePHP. If you are itching to get started on a project, you can start with the tutorial, or dive into the docs.
Conventions Over Configuration CakePHP provides a basic organizational structure that covers class names, filenames, database table names, and other conventions. While the conventions take some time to learn, by following the conventions CakePHP provides you can avoid needless configuration and make a uniform application structure that makes working with various projects simple. The conventions chapter covers the various conventions that CakePHP uses.
The Model Layer The Model layer represents the part of your application that implements the business logic. It is responsible for retrieving data and converting it into the primary meaningful concepts in your application. This includes processing, validating, associating or other tasks related to handling data. In the case of a social network, the Model layer would take care of tasks such as saving the user data, saving friends’ associations, storing and retrieving user photos, finding suggestions for new friends, etc. The model objects can be thought of as “Friend”, “User”, “Comment”, or “Photo”. If we wanted to load some data from our users table we could do: use Cake\ORM\TableRegistry; $users = TableRegistry::get('Users'); $query = $users->find(); foreach ($query as $row) { echo $row->username; }
You may notice that we didn’t have to write any code before we could start working with our data. By using conventions, CakePHP will use standard classes for table and entity classes that have not yet been defined. 1
CakePHP Cookbook Documentation, Release 3.5
If we wanted to make a new user and save it (with validation) we would do something like: use Cake\ORM\TableRegistry; $users = TableRegistry::get('Users'); $user = $users->newEntity(['email' => '
[email protected]']); $users->save($user);
The View Layer The View layer renders a presentation of modeled data. Being separate from the Model objects, it is responsible for using the information it has available to produce any presentational interface your application might need. For example, the view could use model data to render an HTML view template containing it, or a XML formatted result for others to consume: // In a view template file, we'll render an 'element' for each user.
The View layer provides a number of extension points like View Templates, Elements and View Cells to let you re-use your presentation logic. The View layer is not only limited to HTML or text representation of the data. It can be used to deliver common data formats like JSON, XML, and through a pluggable architecture any other format you may need, such as CSV.
The Controller Layer The Controller layer handles requests from users. It is responsible for rendering a response with the aid of both the Model and the View layers. A controller can be seen as a manager that ensures that all resources needed for completing a task are delegated to the correct workers. It waits for petitions from clients, checks their validity according to authentication or authorization rules, delegates data fetching or processing to the model, selects the type of presentational data that the clients are accepting, and finally delegates the rendering process to the View layer. An example of a user registration controller would be: public function add() { $user = $this->Users->newEntity(); if ($this->request->is('post')) { $user = $this->Users->patchEntity($user, $this->request->getData()); if ($this->Users->save($user, ['validate' => 'registration'])) { $this->Flash->success(__('You are now registered.')); } else { $this->Flash->error(__('There were some problems.')); } } $this->set('user', $user); }
2
Chapter 1. CakePHP at a Glance
CakePHP Cookbook Documentation, Release 3.5
You may notice that we never explicitly rendered a view. CakePHP’s conventions will take care of selecting the right view and rendering it with the view data we prepared with set().
CakePHP Request Cycle Now that you are familiar with the different layers in CakePHP, lets review how a request cycle works in CakePHP:
The typical CakePHP request cycle starts with a user requesting a page or resource in your application. At a high level each request goes through the following steps: 1. The webserver rewrite rules direct the request to webroot/index.php. 2. Your Application is loaded and bound to an HttpServer. 3. Your application’s middleware is initialized. 4. A request and response is dispatched through the PSR-7 Middleware that your application uses. Typically this includes error trapping and routing. 5. If no response is returned from the middleware and the request contains routing information, a controller & action are selected. 6. The controller’s action is called and the controller interacts with the required Models and Components. CakePHP Request Cycle
3
CakePHP Cookbook Documentation, Release 3.5
7. The controller delegates response creation to the View to generate the output resulting from the model data. 8. The view uses Helpers and Cells to generate the response body and headers. 9. The response is sent back out through the /controllers/middleware. 10. The HttpServer emits the response to the webserver.
Just the Start Hopefully this quick overview has piqued your interest. Some other great features in CakePHP are: • A caching framework that integrates with Memcached, Redis and other backends. • Powerful code generation tools so you can start immediately. • Integrated testing framework so you can ensure your code works perfectly. The next obvious steps are to download CakePHP, read the tutorial and build something awesome.
Additional Reading Where to Get Help The Official CakePHP website https://cakephp.org The Official CakePHP website is always a great place to visit. It features links to oft-used developer tools, screencasts, donation opportunities, and downloads. The Cookbook https://book.cakephp.org This manual should probably be the first place you go to get answers. As with many other open source projects, we get new folks regularly. Try your best to answer your questions on your own first. Answers may come slower, but will remain longer – and you’ll also be lightening our support load. Both the manual and the API have an online component. The Bakery https://bakery.cakephp.org The CakePHP Bakery is a clearing house for all things regarding CakePHP. Check it out for tutorials, case studies, and code examples. Once you’re acquainted with CakePHP, log on and share your knowledge with the community and gain instant fame and fortune.
4
Chapter 1. CakePHP at a Glance
CakePHP Cookbook Documentation, Release 3.5
The API https://api.cakephp.org/ Straight to the point and straight from the core developers, the CakePHP API (Application Programming Interface) is the most comprehensive documentation around for all the nitty gritty details of the internal workings of the framework. It’s a straight forward code reference, so bring your propeller hat. The Test Cases If you ever feel the information provided in the API is not sufficient, check out the code of the test cases provided with CakePHP. They can serve as practical examples for function and data member usage for a class. tests/TestCase/
The IRC Channel IRC Channels on irc.freenode.net: • #cakephp – General Discussion • #cakephp-docs – Documentation • #cakephp-bakery – Bakery • #cakephp-fr – French Canal. If you’re stumped, give us a holler in the CakePHP IRC channel. Someone from the development team4 is usually there, especially during the daylight hours for North and South America users. We’d love to hear from you, whether you need some help, want to find users in your area, or would like to donate your brand new sports car. Official CakePHP Forum CakePHP Official Forum5 Our official forum where you can ask for help, suggest ideas and have a talk about CakePHP. It’s a perfect place for quickly finding answers and help others. Join the CakePHP family by signing up. Stackoverflow https://stackoverflow.com/6 Tag your questions with cakephp and the specific version you are using to enable existing users of stackoverflow to find your questions. Where to get Help in your Language Brazilian Portuguese • Brazilian CakePHP Community7 4 5 6 7
https://github.com/cakephp?tab=members http://discourse.cakephp.org https://stackoverflow.com/questions/tagged/cakephp/ http://cakephp-br.org
Additional Reading
5
CakePHP Cookbook Documentation, Release 3.5
Danish • Danish CakePHP Slack Channel8 French • French CakePHP Community9 German • German CakePHP Slack Channel10 • German CakePHP Facebook Group11 Iranian • Iranian CakePHP Community12 Dutch • Dutch CakePHP Slack Channel13 Japanese • Japanese CakePHP Slack Channel14 • Japanese CakePHP Facebook Group15 Portuguese • Portuguese CakePHP Google Group16 Spanish • Spanish CakePHP Slack Channel17 • Spanish CakePHP IRC Channel • Spanish CakePHP Google Group18 8 9 10 11 12 13 14 15 16 17 18
6
https://cakesf.slack.com/messages/denmark/ http://cakephp-fr.org https://cakesf.slack.com/messages/german/ https://www.facebook.com/groups/146324018754907/ http://cakephp.ir https://cakesf.slack.com/messages/netherlands/ https://cakesf.slack.com/messages/japanese/ https://www.facebook.com/groups/304490963004377/ http://groups.google.com/group/cakephp-pt https://cakesf.slack.com/messages/spanish/ http://groups.google.com/group/cakephp-esp
Chapter 1. CakePHP at a Glance
CakePHP Cookbook Documentation, Release 3.5
CakePHP Conventions We are big fans of convention over configuration. While it takes a bit of time to learn CakePHP’s conventions, you save time in the long run. By following conventions, you get free functionality, and you liberate yourself from the maintenance nightmare of tracking config files. Conventions also make for a very uniform development experience, allowing other developers to jump in and help. Controller Conventions Controller class names are plural, CamelCased, and end in Controller. UsersController and ArticleCategoriesController are both examples of conventional controller names. Public methods on Controllers are often exposed as ‘actions’ accessible through a web browser. For example the /users/view maps to the view() method of the UsersController out of the box. Protected or private methods cannot be accessed with routing. URL Considerations for Controller Names As you’ve just seen, single word controllers map to a simple lower case URL path. For example, UsersController (which would be defined in the file name UsersController.php) is accessed from http://example.com/users. While you can route multiple word controllers in any way you like, the convention is that your URLs are lowercase and dashed using the DashedRoute class, therefore /article-categories/view-all is the correct form to access the ArticleCategoriesController::viewAll() action. When you create links using this->Html->link(), you can use the following conventions for the url array: $this->Html->link('link-title', [ 'prefix' => 'MyPrefix' // CamelCased 'plugin' => 'MyPlugin', // CamelCased 'controller' => 'ControllerName', // CamelCased 'action' => 'actionName' // camelBacked ]
For more information on CakePHP URLs and parameter handling, see Connecting Routes. File and Class Name Conventions In general, filenames match the class names, and follow the PSR-0 or PSR-4 standards for autoloading. The following are some examples of class names and their filenames: • The Controller class LatestArticlesController would be found in a file named LatestArticlesController.php • The Component class MyHandyComponent would be found in a file named MyHandyComponent.php • The Table class OptionValuesTable would be found in a file named OptionValuesTable.php. • The Entity class OptionValue would be found in a file named OptionValue.php. • The Behavior class EspeciallyFunkableBehavior would be found in a file named EspeciallyFunkableBehavior.php • The View class SuperSimpleView would be found in a file named SuperSimpleView.php • The Helper class BestEverHelper would be found in a file named BestEverHelper.php Each file would be located in the appropriate folder/namespace in your app folder. Additional Reading
7
CakePHP Cookbook Documentation, Release 3.5
Model and Database Conventions Table class names are plural, CamelCased and end in Table. UsersTable, ArticleCategoriesTable, and UserFavoritePagesTable are all examples of conventional model names. Table names corresponding to CakePHP models are plural and underscored. The underlying tables for the above mentioned models would be users, article_categories, and user_favorite_pages, respectively. The convention is to use English words for table and column names. If you use words in another language, CakePHP might not be able to process the right inflections (from singular to plural and vice-versa). If you need to add your own language rules for some words, you can use the utility class Cake\Utility\Inflector. Besides defining those custom inflection rules, this class also allows you to check that CakePHP understands your custom syntax for plurals and singulars words. See the documentation about Inflector for more information. Field names with two or more words are underscored: first_name. Foreign keys in hasMany, belongsTo/hasOne relationships are recognized by default as the (singular) name of the related table followed by _id. So if Users hasMany Articles, the articles table will refer to the users table via a user_id foreign key. For a table like article_categories whose name contains multiple words, the foreign key would be article_category_id. Join tables, used in BelongsToMany relationships between models, should be named after the model tables they will join, arranged in alphabetical order (articles_tags rather than tags_articles). In addition to use an auto-increment key as the primary key, you may also use UUID columns. CakePHP will create a unique 36 character UUID (Cake\Utility\Text::uuid()) whenever you save a new record using the Table::save() method. View Conventions View template files are named after the controller functions they display, in an underscored form. The viewAll() function of the ArticlesController class will look for a view template in src/Template/Articles/view_all.ctp. The basic pattern is src/Template/Controller/underscored_function_name.ctp. By naming the pieces of your application using CakePHP conventions, you gain functionality without the hassle and maintenance tethers of configuration. Here’s a final example that ties the conventions together: • Database table: “articles” • Table class: ArticlesTable, found at src/Model/Table/ArticlesTable.php • Entity class: Article, found at src/Model/Entity/Article.php • Controller class: ArticlesController, found at src/Controller/ArticlesController.php • View template, found at src/Template/Articles/index.ctp Using these conventions, CakePHP knows that a request to http://example.com/articles/ maps to a call on the index() function of the ArticlesController, where the Articles model is automatically available (and automatically tied to the ‘articles’ table in the database), and renders to a file. None of these relationships have been configured by any means other than by creating classes and files that you’d need to create anyway. Now that you’ve been introduced to CakePHP’s fundamentals, you might try a run through the Bookmarker Tutorial to see how things fit together.
CakePHP Folder Structure After you’ve downloaded the CakePHP application skeleton, there are a few top level folders you should see:
8
Chapter 1. CakePHP at a Glance
CakePHP Cookbook Documentation, Release 3.5
• The bin folder holds the Cake console executables. • The config folder holds the (few) Configuration files CakePHP uses. Database connection details, bootstrapping, core configuration files and more should be stored here. • The plugins folder is where the Plugins your application uses are stored. • The logs folder normally contains your log files, depending on your log configuration. • The src folder will be where your application’s files will be placed. • The tests folder will be where you put the test cases for your application. • The tmp folder is where CakePHP stores temporary data. The actual data it stores depends on how you have CakePHP configured, but this folder is usually used to store translation messages, model descriptions and sometimes session information. • The vendor folder is where CakePHP and other application dependencies will be installed by composer. Editing these files is not advised, as composer will overwrite your changes next time you update. • The webroot directory is the public document root of your application. It contains all the files you want to be publically reachable. Make sure that the tmp and logs folders exist and are writable, otherwise the performance of your application will be severely impacted. In debug mode, CakePHP will warn you, if these directories are not writable. The src Folder CakePHP’s src folder is where you will do most of your application development. Let’s look a little closer at the folders inside src. Controller Contains your application’s controllers and their components. Locale Stores string files for internationalization. Model Contains your application’s tables, entities and behaviors. Shell Contains the console commands and console tasks for your application. For more information see Console Tools, Shells & Tasks. View Presentational classes are placed here: views, cells, helpers. Template Presentational files are placed here: elements, error pages, layouts, and view template files.
Additional Reading
9
CakePHP Cookbook Documentation, Release 3.5
10
Chapter 1. CakePHP at a Glance
CHAPTER 2
Quick Start Guide
The best way to experience and learn CakePHP is to sit down and build something. To start off we’ll build a simple bookmarking application.
Bookmarker Tutorial This tutorial will walk you through the creation of a simple bookmarking application (bookmarker). To start with, we’ll be installing CakePHP, creating our database, and using the tools CakePHP provides to get our application up fast. Here’s what you’ll need: 1. A database server. We’re going to be using MySQL server in this tutorial. You’ll need to know enough about SQL in order to create a database: CakePHP will be taking the reins from there. Since we’re using MySQL, also make sure that you have pdo_mysql enabled in PHP. 2. Basic PHP knowledge. Before starting you should make sure that you have got an up to date PHP version: php -v
You should at least have got installed PHP 5.6.0 (CLI) or higher. Your webserver’s PHP version must also be of 5.6.0 or higher, and should best be the same version your command line interface (CLI) PHP version is of. If you’d like to see the completed application, checkout cakephp/bookmarker19 . Let’s get started!
Getting CakePHP The easiest way to install CakePHP is to use Composer. Composer is a simple way of installing CakePHP from your terminal or command line prompt. First, you’ll need to download and install Composer if you haven’t done so already. If you have cURL installed, it’s as easy as running the following: curl -s https://getcomposer.org/installer | php 19
https://github.com/cakephp/bookmarker-tutorial
11
CakePHP Cookbook Documentation, Release 3.5
Or, you can download composer.phar from the Composer website20 . Then simply type the following line in your terminal from your installation directory to install the CakePHP application skeleton in the bookmarker directory: php composer.phar create-project --prefer-dist cakephp/app bookmarker
If you downloaded and ran the Composer Windows Installer21 , then type the following line in your terminal from your installation directory (ie. C:\wamp\www\dev\cakephp3): composer self-update && composer create-project --prefer-dist cakephp/app bookmarker
The advantage to using Composer is that it will automatically complete some important set up tasks, such as setting the correct file permissions and creating your config/app.php file for you. There are other ways to install CakePHP. If you cannot or don’t want to use Composer, check out the Installation section. Regardless of how you downloaded and installed CakePHP, once your set up is completed, your directory setup should look something like the following: /bookmarker /bin /config /logs /plugins /src /tests /tmp /vendor /webroot .editorconfig .gitignore .htaccess .travis.yml composer.json index.php phpunit.xml.dist README.md
Now might be a good time to learn a bit about how CakePHP’s directory structure works: check out the CakePHP Folder Structure section.
Checking our Installation We can quickly check that our installation is correct, by checking the default home page. Before you can do that, you’ll need to start the development server: bin/cake server
Note: For Windows, the command needs to be bin\cake server (note the backslash). 20 21
12
https://getcomposer.org/download/ https://getcomposer.org/Composer-Setup.exe
Chapter 2. Quick Start Guide
CakePHP Cookbook Documentation, Release 3.5
This will start PHP’s built-in webserver on port 8765. Open up http://localhost:8765 in your web browser to see the welcome page. All the bullet points should be checkmarks other than CakePHP being able to connect to your database. If not, you may need to install additional PHP extensions, or set directory permissions.
Creating the Database Next, let’s set up the database for our bookmarking application. If you haven’t already done so, create an empty database for use in this tutorial, with a name of your choice, e.g. cake_bookmarks. You can execute the following SQL to create the necessary tables: CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, created DATETIME, modified DATETIME ); CREATE TABLE bookmarks ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, title VARCHAR(50), description TEXT, url TEXT, created DATETIME, modified DATETIME, FOREIGN KEY user_key (user_id) REFERENCES users(id) ); CREATE TABLE tags ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255), created DATETIME, modified DATETIME, UNIQUE KEY (title) ); CREATE TABLE bookmarks_tags ( bookmark_id INT NOT NULL, tag_id INT NOT NULL, PRIMARY KEY (bookmark_id, tag_id), FOREIGN KEY tag_key(tag_id) REFERENCES tags(id), FOREIGN KEY bookmark_key(bookmark_id) REFERENCES bookmarks(id) );
You may have noticed that the bookmarks_tags table used a composite primary key. CakePHP supports composite primary keys almost everywhere, making it easier to build multi-tenanted applications. The table and column names we used were not arbitrary. By using CakePHP’s naming conventions, we can leverage CakePHP better and avoid having to configure the framework. CakePHP is flexible enough to accommodate even inconsistent legacy database schemas, but adhering to the conventions will save you time.
Database Configuration Next, let’s tell CakePHP where our database is and how to connect to it. For many, this will be the first and last time you will need to configure anything. Bookmarker Tutorial
13
CakePHP Cookbook Documentation, Release 3.5
The configuration should be pretty straightforward: just replace the values in the Datasources.default array in the config/app.php file with those that apply to your setup. A sample completed configuration array might look something like the following: return [ // More configuration above. 'Datasources' => [ 'default' => [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Mysql', 'persistent' => false, 'host' => 'localhost', 'username' => 'cakephp', 'password' => 'AngelF00dC4k3~', 'database' => 'cake_bookmarks', 'encoding' => 'utf8', 'timezone' => 'UTC', 'cacheMetadata' => true, ], ], // More configuration below. ];
Once you’ve saved your config/app.php file, you should see that ‘CakePHP is able to connect to the database’ section have a checkmark. Note: A copy of CakePHP’s default configuration file is found in config/app.default.php.
Generating Scaffold Code Because our database is following the CakePHP conventions, we can use the bake console application to quickly generate a basic application. In your command line run the following commands: // On Windows bin/cake bake bin/cake bake bin/cake bake
you'll need to use bin\cake instead. all users all bookmarks all tags
This will generate the controllers, models, views, their corresponding test cases, and fixtures for our users, bookmarks and tags resources. If you’ve stopped your server, restart it and go to http://localhost:8765/bookmarks. You should see a basic but functional application providing data access to your application’s database tables. Once you’re at the list of bookmarks, add a few users, bookmarks, and tags. Note: If you see a Not Found (404) page, confirm that the Apache mod_rewrite module is loaded.
Adding Password Hashing When you created your users (by visiting http://localhost:8765/users), you probably noticed that the passwords were stored in plain text. This is pretty bad from a security point of view, so let’s get that fixed.
14
Chapter 2. Quick Start Guide
CakePHP Cookbook Documentation, Release 3.5
This is also a good time to talk about the model layer in CakePHP. In CakePHP, we separate the methods that operate on a collection of objects, and a single object into different classes. Methods that operate on the collection of entities are put in the Table class, while features belonging to a single record are put on the Entity class. For example, password hashing is done on the individual record, so we’ll implement this behavior on the entity object. Because, we want to hash the password each time it is set, we’ll use a mutator/setter method. CakePHP will call convention based setter methods any time a property is set in one of your entities. Let’s add a setter for the password. In src/Model/Entity/User.php add the following: namespace App\Model\Entity; use Cake\Auth\DefaultPasswordHasher; //include this line use Cake\ORM\Entity; class User extends Entity { // Code from bake. protected function _setPassword($value) { $hasher = new DefaultPasswordHasher(); return $hasher->hash($value); } }
Now update one of the users you created earlier, if you change their password, you should see a hashed password instead of the original value on the list or view pages. CakePHP hashes passwords with bcrypt22 by default. You can also use sha1 or md5 if you’re working with an existing database. Note: If the password doesn’t get hashed, make sure you followed the same case for the password member of the class while naming the setter function
Getting Bookmarks with a Specific Tag Now that we’re storing passwords safely, we can build out some more interesting features in our application. Once you’ve amassed a collection of bookmarks, it is helpful to be able to search through them by tag. Next we’ll implement a route, controller action, and finder method to search through bookmarks by tag. Ideally, we’d have a URL that looks like http://localhost:8765/bookmarks/tagged/funny/cat/gifs. This would let us find all the bookmarks that have the ‘funny’, ‘cat’ or ‘gifs’ tags. Before we can implement this, we’ll add a new route. Your config/routes.php should look like:
| |
Blog Tutorial - Part 2
75
CakePHP Cookbook Documentation, Release 3.5
| |
Deleting Articles Next, let’s make a way for users to delete articles. Start with a delete() action in the ArticlesController: // src/Controller/ArticlesController.php public function delete($id) { $this->request->allowMethod(['post', 'delete']); $article = $this->Articles->get($id); if ($this->Articles->delete($article)) { $this->Flash->success(__('The article with id: {0} has been deleted.', h( ˓→$id))); return $this->redirect(['action' => 'index']); } }
This logic deletes the article specified by $id, and uses $this->Flash->success() to show the user a confirmation message after redirecting them on to /articles. If the user attempts to do a delete using a GET request, the allowMethod() will throw an Exception. Uncaught exceptions are captured by CakePHP’s exception handler, and a nice error page is displayed. There are many built-in Exceptions that can be used to indicate the various HTTP errors your application might need to generate. Because we’re just executing some logic and redirecting, this action has no view. You might want to update your index view with links that allow users to delete articles, however:
Blog articles
Id | Title | Created | Actions |
|
76
Chapter 4. Tutorials & Examples
CakePHP Cookbook Documentation, Release 3.5
| | |
Using View\Helper\FormHelper::postLink() will create a link that uses JavaScript to do a POST request deleting our article. Warning: Allowing content to be deleted using GET requests is dangerous, as web crawlers could accidentally delete all your content.
Note: This view code also uses the FormHelper to prompt the user with a JavaScript confirmation dialog before they attempt to delete an article.
Routes For some, CakePHP’s default routing works well enough. Developers who are sensitive to user-friendliness and general search engine compatibility will appreciate the way that CakePHP’s URLs map to specific actions. So we’ll just make a quick change to routes in this tutorial. For more information on advanced routing techniques, see Connecting Routes. By default, CakePHP responds to a request for the root of your site (e.g., http://www.example.com) using its PagesController, rendering a view called “home”. Instead, we’ll replace this with our ArticlesController by creating a routing rule. CakePHP’s routing is found in config/routes.php. You’ll want to comment out or remove the line that defines the default root route. It looks like this: $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
This line connects the URL ‘/’ with the default CakePHP home page. We want it to connect with our own controller, so replace that line with this one: $routes->connect('/', ['controller' => 'Articles', 'action' => 'index']);
This should connect users requesting ‘/’ to the index() action of our ArticlesController. Note: CakePHP also makes use of ‘reverse routing’. If, with the above route defined, you pass ['controller' => 'Articles','action' => 'index'] to a function expecting an array, the resulting URL used will be
Blog Tutorial - Part 2
77
CakePHP Cookbook Documentation, Release 3.5
‘/’. It’s therefore a good idea to always use arrays for URLs as this means your routes define where a URL goes, and also ensures that links point to the same place.
Conclusion Creating applications this way will win you peace, honor, love, and money beyond even your wildest fantasies. Simple, isn’t it? Keep in mind that this tutorial was very basic. CakePHP has many more features to offer, and is flexible in ways we didn’t wish to cover here for simplicity’s sake. Use the rest of this manual as a guide for building more feature-rich applications. Now that you’ve created a basic CakePHP application, you can either continue to Blog Tutorial - Part 3, or start your own project. You can also peruse the /topics or API48 to learn more about CakePHP. If you need help, there are many ways to get the help you need - please see the Where to Get Help page. Welcome to CakePHP! Suggested Follow-up Reading These are common tasks people learning CakePHP usually want to study next: 1. Layouts: Customizing your website layout 2. Elements: Including and reusing view snippets 3. Code Generation with Bake: Generating basic CRUD code 4. Blog Tutorial - Authentication and Authorization: User authentication and authorization tutorial
Blog Tutorial - Part 3 Create a Tree Category Let’s continue our blog application and imagine we want to categorize our articles. We want the categories to be ordered, and for this, we will use the Tree behavior to help us organize the categories. But first, we need to modify our tables.
Migrations Plugin We will use the migrations plugin49 to create a table in our database. If you already have an articles table in your database, erase it. Now open your application’s composer.json file. Normally you would see that the migrations plugin is already under require. If not, add it by executing: composer require cakephp/migrations:~1.0
The migrations plugin will now be in your application’s plugins Plugin::load('Migrations'); to your application’s bootstrap.php file.
folder.
Also,
add
Once the plugin is loaded, run the following command to create a migration file: 48 49
78
https://api.cakephp.org https://github.com/cakephp/migrations
Chapter 4. Tutorials & Examples
CakePHP Cookbook Documentation, Release 3.5
bin/cake bake migration CreateArticles title:string body:text category_id:integer ˓→created modified
A migration file will be generated in the /config/Migrations folder with the following:
Modifying the ArticlesController In our ArticlesController, we’ll get the list of all the categories. This will allow us to choose a category for an Article when creating or editing it: // src/Controller/ArticlesController.php namespace App\Controller; use Cake\Network\Exception\NotFoundException; class ArticlesController extends AppController { // ... public function add() { $article = $this->Articles->newEntity(); if ($this->request->is('post')) { $article = $this->Articles->patchEntity($article, $this->request-> ˓→getData()); if ($this->Articles->save($article)) { $this->Flash->success(__('Your article has been saved.')); return $this->redirect(['action' => 'index']); } $this->Flash->error(__('Unable to add your article.')); } $this->set('article', $article); // Just added the categories list to be able to choose // one category for an article $categories = $this->Articles->Categories->find('treeList'); $this->set(compact('categories')); } }
Modifying the Articles Templates The article add file should look something like this:
Chapter 4. Tutorials & Examples