CakePHP Cookbook
CakePHP Cookbook
Release 3.8
1 CakePHP at a Glance 1
Conventions Over Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The Model Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The View Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
The Controller Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
CakePHP Request Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Just the Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Additional Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
i
Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Network\Http . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Network\Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Controller\Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
TestSuite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
View\Helper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
I18n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
L10n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5 Contributing 117
Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Tickets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Coding Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Backwards Compatibility Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
6 Installation 143
Installing CakePHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Development Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Production . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Fire It Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
URL Rewriting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
7 Configuration 153
Configuring your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Additional Class Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Inflection Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Configure Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Reading and writing configuration files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Bootstrapping CakePHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Disabling Generic Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
8 Routing 163
Quick Tour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Connecting Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
ii
Connecting Scoped Middleware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
RESTful Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Passed Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Generating URLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Redirect Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Entity Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Custom Route Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Creating Persistent URL Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Handling Named Parameters in URLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
10 Controllers 213
The App Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Request Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Controller Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Interacting with Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Redirecting to Other Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Loading Additional Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Paginating a Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Configuring Components to Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Configuring Helpers to Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Request Life-cycle Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
More on Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
11 Views 263
The App View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
View Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Using View Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
View Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Creating Your Own View Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
More About Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
13 Caching 539
Configuring Cache Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
Writing to a Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Reading From a Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Deleting From a Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Clearing Cached Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Using Cache to Store Counters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Using Cache to Store Common Query Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Using Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
Globally Enable or Disable Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
iii
Creating a Cache Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
16 Debugging 593
Basic Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Using the Debugger Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
Outputting Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
Logging With Stack Traces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Generating Stack Traces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Getting an Excerpt From a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Using Logging to Debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
Debug Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
17 Deployment 597
Moving files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
Adjust config/app.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
Check Your Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
Set Document Root . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
Improve Your Application’s Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
Deploying an update . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
18 Email 601
Basic Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
Setting Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
Sending Templated Emails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
Sending Attachments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606
Using Transports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Sending Messages Quickly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
Sending Emails from CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Creating Reusable Emails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Testing Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
iv
Core Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Registering Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Dispatching Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
Additional Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
22 Logging 645
Logging Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
Error and Exception Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
Interacting with Log Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
Using the FileLog Adapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
Logging to Syslog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
Writing to Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
Log API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
Logging Trait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
Using Monolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
24 Plugins 659
Installing a Plugin With Composer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
Manually Installing a Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
Loading a Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
Plugin Hook Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
Using Plugin Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Creating Your Own Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
Plugin Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
Plugin Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
Plugin Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
Plugin Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
Plugin Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
Plugin Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
Components, Helpers and Behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
Testing your Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
Publishing your Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
Plugin Map File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
Manage Your Plugins using Mixer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672
25 REST 673
The Simple Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
Accepting Input in Other Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
RESTful Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
v
26 Security 677
Security Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
27 Sessions 681
Session Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
Built-in Session Handlers & Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682
Setting ini directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
Creating a Custom Session Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
Accessing the Session Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
Reading & Writing Session Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
Destroying the Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
Rotating Session Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
Flash Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
28 Testing 689
Installing PHPUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
Test Database Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690
Checking the Test Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690
Test Case Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Creating Your First Test Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Running Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Test Case Lifecycle Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
Testing Table Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700
Controller Integration Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
Console Integration Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
Testing Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
Testing Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712
Testing Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
Testing Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
Testing Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716
Creating Test Suites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716
Creating Tests for Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716
Generating Tests with Bake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Integration with Jenkins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 718
29 Validation 721
Creating Validators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
Validating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
Validating Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730
Core Validation Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
31 Collections 737
Quick Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737
List of Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
Iterating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
vi
Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
Working with Tree Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748
Other Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
33 Hash 765
Hash Path Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765
35 Inflector 791
Summary of Inflector Methods and Their Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791
Creating Plural & Singular Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 792
Creating CamelCase and under_scored Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 792
Creating Human Readable Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
Creating Table and Class Name Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
Creating Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
Creating URL Safe Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
Inflection Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
36 Number 795
Formatting Currency Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796
Setting the Default Currency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796
Formatting Floating Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796
Formatting Percentages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
Interacting with Human Readable Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798
Format Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799
Configure formatters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800
38 Text 803
Convert Strings into ASCII . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
Creating URL Safe Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
Generating UUIDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
Simple String Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805
Formatting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805
Wrapping Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806
vii
Highlighting Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806
Removing Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807
Truncating Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807
Truncating the Tail of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808
Extracting an Excerpt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809
Converting an Array to Sentence Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809
40 Xml 819
Loading XML documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
Loading HTML documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
Transforming a XML String in Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
Transforming an Array into a String of XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
42 Chronos 827
44 Migrations 831
45 ElasticSearch 833
46 Appendices 835
3.x Migration Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835
Forwards Compatibility Shimming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885
General Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885
Index 891
viii
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.
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 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;
// Prior to 3.6.0
1
CakePHP Cookbook Documentation, Release 3.8
$users = TableRegistry::get('Users');
$users = TableRegistry::getTableLocator()->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 conven-
tions, CakePHP will use standard classes for table and entity classes that have not yet been defined.
If we wanted to make a new user and save it (with validation) we would do something like:
use Cake\ORM\TableRegistry;
// Prior to 3.6.0
$users = TableRegistry::get('Users');
$users = TableRegistry::getTableLocator()->get('Users');
$user = $users->newEntity(['email' => '[email protected]']);
$users->save($user);
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:
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 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:
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().
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.
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.
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
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.
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.
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/
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.
4 https://github.com/cakephp?tab=members
5 http://discourse.cakephp.org
6 https://stackoverflow.com/questions/tagged/cakephp/
Additional Reading 5
CakePHP Cookbook Documentation, Release 3.8
Brazilian Portuguese
Danish
French
German
Iranian
Dutch
Japanese
Portuguese
Spanish
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, PascalCased, 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.
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' // PascalCased
'plugin' => 'MyPlugin', // PascalCased
'controller' => 'ControllerName', // PascalCased
'action' => 'actionName' // camelBacked
]
For more information on CakePHP URLs and parameter handling, see Connecting Routes.
In general, filenames match the class names, and follow the PSR-4 standard for autoloading. The following are some
examples of class names and their filenames:
• The Controller class LatestArticlesController would be found in a file named LatestArticlesCon-
troller.php
17 https://cakesf.slack.com/messages/spanish/
18 http://groups.google.com/group/cakephp-esp
Additional Reading 7
CakePHP Cookbook Documentation, Release 3.8
Database Conventions
Table names corresponding to CakePHP models are plural and underscored. For example users,
article_categories, and user_favorite_pages respectively.
Field/Column 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 or the bake command won’t work, arranged in alphabetical order (articles_tags rather than
tags_articles). If you need to add additional columns on the junction table you should create a separate en-
tity/table class for that table.
In addition to using an auto-incrementing integer as primary keys, you can also use UUID columns. CakePHP will
create UUID values automatically using (Cake\Utility\Text::uuid()) whenever you save new records using
the Table::save() method.
Model Conventions
Table class names are plural, PascalCased and end in Table. UsersTable, ArticleCategoriesTable,
and UserFavoritePagesTable are all examples of table class names matching the users,
article_categories and user_favorite_pages tables respectively.
Entity class names are singular PascalCased and have no suffix. User, ArticleCategory, and
UserFavoritePage are all examples of entity names matching the users, article_categories and
user_favorite_pages tables respectively.
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.
Note: By default CakePHP uses English inflections. If you have database tables/columns that use an-
other language, you will need to add inflection rules (from singular to plural and vice-versa). You can use
Cake\Utility\Inflector to define your custom inflection rules. See the documentation about Inflector for
more information.
Plugins Conventions
It is useful to prefix a CakePHP plugin with “cakephp-” in the package name. This makes the name semantically
related on the framework it depends on.
Do not use the CakePHP namespace (cakephp) as vendor name as this is reserved to CakePHP owned plugins. The
convention is to use lowercase letters and dashes as separator:
// Bad
cakephp/foo-bar
// Good
your-name/cakephp-foo-bar
Summarized
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 automati-
cally 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 Content Management
Tutorial to see how things fit together.
See awesome list recommendations19 for details.
After you’ve downloaded the CakePHP application skeleton, there are a few top level folders you should see:
• The bin folder holds the Cake console executables.
• The config folder holds the 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 source files will be placed.
19 https://github.com/FriendsOfCake/awesome-cakephp/blob/master/CONTRIBUTING.md#tips-for-creating-cakephp-plugins
Additional Reading 9
CakePHP Cookbook Documentation, Release 3.8
• 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 some-
times session information.
• The vendor folder is where CakePHP and other application dependencies will be installed by Composer20 .
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
publicly 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.
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.
Command Contains your application’s console commands. See Console Commands to learn more.
Console Contains the installation script executed by Composer.
Controller Contains your application’s Controllers and their components.
Locale Stores language files for internationalization.
Middleware Stores any /controllers/middleware for your application.
Model Contains your application’s tables, entities and behaviors.
Shell Contains shell tasks for your application. For more information see Console Tools, Shells & Tasks.
Template Presentational files are placed here: elements, error pages, layouts, and view template files.
View Presentational classes are placed here: views, cells, helpers.
Note: The folders Command and Locale are not there by default. You can add them when you need them.
20 http://getcomposer.org
The best way to experience and learn CakePHP is to sit down and build something. To start off we’ll build a simple
Content Management application.
This tutorial will walk you through the creation of a simple CMS (Content Management System) application. To start
with, we’ll be installing CakePHP, creating our database, and building simple article management.
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, and run SQL snippets from the tutorial. CakePHP will handle building all the
queries your application needs. 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 be the same version your command line interface (CLI) PHP is.
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:
11
CakePHP Cookbook Documentation, Release 3.8
If you downloaded and ran the Composer Windows Installer22 , then type the following line in your terminal from your
installation directory (ie. C:\wamp\www\dev\cakephp3):
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:
/cms
/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.
If you get lost during this tutorial, you can see the finished result on GitHub23 .
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:
21 https://getcomposer.org/download/
22 https://getcomposer.org/Composer-Setup.exe
23 https://github.com/cakephp/cms-tutorial
cd /path/to/our/app
bin/cake server
Note: For Windows, the command needs to be bin\cake server (note the backslash).
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 green chef hats other than CakePHP being able to connect to your
database. If not, you may need to install additional PHP extensions, or set directory permissions.
Next, we will build our Database and create our first model.
Now that we have CakePHP installed, let’s set up the database for our CMS 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_cms. You can execute the
following SQL to create the necessary tables:
USE cake_cms;
INSERT INTO articles (user_id, title, slug, body, published, created, modified)
VALUES
(1, 'First Post', 'first-post', 'This is the first post.', 1, now(), now());
You may have noticed that the articles_tags table used a composite primary key. CakePHP supports composite
primary keys almost everywhere allowing you to have simpler schemas that don’t require additional id columns.
The table and column names we used were not arbitrary. By using CakePHP’s naming conventions, we can leverage
CakePHP more effectively and avoid needing to configure the framework. While CakePHP is flexible enough to
accommodate almost any database schema, adhering to the conventions will save you time as you can leverage the
convention based defaults CakePHP provides.
Database Configuration
Next, let’s tell CakePHP where our database is and how to connect to it. Replace the values in the Datasources.
default array in your config/app.php file with those that apply to your setup. A sample completed configuration
array might look something like the following:
<?php
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_cms',
'encoding' => 'utf8mb4',
'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 green chef hat.
Models are the heart of a CakePHP applications. They enable us to read and modify our data. They allow us to build
relations between our data, validate data, and apply application rules. Models build the foundations necessary to build
our controller actions and templates.
CakePHP’s models are composed of Table and Entity objects. Table objects provide access to the collection
of entities stored in a specific table. They are stored in src/Model/Table. The file we’ll be creating will be saved to
src/Model/Table/ArticlesTable.php. The completed file should look like this:
<?php
// src/Model/Table/ArticlesTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
We’ve attached the Timestamp behavior which will automatically populate the created and modified columns of
our table. By naming our Table object ArticlesTable, CakePHP can use naming conventions to know that our
model uses the articles table. CakePHP also uses conventions to know that the id column is our table’s primary
key.
Note: CakePHP will dynamically create a model object for you if it cannot find a corresponding file in
src/Model/Table. This also means that if you accidentally name your file wrong (i.e. articlestable.php or Arti-
cleTable.php), CakePHP will not recognize any of your settings and will use the generated model instead.
We’ll also create an Entity class for our Articles. Entities represent a single record in the database, and provide row
level behavior for our data. Our entity will be saved to src/Model/Entity/Article.php. The completed file should look
like this:
<?php
// src/Model/Entity/Article.php
namespace App\Model\Entity;
use Cake\ORM\Entity;
Our entity is quite slim right now, and we’ve only setup the _accessible property which controls how properties
can be modified by Mass Assignment.
We can’t do much with our models right now, so next we’ll create our first Controller and Template to allow us to
interact with our model.
With our model created, we need a controller for our articles. Controllers in CakePHP handle HTTP requests and
execute business logic contained in model methods, to prepare the response. We’ll place this new controller in a file
called ArticlesController.php inside the src/Controller directory. Here’s what the basic controller should look like:
<?php
// src/Controller/ArticlesController.php
namespace App\Controller;
Now, let’s add an action to our controller. Actions are controller methods that have routes connected to them. For exam-
ple, when a user requests www.example.com/articles/index (which is also the same as www.example.com/articles),
CakePHP will call the index method of your ArticlesController. This method should query the model layer,
and prepare a response by rendering a Template in the View. The code for that action would look like this:
<?php
// src/Controller/ArticlesController.php
namespace App\Controller;
By defining function index() in our ArticlesController, users can now access the logic there by requesting
www.example.com/articles/index. Similarly, if we were to define a function called foobar(), users would be able
to access that at www.example.com/articles/foobar. You may be tempted to name your controllers and actions in
a way that allows you to obtain specific URLs. Resist that temptation. Instead, follow the CakePHP Conventions
creating readable, meaningful action names. You can then use Routing to connect the URLs you want to the actions
you’ve created.
Our controller action is very simple. It fetches a paginated set of articles from the database, using the Articles Model
that is automatically loaded via naming conventions. It then uses set() to pass the articles into the Template (which
we’ll create soon). CakePHP will automatically render the template after our controller action completes.
Now that we have our controller pulling data from the model, and preparing our view context, let’s create a view
template for our index action.
CakePHP view templates are presentation-flavored PHP code that is inserted inside the application’s layout. While
we’ll be creating HTML here, Views can also generate JSON, CSV or even binary files like PDFs.
A layout is presentation code that is wrapped around a view. Layout files contain common site elements like headers,
footers and navigation elements. Your application can have multiple layouts, and you can switch between them, but
<h1>Articles</h1>
<table>
<tr>
<th>Title</th>
<th>Created</th>
</tr>
<!-- Here is where we iterate through our $articles query object, printing out
˓→ article info -->
</td>
<td>
<?= $article->created->format(DATE_RFC850) ?>
</td>
</tr>
<?php endforeach; ?>
</table>
In the last section we assigned the ‘articles’ variable to the view using set(). Variables passed into the view are
available in the view templates as local variables which we used in the above code.
You might have noticed the use of an object called $this->Html. This is an instance of the CakePHP HtmlHelper.
CakePHP comes with a set of view helpers that make tasks like creating links, forms, and pagination buttons easy.
You can learn more about Helpers in their chapter, but what’s important to note here is that the link() method will
generate an HTML link with the given link text (the first parameter) and URL (the second parameter).
When specifying URLs in CakePHP, it is recommended that you use arrays or named routes. These syntaxes allow
you to leverage the reverse routing features CakePHP offers.
At this point, you should be able to point your browser to http://localhost:8765/articles/index. You should see your
list view, correctly formatted with the title and table listing of the articles.
If you were to click one of the ‘view’ links in our Articles list page, you’d see an error page saying that action hasn’t
been implemented. Lets fix that now:
While this is a simple action, we’ve used some powerful CakePHP features. We start our action off by using
findBySlug() which is a Dynamic Finder. This method allows us to create a basic query that finds articles by
a given slug. We then use firstOrFail() to either fetch the first record, or throw a NotFoundException.
Our action takes a $slug parameter, but where does that parameter come from? If a user requests /articles/
view/first-post, then the value ‘first-post’ is passed as $slug by CakePHP’s routing and dispatching layers.
If we reload our browser with our new action saved, we’d see another CakePHP error page telling us we’re missing a
view template; let’s fix that.
Let’s create the view for our new ‘view’ action and place it in src/Template/Articles/view.ctp
<!-- File: src/Template/Articles/view.ctp -->
You can verify that this is working by trying the links at /articles/index or manually requesting an article by
accessing URLs like /articles/view/first-post.
Adding Articles
With the basic read views created, we need to make it possible for new articles to be created. Start by creating an
add() action in the ArticlesController. Our controller should now look like:
// src/Controller/ArticlesController.php
namespace App\Controller;
use App\Controller\AppController;
$this->loadComponent('Paginator');
$this->loadComponent('Flash'); // Include the FlashComponent
}
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);
}
}
Note: You need to include the Flash component in any controller where you will use it. Often it makes sense to
include it in your AppController.
<h1>Add Article</h1>
<?php
echo $this->Form->create($article);
// Hard code the user for now.
echo $this->Form->control('user_id', ['type' => 'hidden', 'value' => 1]);
echo $this->Form->control('title');
echo $this->Form->control('body', ['rows' => '3']);
echo $this->Form->button(__('Save Article'));
echo $this->Form->end();
?>
We use the FormHelper to generate the opening tag for an HTML form. Here’s the HTML that
$this->Form->create() generates:
Because we called create() without a URL option, FormHelper assumes we want the form to submit back to
the current action.
The $this->Form->control() method is used to create form elements of the same name. The first parameter
tells CakePHP which field they correspond to, and the second parameter allows you to specify a wide array of options
- in this case, the number of rows for the textarea. There’s a bit of introspection and conventions used here. The
control() will output different form elements based on the model field specified, and use inflection to generate
the label text. You can customize the label, the input or any other aspect of the form controls using options. The
$this->Form->end() call closes the form.
Now let’s go back and update our src/Template/Articles/index.ctp view to include a new “Add Article” link. Before
the <table>, add the following line:
If we were to save an Article right now, saving would fail as we are not creating a slug attribute, and the column is
NOT NULL. Slug values are typically a URL-safe version of an article’s title. We can use the beforeSave() callback
of the ORM to populate our slug:
// in src/Model/Table/ArticlesTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
// the Text class
use Cake\Utility\Text;
This code is simple, and doesn’t take into account duplicate slugs. But we’ll fix that later on.
Our application can now save articles, but we can’t edit them. Lets rectify that now. Add the following action to your
ArticlesController:
// in src/Controller/ArticlesController.php
$this->set('article', $article);
}
This action first ensures that the user has tried to access an existing record. If they haven’t passed in an $slug
parameter, or the article does not exist, a NotFoundException will be thrown, and the CakePHP ErrorHandler
will render the appropriate error page.
Next the action checks whether the request is either a POST or a PUT request. If it is, then we use the POST/PUT
data to update our article entity by using the patchEntity() method. Finally, we call save() set the appropriate
flash message and either redirect or display validation errors.
<h1>Edit Article</h1>
<?php
echo $this->Form->create($article);
echo $this->Form->control('user_id', ['type' => 'hidden']);
echo $this->Form->control('title');
echo $this->Form->control('body', ['rows' => '3']);
echo $this->Form->button(__('Save Article'));
echo $this->Form->end();
?>
This template outputs the edit form (with the values populated), along with any necessary validation error messages.
You can now update your index view with links to edit specific articles:
<h1>Articles</h1>
<p><?= $this->Html->link("Add Article", ['action' => 'add']) ?></p>
<table>
<tr>
<th>Title</th>
<th>Created</th>
<th>Action</th>
</tr>
<!-- Here's where we iterate through our $articles query object, printing out article
˓→info -->
</td>
<td>
<?= $article->created->format(DATE_RFC850) ?>
</td>
<td>
<?= $this->Html->link('Edit', ['action' => 'edit', $article->slug]) ?>
</td>
</tr>
<?php endforeach; ?>
</table>
Up until this point our Articles had no input validation done. Lets fix that by using a validator:
// src/Model/Table/ArticlesTable.php
// add this use statement right below the namespace declaration to import
// the Validator class
use Cake\Validation\Validator;
->allowEmptyString('body', false)
->minLength('body', 10);
return $validator;
}
The validationDefault() method tells CakePHP how to validate your data when the save() method is called.
Here, we’ve specified that both the title, and body fields must not be empty, and have certain length constraints.
CakePHP’s validation engine is powerful and flexible. It provides a suite of frequently used rules for tasks like email
addresses, IP addresses etc. and the flexibility for adding your own validation rules. For more information on that
setup, check the Validation documentation.
Now that your validation rules are in place, use the app to try to add an article with an empty title or body to see how
it works. Since we’ve used the Cake\View\Helper\FormHelper::control() method of the FormHelper to
create our form elements, our validation error messages will be shown automatically.
Next, let’s make a way for users to delete articles. Start with a delete() action in the ArticlesController:
// src/Controller/ArticlesController.php
$article = $this->Articles->findBySlug($slug)->firstOrFail();
if ($this->Articles->delete($article)) {
$this->Flash->success(__('The {0} article has been deleted.', $article->
˓→title));
This logic deletes the article specified by $slug, and uses $this->Flash->success() to show the user a
confirmation message after redirecting them to /articles. If the user attempts to delete an article using a GET
request, 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.
Warning: Allowing content to be deleted using GET requests is very dangerous, as web crawlers could acciden-
tally delete all your content. That is why we used allowMethod() in our controller.
Because we’re only executing logic and redirecting to another action, this action has no template. You might want to
update your index template with links that allow users to delete articles:
<!-- File: src/Template/Articles/index.ctp (delete links added) -->
<h1>Articles</h1>
<p><?= $this->Html->link("Add Article", ['action' => 'add']) ?></p>
<table>
<tr>
<th>Title</th>
<th>Created</th>
<th>Action</th>
</tr>
<!-- Here's where we iterate through our $articles query object, printing out article
˓→info -->
</td>
<td>
<?= $article->created->format(DATE_RFC850) ?>
</td>
<td>
<?= $this->Html->link('Edit', ['action' => 'edit', $article->slug]) ?>
<?= $this->Form->postLink(
'Delete',
['action' => 'delete', $article->slug],
['confirm' => 'Are you sure?'])
?>
</td>
</tr>
<?php endforeach; ?>
</table>
Using View\Helper\FormHelper::postLink() will create a link that uses JavaScript to do a POST request
deleting our article.
Note: This view code also uses the FormHelper to prompt the user with a JavaScript confirmation dialog before
they attempt to delete an article.
With a basic articles management setup, we’ll create the basic actions for our Tags and Users tables.
This page summarizes the changes from CakePHP 2.x that will assist in migrating a project to 3.0, as well as a
reference to get up to date with the changes made to the core since the CakePHP 2.x branch. Be sure to read the other
pages in this guide for all the new features and API changes.
Requirements
Warning: CakePHP 3.0 will not work if you do not meet the above requirements.
Upgrade Tool
While this document covers all the breaking changes and improvements made in CakePHP 3.0, we’ve also created a
console application to help you complete some of the time consuming mechanical changes. You can get the upgrade
tool from github26 .
26 https://github.com/cakephp/upgrade
25
CakePHP Cookbook Documentation, Release 3.8
The application directory layout has changed and now follows PSR-427 . You should use the app skeleton28 project as
a reference point when updating your application.
Since CakePHP can no longer be installed via PEAR, or in a shared directory, those options are no longer supported.
Instead you should use Composer29 to install CakePHP into your application.
Namespaces
All of CakePHP’s core classes are now namespaced and follow PSR-4 autoloading specifications. For example
src/Cache/Cache.php is namespaced as Cake\Cache\Cache. Global constants and helper methods like __()
and debug() are not namespaced for convenience sake.
Removed Constants
Configuration
Configuration in CakePHP 3.0 is significantly different than in previous versions. You should read the Configuration
documentation for how configuration is done in 3.0.
You can no longer use App::build() to configure additional class paths. Instead you should map additional paths
using your application’s autoloader. See the section on Additional Class Paths for more information.
Three new configure variables provide the path configuration for plugins, views and locale files. You can add multiple
paths to App.paths.templates, App.paths.plugins, App.paths.locales to configure multiple paths
for templates, plugins and locale files respectively.
The config key www_root has been changed to wwwRoot for consistency. Please adjust your app.php config file as
well as any usage of Configure::read('App.wwwRoot').
27 http://www.php-fig.org/psr/psr-4/
28 https://github.com/cakephp/app
29 http://getcomposer.org
New ORM
CakePHP 3.0 features a new ORM that has been re-built from the ground up. The new ORM is significantly different
and incompatible with the previous one. Upgrading to the new ORM will require extensive changes in any application
that is being upgraded. See the new Database Access & ORM documentation for information on how to use the new
ORM.
Basics
Debugging
• Configure::write('debug', $bool) does not support 0/1/2 anymore. A simple boolean is used
instead to switch debug mode on or off.
Object settings/configuration
• Objects used in CakePHP now have a consistent instance-configuration storage/retrieval system. Code
which previously accessed for example: $object->settings should instead be updated to use
$object->config().
Cache
New ORM 27
CakePHP Cookbook Documentation, Release 3.8
All Cake\Cache\Cache\CacheEngine methods now honor/are responsible for handling the configured key
prefix. The Cake\Cache\CacheEngine::write() no longer permits setting the duration on write - the dura-
tion is taken from the cache engine’s runtime config. Calling a cache method with an empty key will now throw an
InvalidArgumentException, instead of returning false.
Core
App
Plugin
• Cake\Core\Plugin::load() does not setup an autoloader unless you set the autoload option to true.
• When loading plugins you can no longer provide a callable.
• When loading plugins you can no longer provide an array of config files to load.
Configure
Object
The Object class has been removed. It formerly contained a grab bag of methods that were used in various
places across the framework. The most useful of these methods have been extracted into traits. You can use the
Cake\Log\LogTrait to access the log() method. The Cake\Routing\RequestActionTrait provides
requestAction().
Console
The cake executable has been moved from the app/Console directory to the bin directory within the application
skeleton. You can now invoke CakePHP’s console with bin/cake.
TaskCollection Replaced
This class has been renamed to Cake\Console\TaskRegistry. See the section on Registry Objects for more
information on the features provided by the new class. You can use the cake upgrade rename_collections
to assist in upgrading your code. Tasks no longer have access to callbacks, as there were never any callbacks to use.
Shell
ConsoleOptionParser
ConsoleInputArgument
Shell / Task
Shells and Tasks have been moved from Console/Command and Console/Command/Task to Shell and
Shell/Task.
ApiShell Removed
The ApiShell was removed as it didn’t provide any benefit over the file source itself and the online documenta-
tion/API30 .
30 https://api.cakephp.org/
Console 29
CakePHP Cookbook Documentation, Release 3.8
SchemaShell Removed
The SchemaShell was removed as it was never a complete database migration implementation and better tools such
as Phinx31 have emerged. It has been replaced by the CakePHP Migrations Plugin32 which acts as a wrapper between
CakePHP and Phinx33 .
ExtractTask
• bin/cake i18n extract no longer includes untranslated validation messages. If you want translated
validation messages you should wrap those messages in __() calls like any other content.
BakeShell / TemplateTask
• Bake is no longer part of the core source and is superseded by CakePHP Bake Plugin34
• Bake templates have been moved under src/Template/Bake.
• The syntax of Bake templates now uses erb-style tags (<% %>) to denote templating logic, allowing php code
to be treated as plain text.
• The bake view command has been renamed bake template.
Event
The getEventManager() method, was removed on all objects that had it. An eventManager() method is
now provided by the EventManagerTrait. The EventManagerTrait contains the logic of instantiating and
keeping a reference to a local event manager.
The Event subsystem has had a number of optional features removed. When dispatching events you can no longer use
the following options:
• passParams This option is now enabled always implicitly. You cannot turn it off.
• break This option has been removed. You must now stop events.
• breakOn This option has been removed. You must now stop events.
Log
• Log configurations are now immutable. If you need to change configuration you must first drop the configuration
and then re-create it. This prevents synchronization issues with configuration options.
• Log engines are now lazily loaded upon the first write to the logs.
• Cake\Log\Log::engine() has been added.
• The following methods have been removed from Cake\Log\Log :: defaultLevels(), enabled(),
enable(), disable().
• You can no longer create custom levels using Log::levels().
31 https://phinx.org/
32 https://github.com/cakephp/migrations
33 https://phinx.org/
34 https://github.com/cakephp/bake
Routing
Named Parameters
Named parameters were removed in 3.0. Named parameters were added in 1.2.0 as a ‘pretty’ version of query string
parameters. While the visual benefit is arguable, the problems named parameters created are not.
Named parameters required special handling in CakePHP as well as any PHP or JavaScript library that needed to
interact with them, as named parameters are not implemented or understood by any library except CakePHP. The
additional complexity and code required to support named parameters did not justify their existence, and they have
been removed. In their place you should use standard query string parameters or passed arguments. By default
Router will treat any additional parameters to Router::url() as query string arguments.
Since many applications will still need to parse incoming URLs containing named parameters.
Cake\Routing\Router::parseNamedParams() has been added to allow backwards compatibility
with existing URLs.
RequestActionTrait
Router
• Named parameters have been removed, see above for more information.
• The full_base option has been replaced with the _full option.
• The ext option has been replaced with the _ext option.
• _scheme, _port, _host, _base, _full, _ext options added.
• String URLs are no longer modified by adding the plugin/controller/prefix names.
• The default fallback route handling was removed. If no routes match a parameter set / will be returned.
Routing 31
CakePHP Cookbook Documentation, Release 3.8
• Route classes are responsible for all URL generation including query string parameters. This makes routes far
more powerful and flexible.
• Persistent parameters were removed. They were replaced with Cake\Routing\Router::urlFilter()
which allows a more flexible way to mutate URLs being reverse routed.
• Router::parseExtensions() has been removed. Use Cake\Routing\Router::extensions()
instead. This method must be called before routes are connected. It won’t modify existing routes.
• Router::setExtensions() has been removed. Use Cake\Routing\Router::extensions() in-
stead.
• Router::resourceMap() has been removed.
• The [method] option has been renamed to _method.
• The ability to match arbitrary headers with [] style parameters has been removed. If you need to parse/match
on arbitrary conditions consider using custom route classes.
• Router::promote() has been removed.
• Router::parse() will now raise an exception when a URL cannot be handled by any route.
• Router::url() will now raise an exception when no route matches a set of parameters.
• Routing scopes have been introduced. Routing scopes allow you to keep your routes file DRY and give Router
hints on how to optimize parsing & reverse routing URLs.
Route
Dispatcher filters are no longer added to your application using Configure. You now append them with
Cake\Routing\DispatcherFactory. This means if your application used Dispatcher.filters, you
should now use Cake\Routing\DispatcherFactory::add().
In addition to configuration changes, dispatcher filters have had some conventions updated, and features added. See
the Dispatcher Filters documentation for more information.
FilterAssetFilter
• Plugin & theme assets handled by the AssetFilter are no longer read via include instead they are treated as
plain text files. This fixes a number of issues with JavaScript libraries like TinyMCE and environments with
short_tags enabled.
• Support for the Asset.filter configuration and hooks were removed. This feature should be replaced with
a plugin or dispatcher filter.
Network
Request
Response
• The mapping of mimetype text/plain to extension csv has been removed. As a consequence
Cake\Controller\Component\RequestHandlerComponent doesn’t set extension to csv if
Accept header contains mimetype text/plain which was a common annoyance when receiving a jQuery
XHR request.
Sessions
The session class is no longer static, instead the session can be accessed through the request object. See the Sessions
documentation for using the session object.
• Cake\Network\Session and related session classes have been moved under the Cake\Network names-
pace.
• SessionHandlerInterface has been removed in favor of the one provided by PHP itself.
• The property Session::$requestCountdown has been removed.
• The session checkAgent feature has been removed. It caused a number of bugs when chrome frame, and flash
player are involved.
• The conventional sessions database table name is now sessions instead of cake_sessions.
• The session cookie timeout is automatically updated in tandem with the timeout in the session data.
• The path for session cookie now defaults to app’s base path instead of “/”. A new configuration variable
Session.cookiePath has been added to customize the cookie path.
• A new convenience method Cake\Network\Session::consume() has been added to allow reading and
deleting session data in a single step.
Network 33
CakePHP Cookbook Documentation, Release 3.8
• The default value of Cake\Network\Session::clear()’s argument $renew has been changed from
true to false.
Network\Http
Network\Email
Controller
Controller
• The $helpers, $components properties are now merged with all parent classes not just AppController
and the plugin AppController. The properties are merged differently now as well. Instead of all settings in all
classes being merged together, the configuration defined in the child class will be used. This means that if you
have some configuration defined in your AppController, and some configuration defined in a subclass, only the
configuration in the subclass will be used.
• Controller::httpCodes() has been removed, use Cake\Network\Response::httpCodes()
instead.
• Controller::disableCache() has been removed, use Cake\Network\Response::disableCache()
instead.
• Controller::flash() has been removed. This method was rarely used in real applications and served no
purpose anymore.
• Controller::validate() and Controller::validationErrors() have been removed. They
were left over methods from the 1.x days where the concerns of models + controllers were far more intertwined.
• Controller::loadModel() now loads table objects.
• The Controller::$scaffold property has been removed. Dynamic scaffolding has been removed
from CakePHP core. An improved scaffolding plugin, named CRUD, can be found here: https://github.com/
FriendsOfCake/crud
• The Controller::$ext property has been removed. You now have to extend and override the
View::$_ext property if you want to use a non-default view file extension.
• The Controller::$methods property has been removed. You should now use
Controller::isAction() to determine whether or not a method name is an action. This change
was made to allow easier customization of what is and is not counted as an action.
• The Controller::$Components property has been removed and replaced with _components. If you
need to load components at runtime you should use $this->loadComponent() on your controller.
• The signature of Cake\Controller\Controller::redirect() has been changed to
Controller::redirect(string|array $url, int $status = null). The 3rd argu-
ment $exit has been dropped. The method can no longer send response and exit script, instead it returns a
Response instance with appropriate headers set.
• The base, webroot, here, data, action, and params magic properties have been removed. You should
access all of these properties on $this->request instead.
• Underscore prefixed controller methods like _someMethod() are no longer treated as private methods. Use
proper visibility keywords instead. Only public methods can be used as controller actions.
Scaffold Removed
The dynamic scaffolding in CakePHP has been removed from CakePHP core. It was infrequently used, and never
intended for production use. An improved scaffolding plugin, named CRUD, can be found here: https://github.com/
FriendsOfCake/crud
ComponentCollection Replaced
This class has been renamed to Cake\Controller\ComponentRegistry. See the section on Registry
Objects for more information on the features provided by the new class. You can use the cake upgrade
rename_collections to assist in upgrading your code.
Component
Controller 35
CakePHP Cookbook Documentation, Release 3.8
Controller\Components
CookieComponent
• Uses Cake\Network\Request::cookie() to read cookie data, this eases testing, and allows for Con-
trollerTestCase to set cookies.
• Cookies encrypted in previous versions of CakePHP using the cipher() method are now un-readable because
Security::cipher() has been removed. You will need to re-encrypt cookies with the rijndael() or
aes() method before upgrading.
• CookieComponent::type() has been removed and replaced with configuration data accessed through
config().
• write() no longer takes encryption or expires parameters. Both of these are now managed through
config data. See Cookie for more information.
• The path for cookies now defaults to app’s base path instead of “/”.
AuthComponent
• Default is now the default password hasher used by authentication classes. It uses exclusively the bcrypt
hashing algorithm. If you want to continue using SHA1 hashing used in 2.x use 'passwordHasher' =>
'Weak' in your authenticator configuration.
• A new FallbackPasswordHasher was added to help users migrate old passwords from one algorithm to
another. Check AuthComponent’s documentation for more info.
• BlowfishAuthenticate class has been removed. Just use FormAuthenticate
• BlowfishPasswordHasher class has been removed. Use DefaultPasswordHasher instead.
• The loggedIn() method has been removed. Use user() instead.
• Configuration options are no longer set as public properties.
• The methods allow() and deny() no longer accept “var args”. All method names need to be passed as first
argument, either as string or array of strings.
• The method login() has been removed and replaced by setUser() instead. To login a user you now have
to call identify() which returns user info upon successful identification and then use setUser() to save
the info to session for persistence across requests.
• BaseAuthenticate::_password() has been removed. Use a PasswordHasher class instead.
• BaseAuthenticate::logout() has been removed.
• AuthComponent now triggers two events Auth.afterIdentify and Auth.logout after a user has
been identified and before a user is logged out respectively. You can set callback functions for these events by
returning a mapping array from implementedEvents() method of your authenticate class.
ACL related classes were moved to a separate plugin. Password hashers, Authentication and Authorization providers
where moved to the \Cake\Auth namespace. You are required to move your providers and hashers to the
App\Auth namespace as well.
RequestHandlerComponent
• The following methods have been removed from RequestHandler component:: isAjax(),
isFlash(), isSSL(), isPut(), isPost(), isGet(), isDelete(). Use the
Cake\Network\Request::is() method instead with relevant argument.
• RequestHandler::setContent() was removed, use Cake\Network\Response::type() in-
stead.
• RequestHandler::getReferer() was removed, use Cake\Network\Request::referer() in-
stead.
• RequestHandler::getClientIP() was removed, use Cake\Network\Request::clientIp()
instead.
• RequestHandler::getAjaxVersion() was removed.
• RequestHandler::mapType() was removed, use Cake\Network\Response::mapType() in-
stead.
• Configuration options are no longer set as public properties.
SecurityComponent
• The following methods and their related properties have been removed from Security compo-
nent: requirePost(), requireGet(), requirePut(), requireDelete(). Use the
Cake\Network\Request::allowMethod() instead.
• SecurityComponent::$disabledFields() has been removed, use
SecurityComponent::$unlockedFields().
• The CSRF related features in SecurityComponent have been extracted and moved into a separate CsrfCompo-
nent. This allows you to use CSRF protection without having to use form tampering prevention.
• Configuration options are no longer set as public properties.
• The methods requireAuth() and requireSecure() no longer accept “var args”. All method names
need to be passed as first argument, either as string or array of strings.
SessionComponent
Error
Custom ExceptionRenderers are now expected to either return a Cake\Network\Response object or string when
rendering errors. This means that any methods handling specific exceptions must return a response or string value.
Model
The Model layer in 2.x has been entirely re-written and replaced. You should review the New ORM Upgrade Guide
for information on how to use the new ORM.
• The Model class has been removed.
• The BehaviorCollection class has been removed.
Model 37
CakePHP Cookbook Documentation, Release 3.8
ConnectionManager
Behaviors
• Underscore prefixed behavior methods like _someMethod() are no longer treated as private methods. Use
proper visibility keywords instead.
TreeBehavior
The TreeBehavior was completely re-written to use the new ORM. Although it works the same as in 2.x, a few methods
were renamed or removed:
• TreeBehavior::children() is now a custom finder find('children').
• TreeBehavior::generateTreeList() is now a custom finder find('treeList').
• TreeBehavior::getParentNode() was removed.
• TreeBehavior::getPath() is now a custom finder find('path').
• TreeBehavior::reorder() was removed.
• TreeBehavior::verify() was removed.
TestSuite
TestCase
• _normalizePath() has been added to allow path comparison tests to run across all operation systems
regarding their DS settings (\ in Windows vs / in UNIX, for example).
The following assertion methods have been removed as they have long been deprecated and replaced by their new
PHPUnit counterpart:
• assertEqual() in favor of assertEquals()
• assertNotEqual() in favor of assertNotEquals()
• assertIdentical() in favor of assertSame()
• assertNotIdentical() in favor of assertNotSame()
• assertPattern() in favor of assertRegExp()
• assertNoPattern() in favor of assertNotRegExp()
• assertReference() if favor of assertSame()
• assertIsA() in favor of assertInstanceOf()
Note that some methods have switched the argument order, e.g. assertEqual($is, $expected) should now
be assertEquals($expected, $is).
The following assertion methods have been deprecated and will be removed in the future:
• assertWithinMargin() in favor of assertWithinRange()
• assertTags() in favor of assertHtml()
Both method replacements also switched the argument order for a consistent assert method API with $expected as
first argument.
The following assertion methods have been added:
• assertNotWithinRange() as counter part to assertWithinRange()
View
Having themes and plugins as ways to create modular application components has proven to be limited, and confusing.
In CakePHP 3.0, themes no longer reside inside the application. Instead they are standalone plugins. This solves a
few problems with themes:
• You could not put themes in plugins.
• Themes could not provide helpers, or custom view classes.
Both these issues are solved by converting themes into plugins.
The folders containing view files now go under src/Template instead of src/View. This was done to separate the view
files from files containing php classes (eg. Helpers, View classes).
The following View folders have been renamed to avoid naming collisions with controller names:
• Layouts is now Layout
View 39
CakePHP Cookbook Documentation, Release 3.8
HelperCollection Replaced
This class has been renamed to Cake\View\HelperRegistry. See the section on Registry Objects for more
information on the features provided by the new class. You can use the cake upgrade rename_collections
to assist in upgrading your code.
View Class
• The plugin key has been removed from $options argument of Cake\View\View::element(). Spec-
ify the element name as SomePlugin.element_name instead.
• View::getVar() has been removed, use Cake\View\View::get() instead.
• View::$ext has been removed and instead a protected property View::$_ext has been added.
• View::addScript() has been removed. Use Using View Blocks instead.
• The base, webroot, here, data, action, and params magic properties have been removed. You should
access all of these properties on $this->request instead.
• View::start() no longer appends to an existing block. Instead it will overwrite the block content when end
is called. If you need to combine block contents you should fetch the block content when calling start a second
time, or use the capturing mode of append().
• View::prepend() no longer has a capturing mode.
• View::startIfEmpty() has been removed. Now that start() always overwrites startIfEmpty serves no
purpose.
• The View::$Helpers property has been removed and replaced with _helpers. If you need to load helpers
at runtime you should use $this->addHelper() in your view files.
• View will now raise Cake\View\Exception\MissingTemplateException when templates are
missing instead of MissingViewException.
ViewBlock
JsonView
• By default JSON data will have HTML entities encoded now. This prevents possible XSS issues when JSON
view content is embedded in HTML files.
• Cake\View\JsonView now supports the _jsonOptions view variable. This allows you to configure the
bit-mask options used when generating JSON.
XmlView
• Cake\View\XmlView now supports the _xmlOptions view variable. This allows you to configure the
options used when generating XML.
View\Helper
• The $settings property is now called $_config and should be accessed through the config() method.
• Configuration options are no longer set as public properties.
• Helper::clean() was removed. It was never robust enough to fully prevent XSS. instead you should escape
content with h or use a dedicated library like htmlPurifier.
• Helper::output() was removed. This method was deprecated in 2.x.
• Methods Helper::webroot(), Helper::url(), Helper::assetUrl(),
Helper::assetTimestamp() have been moved to new Cake\View\Helper\UrlHelper helper.
Helper::url() is now available as Cake\View\Helper\UrlHelper::build().
• Magic accessors to deprecated properties have been removed. The following properties now need to be accessed
from the request object:
– base
– here
– webroot
– data
– action
– params
Helper
View\Helper 41
CakePHP Cookbook Documentation, Release 3.8
• Helper::_formatAttribute()
These methods can now be found on the StringTemplate class that helpers frequently use. See the
StringTemplateTrait for an easy way to integrate string templates into your own helpers.
FormHelper
FormHelper has been entirely rewritten for 3.0. It features a few large changes:
• FormHelper works with the new ORM. But has an extensible system for integrating with other ORMs or data-
sources.
• FormHelper features an extensible widget system that allows you to create new custom input widgets and aug-
ment the built-in ones.
• String templates are the foundation of the helper. Instead of munging arrays together everywhere, most of the
HTML FormHelper generates can be customized in one central place using template sets.
In addition to these larger changes, some smaller breaking changes have been made as well. These changes should
help streamline the HTML FormHelper generates and reduce the problems people had in the past:
• The data[ prefix was removed from all generated inputs. The prefix serves no real purpose anymore.
• The various standalone input methods like text(), select() and others no longer generate id attributes.
• The inputDefaults option has been removed from create().
• Options default and onsubmit of create() have been removed. Instead one should use JavaScript event
binding or set all required js code for onsubmit.
• end() can no longer make buttons. You should create buttons with button() or submit().
• FormHelper::tagIsInvalid() has been removed. Use isFieldError() instead.
• FormHelper::inputDefaults() has been removed. You can use templates() to define/augment the
templates FormHelper uses.
• The wrap and class options have been removed from the error() method.
• The showParents option has been removed from select().
• The div, before, after, between and errorMessage options have been removed from input(). You
can use templates to update the wrapping HTML. The templates option allows you to override the loaded
templates for one input.
• The separator, between, and legend options have been removed from radio(). You can use templates
to change the wrapping HTML now.
• The format24Hours parameter has been removed from hour(). It has been replaced with the format
option.
• The minYear, and maxYear parameters have been removed from year(). Both of these parameters can
now be provided as options.
• The dateFormat and timeFormat parameters have been removed from datetime(). You can use the
template to define the order the inputs should be displayed in.
• The submit() has had the div, before and after options removed. You can customize the
submitContainer template to modify this content.
• The inputs() method no longer accepts legend and fieldset in the $fields parameter, you must
use the $options parameter. It now also requires $fields parameter to be an array. The $blacklist
parameter has been removed, the functionality has been replaced by specifying 'field' => false in the
$fields parameter.
• The inline parameter has been removed from postLink() method. You should use the block option instead.
Setting block => true will emulate the previous behavior.
• The timeFormat parameter for hour(), time() and dateTime() now defaults to 24, complying with
ISO 8601.
• The $confirmMessage argument of Cake\View\Helper\FormHelper::postLink() has been re-
moved. You should now use key confirm in $options to specify the message.
• Checkbox and radio input types are now rendered inside of label elements by default. This helps increase
compatibility with popular CSS libraries like Bootstrap35 and Foundation36 .
• Templates tags are now all camelBacked. Pre-3.0 tags formstart, formend, hiddenblock and
inputsubmit are now formStart, formEnd, hiddenBlock and inputSubmit. Make sure you
change them if they are customized in your app.
It is recommended that you review the Form documentation for more details on how to use the FormHelper in 3.0.
HtmlHelper
PaginatorHelper
• link() has been removed. It was no longer used by the helper internally. It had low usage in user land code,
and no longer fit the goals of the helper.
• next() no longer has ‘class’, or ‘tag’ options. It no longer has disabled arguments. Instead templates are used.
• prev() no longer has ‘class’, or ‘tag’ options. It no longer has disabled arguments. Instead templates are used.
• first() no longer has ‘after’, ‘ellipsis’, ‘separator’, ‘class’, or ‘tag’ options.
• last() no longer has ‘after’, ‘ellipsis’, ‘separator’, ‘class’, or ‘tag’ options.
• numbers() no longer has ‘separator’, ‘tag’, ‘currentTag’, ‘currentClass’, ‘class’, ‘tag’, ‘ellipsis’ options.
These options are now facilitated through templates. It also requires the $options parameter to be an ar-
ray now.
35 http://getbootstrap.com/
36 http://foundation.zurb.com/
View\Helper 43
CakePHP Cookbook Documentation, Release 3.8
TimeHelper
NumberHelper
SessionHelper
• The SessionHelper has been deprecated. You can use $this->request->session() directly, and
the flash message functionality has been moved into Flash instead.
JsHelper
• JsHelper and all associated engines have been removed. It could only generate a very small subset of
JavaScript code for selected library and hence trying to generate all JavaScript code using just the helper of-
ten became an impediment. It’s now recommended to directly use JavaScript library of your choice.
CacheHelper Removed
CacheHelper has been removed. The caching functionality it provided was non-standard, limited and incompatible
with non-HTML layouts and data views. These limitations meant a full rebuild would be necessary. Edge Side
Includes have become a standardized way to implement the functionality CacheHelper used to provide. However,
implementing Edge Side Includes37 in PHP has a number of limitations and edge cases. Instead of building a sub-par
solution, we recommend that developers needing full response caching use Varnish38 or Squid39 instead.
I18n
The I18n subsystem was completely rewritten. In general, you can expect the same behavior as in previous versions,
specifically if you are using the __() family of functions.
37 http://en.wikipedia.org/wiki/Edge_Side_Includes
38 http://varnish-cache.org
39 http://squid-cache.org
Internally, the I18n class uses Aura\Intl, and appropriate methods are exposed to access the specific features of
this library. For this reason most methods inside I18n were removed or renamed.
Due to the use of ext/intl, the L10n class was completely removed. It provided outdated and incomplete data in
comparison to the data available from the Locale class in PHP.
The default application language will no longer be changed automatically by the browser accepted language nor by
having the Config.language value set in the browser session. You can, however, use a dispatcher filter to get
automatic language switching from the Accept-Language header sent by the browser:
// In config/bootstrap.php
DispatcherFactory::addFilter('LocaleSelector');
There is no built-in replacement for automatically selecting the language by setting a value in the user session.
The default formatting function for translated messages is no longer sprintf, but the more advanced and feature
rich MessageFormatter class. In general you can rewrite placeholders in messages as follows:
// Before:
__('Today is a %s day in %s', 'Sunny', 'Spain');
// After:
__('Today is a {0} day in {1}', 'Sunny', 'Spain');
You can avoid rewriting your messages by using the old sprintf formatter:
I18n::defaultFormatter('sprintf');
Additionally, the Config.language value was removed and it can no longer be used to control the current language
of the application. Instead, you can use the I18n class:
// Before
Configure::write('Config.language', 'fr_FR');
// Now
I18n::setLocale('en_US');
L10n
L10n 45
CakePHP Cookbook Documentation, Release 3.8
Testing
• The TestShell has been removed. CakePHP, the application skeleton and newly baked plugins all use
phpunit to run tests.
• The webrunner (webroot/test.php) has been removed. CLI adoption has greatly increased since the initial release
of 2.x. Additionaly, CLI runners offer superior integration with IDE’s and other automated tooling.
If you find yourself in need of a way to run tests from a browser you should checkout VisualPHPUnit40 . It offers
many additional features over the old webrunner.
• ControllerTestCase is deprecated and will be removed for CakePHP 3.0.0. You should use the new
Controller Integration Testing features instead.
• Fixtures should now be referenced using their plural form:
// Instead of
$fixtures = ['app.article'];
Utility
The Set class has been removed, you should use the Hash class instead now.
Inflector
// Instead of
Inflector::rules('transliteration', [
'/ä|æ/' => 'ae',
'/å/' => 'aa'
]);
40 https://github.com/NSinopoli/VisualPHPUnit
• Separate set of uninflected and irregular rules for pluralization and singularization have been removed. Instead
we now have a common list for each. When using Cake\Utility\Inflector::rules() with type
‘singular’ and ‘plural’ you can no longer use keys like ‘uninflected’, ‘irregular’ in $rules argument array.
You can add / overwrite the list of uninflected and irregular rules using
Cake\Utility\Inflector::rules() by using values ‘uninflected’ and ‘irregular’ for $type
argument.
Sanitize
Security
• Security::cipher() has been removed. It is insecure and promoted bad cryptographic practices. You
should use Security::encrypt() instead.
• The Configure value Security.cipherSeed is no longer required. With the removal of
Security::cipher() it serves no use.
• Backwards compatibility in Cake\Utility\Security::rijndael() for values encrypted prior to
CakePHP 2.3.1 has been removed. You should re-encrypt values using Security::encrypt() and a recent
version of CakePHP 2.x before migrating.
• The ability to generate a blowfish hash has been removed. You can no longer use type “blowfish” for
Security::hash(). One should just use PHP’s password_hash() and password_verify() to generate and
verify blowfish hashes. The compability library ircmaxell/password-compat41 which is installed along with
CakePHP provides these functions for PHP < 5.5.
• OpenSSL is now used over mcrypt when encrypting/decrypting data. This change provides better performance
and future proofs CakePHP against distros dropping support for mcrypt.
• Security::rijndael() is deprecated and only available when using mcrypt.
Warning: Data encrypted with Security::encrypt() in previous versions is not compatible with the openssl imple-
mentation. You should set the implementation to mcrypt when upgrading.
Time
Utility 47
CakePHP Cookbook Documentation, Release 3.8
CakeTime::aMethod($date);
(new Time($date))->aMethod();
Number
The Number library was rewritten to internally use the NumberFormatter class.
• CakeNumber has been renamed to Cake\I18n\Number.
• Number::format() now requires $options to be an array.
• Number::addFormat() was removed.
• Number::fromReadableSize() has been moved to Cake\Utility\Text::parseFileSize().
Validation
• The range for Validation::range() now is inclusive if $lower and $upper are provided.
• Validation::ssn() has been removed.
Xml
In this section, you can walk through typical CakePHP applications to see how all of the pieces come together.
Alternatively, you can refer to the non-official CakePHP plugin repository CakePackages42 and the Bakery43 for exist-
ing applications and components.
This tutorial will walk you through the creation of a simple CMS application. To start with, we’ll be installing
CakePHP, creating our database, and building simple article management.
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, and run SQL snippets from the tutorial. CakePHP will handle building all the
queries your application needs. 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 be the same version your command line interface (CLI) PHP is.
42 https://plugins.cakephp.org/
43 https://bakery.cakephp.org/
49
CakePHP Cookbook Documentation, Release 3.8
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:
If you downloaded and ran the Composer Windows Installer45 , then type the following line in your terminal from your
installation directory (ie. C:\wamp\www\dev\cakephp3):
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:
/cms
/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.
If you get lost during this tutorial, you can see the finished result on GitHub46 .
44 https://getcomposer.org/download/
45 https://getcomposer.org/Composer-Setup.exe
46 https://github.com/cakephp/cms-tutorial
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:
cd /path/to/our/app
bin/cake server
Note: For Windows, the command needs to be bin\cake server (note the backslash).
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 green chef hats other than CakePHP being able to connect to your
database. If not, you may need to install additional PHP extensions, or set directory permissions.
Next, we will build our Database and create our first model.
Now that we have CakePHP installed, let’s set up the database for our CMS 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_cms. You can execute the
following SQL to create the necessary tables:
USE cake_cms;
INSERT INTO articles (user_id, title, slug, body, published, created, modified)
VALUES
(1, 'First Post', 'first-post', 'This is the first post.', 1, now(), now());
You may have noticed that the articles_tags table used a composite primary key. CakePHP supports composite
primary keys almost everywhere allowing you to have simpler schemas that don’t require additional id columns.
The table and column names we used were not arbitrary. By using CakePHP’s naming conventions, we can leverage
CakePHP more effectively and avoid needing to configure the framework. While CakePHP is flexible enough to
accommodate almost any database schema, adhering to the conventions will save you time as you can leverage the
convention based defaults CakePHP provides.
Database Configuration
Next, let’s tell CakePHP where our database is and how to connect to it. Replace the values in the Datasources.
default array in your config/app.php file with those that apply to your setup. A sample completed configuration
array might look something like the following:
<?php
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_cms',
'encoding' => 'utf8mb4',
'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 green chef hat.
Models are the heart of a CakePHP applications. They enable us to read and modify our data. They allow us to build
relations between our data, validate data, and apply application rules. Models build the foundations necessary to build
our controller actions and templates.
CakePHP’s models are composed of Table and Entity objects. Table objects provide access to the collection
of entities stored in a specific table. They are stored in src/Model/Table. The file we’ll be creating will be saved to
src/Model/Table/ArticlesTable.php. The completed file should look like this:
<?php
// src/Model/Table/ArticlesTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
We’ve attached the Timestamp behavior which will automatically populate the created and modified columns of
our table. By naming our Table object ArticlesTable, CakePHP can use naming conventions to know that our
model uses the articles table. CakePHP also uses conventions to know that the id column is our table’s primary
key.
Note: CakePHP will dynamically create a model object for you if it cannot find a corresponding file in
src/Model/Table. This also means that if you accidentally name your file wrong (i.e. articlestable.php or Arti-
cleTable.php), CakePHP will not recognize any of your settings and will use the generated model instead.
We’ll also create an Entity class for our Articles. Entities represent a single record in the database, and provide row
level behavior for our data. Our entity will be saved to src/Model/Entity/Article.php. The completed file should look
like this:
<?php
// src/Model/Entity/Article.php
namespace App\Model\Entity;
use Cake\ORM\Entity;
Our entity is quite slim right now, and we’ve only setup the _accessible property which controls how properties
can be modified by Mass Assignment.
We can’t do much with our models right now, so next we’ll create our first Controller and Template to allow us to
interact with our model.
With our model created, we need a controller for our articles. Controllers in CakePHP handle HTTP requests and
execute business logic contained in model methods, to prepare the response. We’ll place this new controller in a file
called ArticlesController.php inside the src/Controller directory. Here’s what the basic controller should look like:
<?php
// src/Controller/ArticlesController.php
namespace App\Controller;
Now, let’s add an action to our controller. Actions are controller methods that have routes connected to them. For exam-
ple, when a user requests www.example.com/articles/index (which is also the same as www.example.com/articles),
CakePHP will call the index method of your ArticlesController. This method should query the model layer,
and prepare a response by rendering a Template in the View. The code for that action would look like this:
<?php
// src/Controller/ArticlesController.php
namespace App\Controller;
By defining function index() in our ArticlesController, users can now access the logic there by requesting
www.example.com/articles/index. Similarly, if we were to define a function called foobar(), users would be able
to access that at www.example.com/articles/foobar. You may be tempted to name your controllers and actions in
a way that allows you to obtain specific URLs. Resist that temptation. Instead, follow the CakePHP Conventions
creating readable, meaningful action names. You can then use Routing to connect the URLs you want to the actions
you’ve created.
Our controller action is very simple. It fetches a paginated set of articles from the database, using the Articles Model
that is automatically loaded via naming conventions. It then uses set() to pass the articles into the Template (which
we’ll create soon). CakePHP will automatically render the template after our controller action completes.
Now that we have our controller pulling data from the model, and preparing our view context, let’s create a view
template for our index action.
CakePHP view templates are presentation-flavored PHP code that is inserted inside the application’s layout. While
we’ll be creating HTML here, Views can also generate JSON, CSV or even binary files like PDFs.
A layout is presentation code that is wrapped around a view. Layout files contain common site elements like headers,
footers and navigation elements. Your application can have multiple layouts, and you can switch between them, but
<h1>Articles</h1>
<table>
<tr>
<th>Title</th>
<th>Created</th>
</tr>
<!-- Here is where we iterate through our $articles query object, printing out
˓→ article info -->
</td>
<td>
<?= $article->created->format(DATE_RFC850) ?>
</td>
</tr>
<?php endforeach; ?>
</table>
In the last section we assigned the ‘articles’ variable to the view using set(). Variables passed into the view are
available in the view templates as local variables which we used in the above code.
You might have noticed the use of an object called $this->Html. This is an instance of the CakePHP HtmlHelper.
CakePHP comes with a set of view helpers that make tasks like creating links, forms, and pagination buttons easy.
You can learn more about Helpers in their chapter, but what’s important to note here is that the link() method will
generate an HTML link with the given link text (the first parameter) and URL (the second parameter).
When specifying URLs in CakePHP, it is recommended that you use arrays or named routes. These syntaxes allow
you to leverage the reverse routing features CakePHP offers.
At this point, you should be able to point your browser to http://localhost:8765/articles/index. You should see your
list view, correctly formatted with the title and table listing of the articles.
If you were to click one of the ‘view’ links in our Articles list page, you’d see an error page saying that action hasn’t
been implemented. Lets fix that now:
While this is a simple action, we’ve used some powerful CakePHP features. We start our action off by using
findBySlug() which is a Dynamic Finder. This method allows us to create a basic query that finds articles by
a given slug. We then use firstOrFail() to either fetch the first record, or throw a NotFoundException.
Our action takes a $slug parameter, but where does that parameter come from? If a user requests /articles/
view/first-post, then the value ‘first-post’ is passed as $slug by CakePHP’s routing and dispatching layers.
If we reload our browser with our new action saved, we’d see another CakePHP error page telling us we’re missing a
view template; let’s fix that.
Let’s create the view for our new ‘view’ action and place it in src/Template/Articles/view.ctp
<!-- File: src/Template/Articles/view.ctp -->
You can verify that this is working by trying the links at /articles/index or manually requesting an article by
accessing URLs like /articles/view/first-post.
Adding Articles
With the basic read views created, we need to make it possible for new articles to be created. Start by creating an
add() action in the ArticlesController. Our controller should now look like:
// src/Controller/ArticlesController.php
namespace App\Controller;
use App\Controller\AppController;
$this->loadComponent('Paginator');
$this->loadComponent('Flash'); // Include the FlashComponent
}
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);
}
}
Note: You need to include the Flash component in any controller where you will use it. Often it makes sense to
include it in your AppController.
<h1>Add Article</h1>
<?php
echo $this->Form->create($article);
// Hard code the user for now.
echo $this->Form->control('user_id', ['type' => 'hidden', 'value' => 1]);
echo $this->Form->control('title');
echo $this->Form->control('body', ['rows' => '3']);
echo $this->Form->button(__('Save Article'));
echo $this->Form->end();
?>
We use the FormHelper to generate the opening tag for an HTML form. Here’s the HTML that
$this->Form->create() generates:
Because we called create() without a URL option, FormHelper assumes we want the form to submit back to
the current action.
The $this->Form->control() method is used to create form elements of the same name. The first parameter
tells CakePHP which field they correspond to, and the second parameter allows you to specify a wide array of options
- in this case, the number of rows for the textarea. There’s a bit of introspection and conventions used here. The
control() will output different form elements based on the model field specified, and use inflection to generate
the label text. You can customize the label, the input or any other aspect of the form controls using options. The
$this->Form->end() call closes the form.
Now let’s go back and update our src/Template/Articles/index.ctp view to include a new “Add Article” link. Before
the <table>, add the following line:
If we were to save an Article right now, saving would fail as we are not creating a slug attribute, and the column is
NOT NULL. Slug values are typically a URL-safe version of an article’s title. We can use the beforeSave() callback
of the ORM to populate our slug:
// in src/Model/Table/ArticlesTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
// the Text class
use Cake\Utility\Text;
This code is simple, and doesn’t take into account duplicate slugs. But we’ll fix that later on.
Our application can now save articles, but we can’t edit them. Lets rectify that now. Add the following action to your
ArticlesController:
// in src/Controller/ArticlesController.php
$this->set('article', $article);
}
This action first ensures that the user has tried to access an existing record. If they haven’t passed in an $slug
parameter, or the article does not exist, a NotFoundException will be thrown, and the CakePHP ErrorHandler
will render the appropriate error page.
Next the action checks whether the request is either a POST or a PUT request. If it is, then we use the POST/PUT
data to update our article entity by using the patchEntity() method. Finally, we call save() set the appropriate
flash message and either redirect or display validation errors.
<h1>Edit Article</h1>
<?php
echo $this->Form->create($article);
echo $this->Form->control('user_id', ['type' => 'hidden']);
echo $this->Form->control('title');
echo $this->Form->control('body', ['rows' => '3']);
echo $this->Form->button(__('Save Article'));
echo $this->Form->end();
?>
This template outputs the edit form (with the values populated), along with any necessary validation error messages.
You can now update your index view with links to edit specific articles:
<h1>Articles</h1>
<p><?= $this->Html->link("Add Article", ['action' => 'add']) ?></p>
<table>
<tr>
<th>Title</th>
<th>Created</th>
<th>Action</th>
</tr>
<!-- Here's where we iterate through our $articles query object, printing out article
˓→info -->
</td>
<td>
<?= $article->created->format(DATE_RFC850) ?>
</td>
<td>
<?= $this->Html->link('Edit', ['action' => 'edit', $article->slug]) ?>
</td>
</tr>
<?php endforeach; ?>
</table>
Up until this point our Articles had no input validation done. Lets fix that by using a validator:
// src/Model/Table/ArticlesTable.php
// add this use statement right below the namespace declaration to import
// the Validator class
use Cake\Validation\Validator;
->allowEmptyString('body', false)
->minLength('body', 10);
return $validator;
}
The validationDefault() method tells CakePHP how to validate your data when the save() method is called.
Here, we’ve specified that both the title, and body fields must not be empty, and have certain length constraints.
CakePHP’s validation engine is powerful and flexible. It provides a suite of frequently used rules for tasks like email
addresses, IP addresses etc. and the flexibility for adding your own validation rules. For more information on that
setup, check the Validation documentation.
Now that your validation rules are in place, use the app to try to add an article with an empty title or body to see how
it works. Since we’ve used the Cake\View\Helper\FormHelper::control() method of the FormHelper to
create our form elements, our validation error messages will be shown automatically.
Next, let’s make a way for users to delete articles. Start with a delete() action in the ArticlesController:
// src/Controller/ArticlesController.php
$article = $this->Articles->findBySlug($slug)->firstOrFail();
if ($this->Articles->delete($article)) {
$this->Flash->success(__('The {0} article has been deleted.', $article->
˓→title));
This logic deletes the article specified by $slug, and uses $this->Flash->success() to show the user a
confirmation message after redirecting them to /articles. If the user attempts to delete an article using a GET
request, 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.
Warning: Allowing content to be deleted using GET requests is very dangerous, as web crawlers could acciden-
tally delete all your content. That is why we used allowMethod() in our controller.
Because we’re only executing logic and redirecting to another action, this action has no template. You might want to
update your index template with links that allow users to delete articles:
<!-- File: src/Template/Articles/index.ctp (delete links added) -->
<h1>Articles</h1>
<p><?= $this->Html->link("Add Article", ['action' => 'add']) ?></p>
<table>
<tr>
<th>Title</th>
<th>Created</th>
<th>Action</th>
</tr>
<!-- Here's where we iterate through our $articles query object, printing out article
˓→info -->
</td>
<td>
<?= $article->created->format(DATE_RFC850) ?>
</td>
<td>
<?= $this->Html->link('Edit', ['action' => 'edit', $article->slug]) ?>
<?= $this->Form->postLink(
'Delete',
['action' => 'delete', $article->slug],
['confirm' => 'Are you sure?'])
?>
</td>
</tr>
<?php endforeach; ?>
</table>
Using View\Helper\FormHelper::postLink() will create a link that uses JavaScript to do a POST request
deleting our article.
Note: This view code also uses the FormHelper to prompt the user with a JavaScript confirmation dialog before
they attempt to delete an article.
With a basic articles management setup, we’ll create the basic actions for our Tags and Users tables.
With the basic article creation functionality built, we need to enable multiple authors to work in our CMS. Previously,
we built all the models, views and controllers by hand. This time around we’re going to use Bake Console to create our
skeleton code. Bake is a powerful code generation CLI (Command Line Interface) tool that leverages the conventions
CakePHP uses to create skeleton CRUD (Create, Read, Update, Delete) applications very efficiently. We’re going to
use bake to build our users code:
cd /path/to/our/app
With multiple users able to access our small CMS it would be nice to have a way to categorize our content. We’ll use
tags and tagging to allow users to create free-form categories and labels for their content. Again, we’ll use bake to
quickly generate some skeleton code for our application:
Once you have the scaffold code created, create a few sample tags by going to http://localhost:8765/tags/add.
Now that we have a Tags table, we can create an association between Articles and Tags. We can do so by adding the
following to the initialize method on the ArticlesTable:
This association will work with this simple definition because we followed CakePHP conventions when creating our
tables. For more information, read Associations - Linking Tables Together.
Now that our application has tags, we need to enable users to tag their articles. First, update the add action to look
like:
// in src/Controller/ArticlesController.php
namespace App\Controller;
use App\Controller\AppController;
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.'));
}
// Get a list of tags.
$tags = $this->Articles->Tags->find('list');
$this->set('tags', $tags);
$this->set('article', $article);
}
// Other actions
}
The added lines load a list of tags as an associative array of id => title. This format will let us create a new tag
input in our template. Add the following to the PHP block of controls in src/Template/Articles/add.ctp:
echo $this->Form->control('tags._ids', ['options' => $tags]);
This will render a multiple select element that uses the $tags variable to generate the select box options. You should
now create a couple new articles that have tags, as in the following section we’ll be adding the ability to find articles
by tags.
You should also update the edit method to allow adding or editing tags. The edit method should now look like:
public function edit($slug)
{
$article = $this->Articles
->findBySlug($slug)
->contain('Tags') // load associated Tags
->firstOrFail();
if ($this->request->is(['post', 'put'])) {
$this->Articles->patchEntity($article, $this->request->getData());
if ($this->Articles->save($article)) {
$this->Flash->success(__('Your article has been updated.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to update your article.'));
}
$this->set('article', $article);
}
Remember to add the new tags multiple select control we added to the add.ctp template to the
src/Template/Articles/edit.ctp template as well.
Once users have categorized their content, they will want to find that content by the tags they used. For this feature
we’ll implement a route, controller action, and finder method to search through articles by tag.
Ideally, we’d have a URL that looks like http://localhost:8765/articles/tagged/funny/cat/gifs. This would let us find
all the articles 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:
<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\DashedRoute;
Router::defaultRouteClass(DashedRoute::class);
The above defines a new ‘route’ which connects the /articles/tagged/ path, to ArticlesController::tags().
By defining routes, you can isolate how your URLs look, from how they are implemented. If we were to visit
http://localhost:8765/articles/tagged, we would see a helpful error page from CakePHP informing you that the con-
troller action does not exist. Let’s implement that missing method now. In src/Controller/ArticlesController.php
add the following:
public function tags()
{
// The 'pass' key is provided by CakePHP and contains all
// the passed URL path segments in the request.
$tags = $this->request->getParam('pass');
To access other parts of the request data, consult the Request section.
Since passed arguments are passed as method parameters, you could also write the action using PHP’s variadic argu-
ment:
public function tags(...$tags)
{
// Use the ArticlesTable to find tagged articles.
$articles = $this->Articles->find('tagged', [
'tags' => $tags
]);
In CakePHP we like to keep our controller actions slim, and put most of our application’s logic in the model layer.
If you were to visit the /articles/tagged URL now you would see an error that the findTagged() method has not
been implemented yet, so let’s do that. In src/Model/Table/ArticlesTable.php add the following:
// add this use statement right below the namespace declaration to import
// the Query class
use Cake\ORM\Query;
$query = $query
->select($columns)
->distinct($columns);
if (empty($options['tags'])) {
// If there are no tags provided, find articles that have no tags.
$query->leftJoinWith('Tags')
->where(['Tags.title IS' => null]);
} else {
// Find articles that have one or more of the provided tags.
$query->innerJoinWith('Tags')
->where(['Tags.title IN' => $options['tags']]);
}
return $query->group(['Articles.id']);
}
We just implemented a custom finder method. This is a very powerful concept in CakePHP that allows you to package
up re-usable queries. Finder methods always get a Query Builder object and an array of options as parameters. Finders
can manipulate the query and add any required conditions or criteria. When complete, finder methods must return a
modified query object. In our finder we’ve leveraged the distinct() and leftJoin() methods which allow us
to find distinct articles that have a ‘matching’ tag.
Now if you visit the /articles/tagged URL again, CakePHP will show a new error letting you know that you have not
made a view file. Next, let’s build the view file for our tags() action. In src/Template/Articles/tags.ctp put the
following content:
<h1>
Articles tagged with
<?= $this->Text->toList(h($tags), 'or') ?>
</h1>
<section>
<?php foreach ($articles as $article): ?>
<article>
<!-- Use the HtmlHelper to create a link -->
<h4><?= $this->Html->link(
$article->title,
['controller' => 'Articles', 'action' => 'view', $article->slug]
) ?></h4>
<span><?= h($article->created) ?></span>
</article>
<?php endforeach; ?>
</section>
In the above code we use the Html and Text helpers to assist in generating our view output. We also use the h shortcut
function to HTML encode output. You should remember to always use h() when outputting data to prevent HTML
injection issues.
The tags.ctp file we just created follows the CakePHP conventions for view template files. The convention is to have
the template use the lower case and underscored version of the controller action name.
You may notice that we were able to use the $tags and $articles variables in our view template. When we use
the set() method in our controller, we set specific variables to be sent to the view. The View will make all passed
variables available in the template scope as local variables.
You should now be able to visit the /articles/tagged/funny URL and see all the articles tagged with ‘funny’.
Right now, adding new tags is a cumbersome process, as authors need to pre-create all the tags they want to use. We
can improve the tag selection UI by using a comma separated text field. This will let us give a better experience to our
users, and use some more great features in the ORM.
Because we’ll want a simple way to access the formatted tags for an entity, we can add a virtual/computed field to the
entity. In src/Model/Entity/Article.php add the following:
// add this use statement right below the namespace declaration to import
// the Collection class
use Cake\Collection\Collection;
];
This will let us access the $article->tag_string computed property. We’ll use this property in controls later
on.
With the entity updated we can add a new control for our tags. In src/Template/Articles/add.ctp and
src/Template/Articles/edit.ctp, replace the existing tags._ids control with the following:
echo $this->Form->control('tag_string', ['type' => 'text']);
We’ll also need to update the article view template. In src/Template/Articles/view.ctp add the line as shown:
<!-- File: src/Template/Articles/view.ctp -->
Now that we can view existing tags as a string, we’ll want to save that data as well. Because we marked
the tag_string as accessible, the ORM will copy that data from the request into our entity. We can use a
beforeSave() hook method to parse the tag string and find/build the related entities. Add the following to
src/Model/Table/ArticlesTable.php:
public function beforeSave($event, $entity, $options)
{
if ($entity->tag_string) {
$entity->tags = $this->_buildTags($entity->tag_string);
}
// Other code
}
$out = [];
$query = $this->Tags->find()
->where(['Tags.title IN' => $newTags]);
If you now create or edit articles, you should be able to save tags as a comma separated list of tags, and have the tags
and linking records automatically created.
While this code is a bit more complicated than what we’ve done so far, it helps to showcase how powerful the ORM
in CakePHP is. You can manipulate query results using the Collections methods, and handle scenarios where you are
creating entities on the fly with ease.
Before we finish up, we’ll need a mechanism that will load the associated tags (if any) whenever we load an article.
In your src/Model/Table/ArticlesTable.php, change:
This will tell the Articles table model that there is a join table associated with tags. The ‘dependent’ option tells the
table to delete any associated records from the join table if an article is deleted.
Lastly, update the findBySlug() method calls in src/Controller/ArticlesController.php:
The contain() method tells the ArticlesTable object to also populate the Tags association when the article is loaded.
Now when tag_string is called for an Article entity, there will be data present to create the string!
Next we’ll be adding authentication.
Now that our CMS has users, we should enable them to login, and apply some basic access control to the article
creation & editing experiences.
If you were to create/update a user at this point in time, you might notice that the passwords are stored in plain text.
This is really bad from a security point of view, so lets fix that.
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:
<?php
namespace App\Model\Entity;
return $hasher->hash($value);
}
}
}
Now, point your browser to http://localhost:8765/users to see a list of users. You can edit the default user that was
created during Installation. If you change that user’s password, you should see a hashed password instead of the
original value on the list or view pages. CakePHP hashes passwords with bcrypt49 by default. You can also use SHA-1
or MD5 if you’re working with an existing database, but we recommend bcrypt for all new applications.
Note: Create a hashed password for at least one of the user accounts now! It will be needed in the next steps.
Adding Login
In CakePHP, authentication is handled by Components. Components can be thought of as ways to create reusable
chunks of controller code related to a specific feature or concept. Components can hook into the controller’s event
life-cycle and interact with your application that way. To get started, we’ll add the AuthComponent to our application.
We’ll want the create, update and delete methods to require authentication, so we’ll add AuthComponent in our
AppController:
// In src/Controller/AppController.php
namespace App\Controller;
use Cake\Controller\Controller;
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
// If unauthorized, return them to page they were just on
'unauthorizedRedirect' => $this->referer()
]);
49 http://codahale.com/how-to-safely-store-a-password/
We’ve just told CakePHP that we want to load the Auth component. We’ve customized the configuration of AuthCom-
ponent, as our users table uses email as the username. Now, if you go any protected URL, such as /articles/
add, you’ll be redirected to /users/login, which will show an error page as we have not written that code yet. So let’s
create the login action:
// In src/Controller/UsersController.php
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error('Your username or password is incorrect.');
}
}
<h1>Login</h1>
<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->control('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>
Now that we have a simple login form, we should be able to log in with one of the users that has a hashed password.
Note: If none of your users have hashed passwords, comment the loadComponent('Auth') block and
$this->Auth->allow() calls. Then go and edit the user, saving a new password for them. After saving a
new password for the user, make sure to uncomment the lines we just temporarily commented!
Try it out! Before logging in, visit /articles/add. Since this action is not allowed, you will be redirected to the
login page. After logging in successfully, CakePHP will automatically redirect you back to /articles/add.
Adding Logout
Now that people can log in, you’ll probably want to provide a way to log out as well. Again, in the
UsersController, add the following code:
return $this->redirect($this->Auth->logout());
}
This code adds the logout action to the list of actions that do not require authentication and implements the logout
method. Now you can visit /users/logout to log out. You should then be sent to the login page.
Enabling Registrations
If you aren’t logged in and you try to visit /users/add you will be redirected to the login page. We should fix that as
we want to allow people to sign up for our application. In the UsersController add the following:
public function initialize()
{
parent::initialize();
// Add the 'add' action to the allowed actions list.
$this->Auth->allow(['logout', 'add']);
}
The above tells AuthComponent that the add() action of the UsersController does not require authentication
or authorization. You may want to take the time to clean up the Users/add.ctp and remove the misleading links, or
continue on to the next section. We won’t be building out user editing, viewing or listing in this tutorial, but that is an
exercise you can complete on your own.
Now that users can log in, we’ll want to limit users to only edit articles that they created. We’ll do this us-
ing an ‘authorization’ adapter. Since our requirements are basic, we can use a controller hook method in our
ArticlesController. But before we do that, we’ll want to tell the AuthComponent how our application
is going to authorize actions. Update your AppController adding the following:
public function isAuthorized($user)
{
// By default deny access.
return false;
}
Next we’ll tell AuthComponent that we want to use controller hook methods for authorization. Your
AppController::initialize() method should now look like:
public function initialize()
{
// Existing code
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
// Added this line
'authorize'=> 'Controller',
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
// If unauthorized, return them to page they were just on
'unauthorizedRedirect' => $this->referer()
]);
We’ll default to denying access, and incrementally grant access where it makes sense. First, we’ll add the authorization
logic for articles. In your ArticlesController add the following:
Now if you try to edit or delete an article that does not belong to you, you should be redirected back to the page you
came from. If no error message is displayed, add the following to your layout:
// In src/Template/Layout/default.ctp
<?= $this->Flash->render() ?>
Next you should add the tags action to the actions allowed for unauthenticated users, by adding the following to
initialize() in src/Controller/ArticlesController.php:
$this->Auth->allow(['tags']);
While the above is fairly simplistic it illustrates how you could build more complex logic that combines the current
user and request data to build flexible authorization logic.
While we’ve blocked access to the edit action, we’re still open to users changing the user_id attribute of articles
during edit. We will solve these problems next. First up is the add action.
When creating articles, we want to fix the user_id to be the currently logged in user. Replace your add action with
the following:
// in src/Controller/ArticlesController.php
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);
}
Next we’ll update the edit action. Replace the edit method with the following:
// in src/Controller/ArticlesController.php
if ($this->request->is(['post', 'put'])) {
$this->Articles->patchEntity($article, $this->request->getData(), [
// Added: Disable modification of user_id.
'accessibleFields' => ['user_id' => false]
]);
if ($this->Articles->save($article)) {
$this->Flash->success(__('Your article has been updated.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to update your article.'));
}
$this->set('article', $article);
}
Here we’re modifying which properties can be mass-assigned, via the options for patchEntity(). See the
Changing Accessible Fields section for more information. Remember to remove the user_id control from
src/Template/Articles/edit.ctp as we no longer need it.
Wrapping Up
We’ve built a simple CMS application that allows users to login, post articles, tag them, explore posted articles by
tag, and applied basic access control to articles. We’ve also added some nice UX improvements by leveraging the
FormHelper and ORM capabilities.
Thank you for taking the time to explore CakePHP. Next, you should learn more about the Database Access & ORM,
or you peruse the /topics.
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/bookmarker50 . 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:
If you downloaded and ran the Composer Windows Installer52 , then type the following line in your terminal from your
installation directory (ie. C:\wamp\www\dev\cakephp3):
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
50 https://github.com/cakephp/bookmarker-tutorial
51 https://getcomposer.org/download/
52 https://getcomposer.org/Composer-Setup.exe
/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.
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).
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.
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:
Bookmarker Tutorial 77
CakePHP Cookbook Documentation, Release 3.8
modified DATETIME,
FOREIGN KEY user_key (user_id) REFERENCES users(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.
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.
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:
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.
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.
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;
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 bcrypt53 by default. You can
also use sha1 or md5 if you’re working with an existing database.
53 http://codahale.com/how-to-safely-store-a-password/
Bookmarker Tutorial 79
CakePHP Cookbook Documentation, Release 3.8
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
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:
<?php
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\Router;
Router::defaultRouteClass(DashedRoute::class);
The above defines a new ‘route’ which connects the /bookmarks/tagged/ path, to
BookmarksController::tags(). By defining routes, you can isolate how your URLs look, from how
they are implemented. If we were to visit http://localhost:8765/bookmarks/tagged, we would see a helpful error
page from CakePHP informing you that the controller action does not exist. Let’s implement that missing method
now. In src/Controller/BookmarksController.php add the following:
public function tags()
{
// The 'pass' key is provided by CakePHP and contains all
// the passed URL path segments in the request.
$tags = $this->request->getParam('pass');
To access other parts of the request data, consult the Request section.
In CakePHP we like to keep our controller actions slim, and put most of our application’s logic in the models. If you
were to visit the /bookmarks/tagged URL now you would see an error that the findTagged() method has not
been implemented yet, so let’s do that. In src/Model/Table/BookmarksTable.php add the following:
// The $query argument is a query builder instance.
// The $options array will contain the 'tags' option we passed
// to find('tagged') in our controller action.
public function findTagged(Query $query, array $options)
{
$bookmarks = $this->find()
->select(['id', 'url', 'title', 'description']);
if (empty($options['tags'])) {
$bookmarks
->leftJoinWith('Tags')
->where(['Tags.title IS' => null]);
} else {
$bookmarks
->innerJoinWith('Tags')
->where(['Tags.title IN ' => $options['tags']]);
}
return $bookmarks->group(['Bookmarks.id']);
}
We just implemented a custom finder method. This is a very powerful concept in CakePHP that allows you to package
up re-usable queries. Finder methods always get a Query Builder object and an array of options as parameters.
Finders can manipulate the query and add any required conditions or criteria. When complete, finder methods must
return a modified query object. In our finder we’ve leveraged the innerJoinWith(), where() and group()
methods which allow us to find distinct bookmarks that have a matching tag. When no tags are provided we use a
leftJoinWith() and modify the ‘where’ condition, finding bookmarks without tags.
Now if you visit the /bookmarks/tagged URL, CakePHP will show an error letting you know that you have not made
a view file. Next, let’s build the view file for our tags() action. In src/Template/Bookmarks/tags.ctp put the
following content:
Bookmarker Tutorial 81
CakePHP Cookbook Documentation, Release 3.8
<h1>
Bookmarks tagged with
<?= $this->Text->toList(h($tags)) ?>
</h1>
<section>
<?php foreach ($bookmarks as $bookmark): ?>
<article>
<!-- Use the HtmlHelper to create a link -->
<h4><?= $this->Html->link($bookmark->title, $bookmark->url) ?></h4>
<small><?= h($bookmark->url) ?></small>
In the above code we use the Html and Text helpers to assist in generating our view output. We also use the h shortcut
function to HTML encode output. You should remember to always use h() when outputting user data to prevent
HTML injection issues.
The tags.ctp file we just created follows the CakePHP conventions for view template files. The convention is to have
the template use the lower case and underscored version of the controller action name.
You may notice that we were able to use the $tags and $bookmarks variables in our view. When we use the
set() method in our controller, we set specific variables to be sent to the view. The view will make all passed
variables available in the templates as local variables.
You should now be able to visit the /bookmarks/tagged/funny URL and see all the bookmarks tagged with ‘funny’.
So far, we’ve created a basic application to manage bookmarks, tags and users. However, everyone can see everyone
else’s tags. In the next chapter, we’ll implement authentication and restrict the visible bookmarks to only those that
belong to the current user.
Now continue to Bookmarker Tutorial Part 2 to continue building your application or dive into the documentation to
learn more about what CakePHP can do for you.
After finishing the first part of this tutorial you should have a very basic bookmarking application. In this chapter
we’ll be adding authentication and restricting the bookmarks each user can see/modify to only the ones they own.
Adding Login
In CakePHP, authentication is handled by Components. Components can be thought of as ways to create reusable
chunks of controller code related to a specific feature or concept. Components can also hook into the controller’s event
life-cycle and interact with your application that way. To get started, we’ll add the AuthComponent to our application.
We’ll pretty much want every method to require authentication, so we’ll add AuthComponent in our AppController:
// In src/Controller/AppController.php
namespace App\Controller;
use Cake\Controller\Controller;
]);
We’ve just told CakePHP that we want to load the Flash and Auth components. In addition, we’ve customized the
configuration of AuthComponent, as our users table uses email as the username. Now, if you go to any URL you’ll
be kicked to /users/login, which will show an error page as we have not written that code yet. So let’s create the login
action:
// In src/Controller/UsersController.php
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error('Your username or password is incorrect.');
}
}
Note: The control() method is available since 3.4. For prior versions you can use the input() method instead.
Now that we have a simple login form, we should be able to log in with one of the users that has a hashed password.
Note: If none of your users have hashed passwords, comment the loadComponent('Auth') line. Then go and
edit the user, saving a new password for them.
Adding Logout
Now that people can log in, you’ll probably want to provide a way to log out as well. Again, in the
UsersController, add the following code:
This code whitelists the logout action as a public action, and implements the logout method. Now you can visit
/users/logout to log out. You should then be sent to the login page.
Enabling Registrations
If you aren’t logged in and you try to visit /users/add you will be kicked to the login page. We should fix that as we
want to allow people to sign up for our application. In the UsersController add the following:
The above tells AuthComponent that the add() action does not require authentication or authorization. You may
want to take the time to clean up the Users/add.ctp and remove the misleading links, or continue on to the next section.
We won’t be building out user editing, viewing or listing in this tutorial so they will not work as AuthComponent
will deny you access to those controller actions.
Now that users can log in, we’ll want to limit the bookmarks they can see to the ones they made. We’ll do this
using an ‘authorization’ adapter. Since our requirements are pretty simple, we can write some simple code in our
BookmarksController. But before we do that, we’ll want to tell the AuthComponent how our application is
going to authorize actions. In your AppController add the following:
return false;
}
Also, add the following to the configuration for Auth in your AppController:
'authorize' => 'Controller',
We’ll default to denying access, and incrementally grant access where it makes sense. First, we’ll add the authorization
logic for bookmarks. In your BookmarksController add the following:
public function isAuthorized($user)
{
$action = $this->request->getParam('action');
Now if you try to view, edit or delete a bookmark that does not belong to you, you should be redirected back to the
page you came from. If no error message is displayed, add the following to your layout:
// In src/Template/Layout/default.ctp
<?= $this->Flash->render() ?>
While view and delete are working, edit, add and index have a few problems:
1. When adding a bookmark you can choose the user.
2. When editing a bookmark you can choose the user.
3. The list page shows bookmarks from other users.
Let’s tackle the add form first. To begin with remove the control('user_id') from
src/Template/Bookmarks/add.ctp. With that removed, we’ll also update the add() action from
src/Controller/BookmarksController.php to look like:
$bookmark->user_id = $this->Auth->user('id');
if ($this->Bookmarks->save($bookmark)) {
$this->Flash->success('The bookmark has been saved.');
return $this->redirect(['action' => 'index']);
}
$this->Flash->error('The bookmark could not be saved. Please, try again.');
}
$tags = $this->Bookmarks->Tags->find('list');
$this->set(compact('bookmark', 'tags'));
$this->set('_serialize', ['bookmark']);
}
By setting the entity property with the session data, we remove any possibility of the user modifying which
user a bookmark is for. We’ll do the same for the edit form and action. Your edit() action from
src/Controller/BookmarksController.php should look like:
$bookmark->user_id = $this->Auth->user('id');
if ($this->Bookmarks->save($bookmark)) {
$this->Flash->success('The bookmark has been saved.');
return $this->redirect(['action' => 'index']);
}
$this->Flash->error('The bookmark could not be saved. Please, try again.');
}
$tags = $this->Bookmarks->Tags->find('list');
$this->set(compact('bookmark', 'tags'));
$this->set('_serialize', ['bookmark']);
}
List View
Now, we only need to show bookmarks for the currently logged in user. We can do that by updating the call to
paginate(). Make your index() action from src/Controller/BookmarksController.php look like:
We should also update the tags() action and the related finder method, but we’ll leave that as an exercise you can
complete on your own.
Right now, adding new tags is a difficult process, as the TagsController disallows all access. Instead of allowing
access, we can improve the tag selection UI by using a comma separated text field. This will let us give a better
experience to our users, and use some more great features in the ORM.
Because we’ll want a simple way to access the formatted tags for an entity, we can add a virtual/computed field to the
entity. In src/Model/Entity/Bookmark.php add the following:
use Cake\Collection\Collection;
This will let us access the $bookmark->tag_string computed property. We’ll use this property in controls later
on. Remember to add the tag_string property to the _accessible list in your entity, as we’ll want to ‘save’ it
later on.
In src/Model/Entity/Bookmark.php add the tag_string to $_accessible this way:
protected $_accessible = [
'user_id' => true,
'title' => true,
'description' => true,
'url' => true,
'user' => true,
'tags' => true,
'tag_string' => true,
];
With the entity updated we can add a new control for our tags. In src/Template/Bookmarks/add.ctp and
src/Template/Bookmarks/edit.ctp, replace the existing tags._ids control with the following:
Now that we can view existing tags as a string, we’ll want to save that data as well. Because we marked
the tag_string as accessible, the ORM will copy that data from the request into our entity. We can use a
beforeSave() hook method to parse the tag string and find/build the related entities. Add the following to
src/Model/Table/BookmarksTable.php:
$out = [];
$query = $this->Tags->find()
->where(['Tags.title IN' => $newTags]);
}
}
// Add existing tags.
foreach ($query as $tag) {
$out[] = $tag;
}
// Add new tags.
foreach ($newTags as $tag) {
$out[] = $this->Tags->newEntity(['title' => $tag]);
}
return $out;
}
While this code is a bit more complicated than what we’ve done so far, it helps to showcase how powerful the ORM
in CakePHP is. You can manipulate query results using the Collections methods, and handle scenarios where you are
creating entities on the fly with ease.
Wrapping Up
We’ve expanded our bookmarking application to handle authentication and basic authorization/access control scenar-
ios. We’ve also added some nice UX improvements by leveraging the FormHelper and ORM capabilities.
Thanks for taking the time to explore CakePHP. Next, you can complete the Blog Tutorial, learn more about the
Database Access & ORM, or you can peruse the /topics.
Blog Tutorial
This tutorial will walk you through the creation of a simple blog application. We’ll be installing CakePHP, creating a
database, and creating enough application logic to list, add, edit, and delete blog articles.
Here’s what you’ll need:
1. A running web server. We’re going to assume you’re using Apache, though the instructions for using other
servers should be very similar. We might have to play a little with the server configuration, but most folks can
get CakePHP up and running without any configuration at all. Make sure you have PHP 5.6.0 or greater, and
that the mbstring and intl extensions are enabled in PHP.
2. 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.
3. Basic PHP knowledge.
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:
Blog Tutorial 89
CakePHP Cookbook Documentation, Release 3.8
In case you’ve already got composer installed globally, you may instead type:
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:
/cake_install
/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.
The tmp and logs directories need to have proper permissions to be writable by your webserver. If you used
Composer for the install, this should have been done for you and confirmed with a “Permissions set on <folder>”
message. If you instead got an error message or want to do it manually, the best way would be to find out what user
your webserver runs as (<?= `whoami`; ?>) and change the ownership of these two directories to that user. The
final command you run (in *nix) might look something like this:
If for some reason CakePHP can’t write to these directories, you’ll be informed by a warning while not in production
mode.
54 https://getcomposer.org/download/
While not recommended, if you are unable to set the permissions to the same as your webserver, you can simply set
write permissions on the folder by running a command such as:
Next, let’s set up the underlying MySQL database for our blog. 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_blog. Right now, we’ll just create a single
table to store our articles. We’ll also throw in a few articles to use for testing purposes. Execute the following SQL
statements into your database:
The choices on table and column names are not arbitrary. If you follow CakePHP’s database naming conventions,
and CakePHP’s class naming conventions (both outlined in CakePHP Conventions), you’ll be able to take advantage
of a lot of free functionality and avoid configuration. CakePHP is flexible enough to accommodate even inconsistent
legacy database schemas, but adhering to the conventions will save you time.
Check out CakePHP Conventions for more information, but it’s suffice to say that naming our table ‘articles’ automat-
ically hooks it to our Articles model, and having fields called ‘modified’ and ‘created’ will be automatically managed
by CakePHP.
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.
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,
Blog Tutorial 91
CakePHP Cookbook Documentation, Release 3.8
Once you’ve saved your config/app.php file, you should be able to open your browser and see the CakePHP welcome
page. It should also tell you that your database connection file was found, and that CakePHP can successfully connect
to the database.
Optional Configuration
There are a few other items that can be configured. Most developers complete these laundry-list items, but they’re not
required for this tutorial. One is defining a custom string (or “salt”) for use in security hashes.
The security salt is used for generating hashes. If you used Composer this too is taken care of for you during the
install. Else you’d need to change the default salt value by editing config/app.php. It doesn’t matter much what the
new value is, as long as it’s not guessable:
'Security' => [
'salt' => 'something long and containing lots of different values.',
],
A Note on mod_rewrite
Occasionally new users will run into mod_rewrite issues. For example if the CakePHP welcome page looks a little
funny (no images or CSS styles). This probably means mod_rewrite is not functioning on your system. Please refer to
the URL Rewriting section to help resolve any issues you are having.
Now continue to Blog Tutorial - Part 2 to start building your first CakePHP application.
Models are the bread and butter of CakePHP applications. By creating a CakePHP model that will interact with our
database, we’ll have the foundation in place needed to do our view, add, edit, and delete operations later.
CakePHP’s model class files are split between Table and Entity objects. Table objects provide access to the
collection of entities stored in a specific table and go in src/Model/Table. The file we’ll be creating will be saved to
src/Model/Table/ArticlesTable.php. The completed file should look like this:
// src/Model/Table/ArticlesTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
Naming conventions are very important in CakePHP. By naming our Table object ArticlesTable, CakePHP can
automatically infer that this Table object will be used in the ArticlesController, and will be tied to a database
table called articles.
Note: CakePHP will dynamically create a model object for you if it cannot find a corresponding file in
src/Model/Table. This also means that if you accidentally name your file wrong (i.e. articlestable.php or Arti-
cleTable.php), CakePHP will not recognize any of your settings and will use the generated model instead.
For more on models, such as callbacks, and validation, check out the Database Access & ORM chapter of the Manual.
Note: If you completed Part 1 of the Blog Tutorial and created the articles table in our Blog database you can
leverage CakePHP’s bake console and its code generation capabilities to create the ArticlesTable model:
For more on bake and its code generation features please visit /bake/usage.
Next, we’ll create a controller for our articles. The controller is where all interaction with articles will happen. In a
nutshell, it’s the place where you play with the business logic contained in the models and get work related to articles
done. We’ll place this new controller in a file called ArticlesController.php inside the src/Controller directory.
Here’s what the basic controller should look like:
// src/Controller/ArticlesController.php
namespace App\Controller;
Now, let’s add an action to our controller. Actions often represent a single function or interface in an
application. For example, when users request www.example.com/articles/index (which is also the same as
www.example.com/articles/), they might expect to see a listing of articles. The code for that action would look like
this:
// src/Controller/ArticlesController.php
namespace App\Controller;
By defining function index() in our ArticlesController, users can now access the logic there by requesting
www.example.com/articles/index. Similarly, if we were to define a function called foobar(), users would be able
to access that at www.example.com/articles/foobar.
Warning: You may be tempted to name your controllers and actions a certain way to obtain a certain URL.
Resist that temptation. Follow CakePHP Conventions (capitalization, plural names, etc.) and create readable,
understandable action names. You can map URLs to your code using Routing covered later on.
The single instruction in the action uses set() to pass data from the controller to the view (which we’ll create
next). The line sets the view variable called ‘articles’ equal to the return value of the find('all') method of the
ArticlesTable object.
Note: If you completed Part 1 of the Blog Tutorial and created the articles table in your Blog database you can
leverage CakePHP’s bake console and its code generation capabilities to create the ArticlesController class:
For more on bake and its code generation features please visit /bake/usage.
To learn more about CakePHP’s controllers, check out the Controllers chapter.
Now that we have our data flowing from our model, and our application logic is defined by our controller, let’s create
a view for the index action we created above.
CakePHP views are just presentation-flavored fragments that fit inside an application’s layout. For most applications,
they’re HTML mixed with PHP, but they may end up as XML, CSV, or even binary data.
A layout is presentation code that is wrapped around a view. Multiple layouts can be defined, and you can switch
between them, but for now, let’s just use the default.
Remember in the last section how we assigned the ‘articles’ variable to the view using the set() method? That
would hand down the query object collection to the view to be invoked with a foreach iteration.
CakePHP’s template files are stored in src/Template inside a folder named after the controller they correspond to
(we’ll have to create a folder named ‘Articles’ in this case). To format this article data in a nice table, our view code
might look something like this:
<h1>Blog articles</h1>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<!-- Here is where we iterate through our $articles query object, printing out
˓→ article info -->
</td>
<td>
<?= $article->created->format(DATE_RFC850) ?>
</td>
</tr>
<?php endforeach; ?>
</table>
// src/Controller/ArticlesController.php
namespace App\Controller;
The set() call should look familiar. Notice we’re using get() rather than find('all') because we only really
want a single article’s information.
Notice that our view action takes a parameter: the ID of the article we’d like to see. This parameter is handed to the
action through the requested URL. If a user requests /articles/view/3, then the value ‘3’ is passed as $id.
We also do a bit of error checking to ensure a user is actually accessing a record. By using the get() function in the
Articles table, we make sure the user has accessed a record that exists. In case the requested article is not present in
the database, or the id is false the get() function will throw a NotFoundException.
Now let’s create the view for our new ‘view’ action and place it in src/Template/Articles/view.ctp
<!-- File: src/Template/Articles/view.ctp -->
Verify that this is working by trying the links at /articles/index or manually requesting an article by accessing
/articles/view/{id}, replacing {id} by an article ‘id’.
Adding Articles
Reading from the database and showing us the articles is a great start, but let’s allow for the adding of new articles.
First, start by creating an add() action in the ArticlesController:
// src/Controller/ArticlesController.php
namespace App\Controller;
use App\Controller\AppController;
{
$article = $this->Articles->get($id);
$this->set(compact('article'));
}
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);
}
}
Note: You need to include the Flash component in any controller where you will use it. If necessary, include it in
your AppController.
Here’s what the add() action does: if the HTTP method of the request was POST, try to save the data using the
Articles model. If for some reason it doesn’t save, just render the view. This gives us a chance to show the user
validation errors or other warnings.
Every CakePHP request includes a ServerRequest object which is accessible using $this->request. The
request object contains useful information regarding the request that was just received, and can be used to control the
flow of your application. In this case, we use the Cake\Http\ServerRequest::is() method to check that the
request is a HTTP POST request.
When a user uses a form to POST data to your application, that information is available in
$this->request->getData() ( Or $this->request->data() for CakePHP v3.3 and under ).
You can use the pr() or debug() functions to print it out if you want to see what it looks like.
We use FlashComponent’s success() and error() methods to set a message to a session variable. These methods
are provided using PHP’s magic method features55 . Flash messages will be displayed on the page after redirection.
In the layout we have <?= $this->Flash->render() ?> which displays the message and clears the corre-
sponding session variable. The controller’s Cake\Controller\Controller::redirect function redirects
to another URL. The param ['action' => 'index'] translates to URL /articles i.e the index action of the
ArticlesController. You can refer to Cake\Routing\Router::url() function on the API56 to see the
formats in which you can specify a URL for various CakePHP functions.
Calling the save() method will check for validation errors and abort the save if any occur. We’ll discuss how those
errors are handled in the following sections.
Data Validation
CakePHP goes a long way toward taking the monotony out of form input validation. Everyone hates coding up endless
forms and their validation routines. CakePHP makes it easier and faster.
55 http://php.net/manual/en/language.oop5.overloading.php#object.call
56 https://api.cakephp.org
To take advantage of the validation features, you’ll need to use CakePHP’s Form helper in your views. The
Cake\View\Helper\FormHelper is available by default to all views at $this->Form.
Here’s our add view:
<h1>Add Article</h1>
<?php
echo $this->Form->create($article);
echo $this->Form->control('title');
echo $this->Form->control('body', ['rows' => '3']);
echo $this->Form->button(__('Save Article'));
echo $this->Form->end();
?>
We use the FormHelper to generate the opening tag for an HTML form. Here’s the HTML that
$this->Form->create() generates:
If create() is called with no parameters supplied, it assumes you are building a form that submits via POST to the
current controller’s add() action (or edit() action when id is included in the form data).
The $this->Form->control() method is used to create form elements of the same name. The first parameter
tells CakePHP which field they correspond to, and the second parameter allows you to specify a wide array of options
- in this case, the number of rows for the textarea. There’s a bit of introspection and automagic here: control()
will output different form elements based on the model field specified.
The $this->Form->end() call ends the form. Outputting hidden inputs if CSRF/Form Tampering prevention is
enabled.
Now let’s go back and update our src/Template/Articles/index.ctp view to include a new “Add Article” link. Before
the <table>, add the following line:
You may be wondering: how do I tell CakePHP about my validation requirements? Validation rules are defined in the
model. Let’s look back at our Articles model and make a few adjustments:
// src/Model/Table/ArticlesTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
->notEmpty('body')
->requirePresence('body');
return $validator;
}
}
The validationDefault() method tells CakePHP how to validate your data when the save() method is called.
Here, we’ve specified that both the body and title fields must not be empty, and are required for both create and update
operations. CakePHP’s validation engine is strong, with a number of pre-built rules (credit card numbers, email
addresses, etc.) and flexibility for adding your own validation rules. For more information on that setup, check the
Validation documentation.
Now that your validation rules are in place, use the app to try to add an article with an empty title or body to see how
it works. Since we’ve used the Cake\View\Helper\FormHelper::control() method of the FormHelper to
create our form elements, our validation error messages will be shown automatically.
Editing Articles
Post editing: here we go. You’re a CakePHP pro by now, so you should have picked up a pattern. Make the action,
then the view. Here’s what the edit() action of the ArticlesController would look like:
// src/Controller/ArticlesController.php
$this->set('article', $article);
}
This action first ensures that the user has tried to access an existing record. If they haven’t passed in an $id parameter,
or the article does not exist, we throw a NotFoundException for the CakePHP ErrorHandler to take care of.
Next the action checks whether the request is either a POST or a PUT request. If it is, then we use the POST data to
update our article entity by using the patchEntity() method. Finally we use the table object to save the entity
back or kick back and show the user validation errors.
The edit view might look something like this:
<!-- File: src/Template/Articles/edit.ctp -->
<h1>Edit Article</h1>
<?php
echo $this->Form->create($article);
echo $this->Form->control('title');
echo $this->Form->control('body', ['rows' => '3']);
echo $this->Form->button(__('Save Article'));
echo $this->Form->end();
?>
This view outputs the edit form (with the values populated), along with any necessary validation error messages.
CakePHP will determine whether a save() generates an insert or an update statement based on the state of the entity.
You can now update your index view with links to edit specific articles:
<h1>Blog articles</h1>
<p><?= $this->Html->link("Add Article", ['action' => 'add']) ?></p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
<th>Action</th>
</tr>
<!-- Here's where we iterate through our $articles query object, printing out article
˓→info -->
</td>
<td>
<?= $article->created->format(DATE_RFC850) ?>
</td>
<td>
<?= $this->Html->link('Edit', ['action' => 'edit', $article->id]) ?>
</td>
</tr>
<?php endforeach; ?>
</table>
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
$article = $this->Articles->get($id);
if ($this->Articles->delete($article)) {
$this->Flash->success(__('The article with id: {0} has been deleted.', h(
˓→$id)));
}
}
This logic deletes the article specified by $id, and uses $this->Flash->success() to show the user a confir-
mation 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:
<h1>Blog articles</h1>
<p><?= $this->Html->link('Add Article', ['action' => 'add']) ?></p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
<th>Actions</th>
</tr>
<!-- Here's where we loop through our $articles query object, printing out article
˓→info -->
</td>
<td>
<?= $article->created->format(DATE_RFC850) ?>
</td>
<td>
<?= $this->Form->postLink(
'Delete',
['action' => 'delete', $article->id],
['confirm' => 'Are you sure?'])
?>
<?= $this->Html->link('Edit', ['action' => 'edit', $article->id]) ?>
</td>
</tr>
<?php endforeach; ?>
</table>
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:
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:
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
‘/’. 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 API57 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!
These are common tasks people learning CakePHP usually want to study next:
1. Layouts: Customizing your website layout
57 https://api.cakephp.org
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 plugin58 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:
The migrations plugin will now be in your application’s plugins folder. Also, add
$this->addPlugin('Migrations'); to your application’s bootstrap method.
Once the plugin is loaded, run the following command to create a migration file:
A migration file will be generated in the /config/Migrations folder with the following:
<?php
use Migrations\AbstractMigration;
58 https://github.com/cakephp/migrations
Run another command to create a categories table. If you need to specify a field length, you can do it within
brackets in the field type, ie:
bin/cake bake migration CreateCategories parent_id:integer lft:integer[10]
˓→rght:integer[10] name:string[100] description:string created modified
use Migrations\AbstractMigration;
$table->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
]);
$table->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
]);
$table->create();
}
}
Now that the migration files are created, you can edit them before creating your tables. We need to change the 'null'
=> false for the parent_id field with 'null' => true because a top-level category has a null parent_id.
Run the following command to create your tables:
With our tables set up, we can now focus on categorizing our articles.
We suppose you already have the files (Tables, Controllers and Templates of Articles) from part 2. So we’ll just add
the references to categories.
We need to associate the Articles and Categories tables together. Open the src/Model/Table/ArticlesTable.php file
and add the following:
// src/Model/Table/ArticlesTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
The bake tool has created all your files in a snap. You can give them a quick read if you want re-familiarize yourself
with how CakePHP works.
echo $this->Form->control('parent_id', [
'options' => $parentCategories,
'empty' => 'No parent category'
]);
The TreeBehavior helps you manage hierarchical Tree structures in database table. It uses the MPTT logic59 to manage
the data. MPTT tree structures are optimized for reads, which often makes them a good fit for read heavy applications
like blogs.
If you open the src/Model/Table/CategoriesTable.php file, you’ll see that the TreeBehavior has been attached to your
CategoriesTable in the initialize() method. Bake adds this behavior to any Tables that contain lft and rght
columns:
$this->addBehavior('Tree');
With the TreeBehavior attached you’ll be able to access some features like reordering the categories. We’ll see that in
a moment.
But for now, you have to remove the following controls in your Categories add and edit template files:
echo $this->Form->control('lft');
echo $this->Form->control('rght');
In addition you should disable or remove the requirePresence from the validator for both the lft and rght columns
in your CategoriesTable model:
$validator
->add('lft', 'valid', ['rule' => 'numeric'])
// ->requirePresence('lft', 'create')
->notEmpty('lft');
$validator
->add('rght', 'valid', ['rule' => 'numeric'])
// ->requirePresence('rght', 'create')
59 http://www.sitepoint.com/hierarchical-data-database-2/
->notEmpty('rght');
}
These fields are automatically managed by the TreeBehavior when a category is saved.
Using your web browser, add some new categories using the /yoursite/categories/add controller action.
In your categories index template file, you can list the categories and re-order them.
Let’s modify the index method in your CategoriesController.php and add moveUp() and moveDown() methods
to be able to reorder the categories in the tree:
}
return $this->redirect($this->referer(['action' => 'index']));
}
}
return $this->redirect($this->referer(['action' => 'index']));
}
}
</ul>
</div>
<div class="categories index large-10 medium-9 columns">
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>Id</th>
<th>Parent Id</th>
<th>Lft</th>
<th>Rght</th>
<th>Name</th>
<th>Description</th>
<th>Created</th>
<th class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($categories as $category): ?>
<tr>
<td><?= $category->id ?></td>
<td><?= $category->parent_id ?></td>
<td><?= $category->lft ?></td>
<td><?= $category->rght ?></td>
<td><?= h($category->name) ?></td>
<td><?= h($category->description) ?></td>
<td><?= h($category->created) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $category->
˓→id]) ?>
˓→$category->id)]) ?>
˓→$category->id)]) ?>
˓→$category->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
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;
// ...
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);
<h1>Add Article</h1>
<?php
echo $this->Form->create($article);
// just added the categories control
echo $this->Form->control('category_id');
echo $this->Form->control('title');
echo $this->Form->control('body', ['rows' => '3']);
echo $this->Form->button(__('Save Article'));
echo $this->Form->end();
When you go to the address /yoursite/categories/add you should see a list of categories to choose.
Following our Blog Tutorial example, imagine we wanted to secure access to certain URLs, based on the logged-in
user. We also have another requirement: to allow our blog to have multiple authors who can create, edit, and delete
their own articles while disallowing other authors from making changes to articles they do not own.
First, let’s create a new table in our blog database to hold our users’ data:
We have adhered to the CakePHP conventions in naming tables, but we’re also taking advantage of another convention:
By using the username and password columns in a users table, CakePHP will be able to auto-configure most things
for us when implementing the user login.
Next step is to create our UsersTable class, responsible for finding, saving and validating any user data:
// src/Model/Table/UsersTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
Let’s also create our UsersController. The following content corresponds to parts of a basic baked UsersController
class using the code generation utilities bundled with CakePHP:
// src/Controller/UsersController.php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
$this->Auth->allow('add');
}
In the same way we created the views for our articles by using the code generation tool, we can implement the user
views. For the purpose of this tutorial, we will show just the add.ctp:
We’re now ready to add our authentication layer. In CakePHP this is handled by the
Cake\Controller\Component\AuthComponent, a class responsible for requiring login for certain
actions, handling user login and logout, and also authorizing logged-in users to the actions they are allowed to reach.
To add this component to your application open your src/Controller/AppController.php file and add the following
lines:
// src/Controller/AppController.php
namespace App\Controller;
use Cake\Controller\Controller;
use Cake\Event\Event;
There is not much to configure, as we used the conventions for the users table. We just set up the URLs that will be
loaded after the login and logout actions is performed, in our case to /articles/ and / respectively.
What we did in the beforeFilter() function was to tell the AuthComponent to not require a login for all
index() and view() actions, in every controller. We want our visitors to be able to read and list the entries
without registering in the site.
Now, we need to be able to register new users, save their username and password, and more importantly, hash their
password so it is not stored as plain text in our database. Let’s tell the AuthComponent to let un-authenticated users
access the users add function and implement the login and logout action:
// src/Controller/UsersController.php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
{
parent::beforeFilter($event);
// Allow users to register and logout.
// You should not add the "login" action to allow list. Doing so would
// cause problems with normal functioning of AuthComponent.
$this->Auth->allow(['add', 'logout']);
}
Password hashing is not done yet, we need an Entity class for our User in order to handle its own specific logic. Create
the src/Model/Entity/User.php entity file and add the following:
// src/Model/Entity/User.php
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
// Make all fields mass assignable except for primary key field "id".
protected $_accessible = [
'*' => true,
'id' => false
];
// ...
// ...
}
Now every time the password property is assigned to the user it will be hashed using the
DefaultPasswordHasher class. We’re just missing a template view file for the login function. Open up
You can now register a new user by accessing the /users/add URL and log in with the newly created credentials
by going to /users/login URL. Also, try to access any other URL that was not explicitly allowed such as /
articles/add, you will see that the application automatically redirects you to the login page.
And that’s it! It looks too simple to be true. Let’s go back a bit to explain what happened. The beforeFilter()
function is telling the AuthComponent to not require a login for the add() action in addition to the index() and
view() actions that were already allowed in the AppController’s beforeFilter() function.
The login() action calls the $this->Auth->identify() function in the AuthComponent, and it works with-
out any further config because we are following conventions as mentioned earlier. That is, having a Users table with
a username and a password column, and use a form posted to a controller with the user data. This function returns
whether the login was successful or not, and in the case it succeeds, then we redirect the user to the configured redi-
rection URL that we used when adding the AuthComponent to our application.
The logout works by just accessing the /users/logout URL and will redirect the user to the configured logoutUrl
formerly described. This URL is the result of the AuthComponent::logout() function on success.
As stated before, we are converting this blog into a multi-user authoring tool, and in order to do this, we need to modify
the articles table a bit to add the reference to the Users table:
Also, a small change in the ArticlesController is required to store the currently logged in user as a reference for the
created article:
// src/Controller/ArticlesController.php
The user() function provided by the component returns any column from the currently logged in user. We used this
method to add the data into the request info that is saved.
Let’s secure our app to prevent some authors from editing or deleting the others’ articles. Basic rules for our app are
that admin users can access every URL, while normal users (the author role) can only access the permitted actions.
Again, open the AppController class and add a few more options to the Auth config:
// src/Controller/AppController.php
// Default deny
return false;
}
We just created a simple authorization mechanism. Users with the admin role will be able to access any URL in the
site when logged-in. All other users – those with the author role – will have the same access as users who aren’t
logged-in.
This is not exactly what we want. We need to supply more rules to our isAuthorized() method. However instead
of doing it in AppController, we’ll delegate supplying those extra rules to each individual controller. The rules we’re
going to add to ArticlesController should permit authors to create articles but prevent authors from editing articles they
do not own. Add the following content to your ArticlesController.php:
// src/Controller/ArticlesController.php
return parent::isAuthorized($user);
}
We’re now overriding the AppController’s isAuthorized() call and internally checking if the parent class is
already authorizing the user. If he isn’t, then just allow him to access the add action, and conditionally access edit and
delete. One final thing has not been implemented. To tell whether or not the user is authorized to edit the article, we’re
calling a isOwnedBy() function in the Articles table. Let’s then implement that function:
// src/Model/Table/ArticlesTable.php
This concludes our simple authentication and authorization tutorial. For securing the UsersController you can follow
the same technique we did for ArticlesController. You could also be more creative and code something more general
in AppController based on your own rules.
Should you need more control, we suggest you read the complete Auth guide in the AuthComponent section where
you will find more about configuring the component, creating custom Authorization classes, and much more.
Contributing
There are a number of ways you can contribute to CakePHP. The following sections cover the various ways you can
contribute to CakePHP:
Documentation
Contributing to the documentation is simple. The files are hosted on https://github.com/cakephp/docs. Feel free to
fork the repo, add your changes/improvements/translations and give back by issuing a pull request. You can even edit
the docs online with GitHub, without ever downloading the files – the “Improve this Doc” button on any given page
will direct you to GitHub’s online editor for that page.
CakePHP documentation is continuously integrated60 , and deployed after each pull request is merged.
Translations
Email the docs team (docs at cakephp dot org) or hop on IRC (#cakephp on freenode) to discuss any translation efforts
you would like to participate in.
We want to provide translations that are as complete as possible. However, there may be times where a translation file
is not up-to-date. You should always consider the English version as the authoritative version.
If your language is not in the current languages, please contact us through Github and we will consider creating a
skeleton folder for it. The following sections are the first one you should consider translating as these files don’t
change often:
• index.rst
60 http://en.wikipedia.org/wiki/Continuous_integration
117
CakePHP Cookbook Documentation, Release 3.8
• intro.rst
• quickstart.rst
• installation.rst
• /intro folder
• /tutorials-and-examples folder
The structure of all language folders should mirror the English folder structure. If the structure changes for the English
version, we should apply those changes in the other languages.
For example, if a new English file is created in en/file.rst, we should:
• Add the file in all other languages : fr/file.rst, zh/file.rst, . . .
• Delete the content, but keeping the title, meta information and eventual toc-tree elements. The follow-
ing note will be added while nobody has translated the file:
File Title
##########
.. note::
The documentation is not currently supported in XX language for this
page.
You can refer to the English version in the select top menu to have
information about this page's topic.
one-toc-file
other-toc-file
.. meta::
:title lang=xx: File Title
:keywords lang=xx: title, description,...
Translator tips
• Browse and edit in the language you want the content to be translated to - otherwise you won’t see what has
already been translated.
• Feel free to dive right in if your chosen language already exists on the book.
• Use Informal Form61 .
• Translate both the content and the title at the same time.
61 http://en.wikipedia.org/wiki/Register_(linguistics)
• Do compare to the English content before submitting a correction (if you correct something, but don’t integrate
an ‘upstream’ change your submission won’t be accepted).
• If you need to write an English term, wrap it in <em> tags. E.g. “asdf asdf Controller asdf” or “asdf asdf
Kontroller (Controller) asfd” as appropriate.
• Do not submit partial translations.
• Do not edit a section with a pending change.
• Do not use HTML entities62 for accented characters, the book uses UTF-8.
• Do not significantly change the markup (HTML) or add new content.
• If the original content is missing some info, submit an edit for that first.
The new CakePHP documentation is written with ReST formatted text63 . ReST (Re Structured Text) is a plain text
markup syntax similar to markdown, or textile. To maintain consistency it is recommended that when adding to the
CakePHP documentation you follow the guidelines here on how to format and structure your text.
Line Length
Lines of text should be wrapped at 80 columns. The only exception should be long URLs, and code snippets.
Section headers are created by underlining the title with punctuation characters at least the length of the text.
• # Is used to denote page titles.
• = Is used for sections in a page.
• - Is used for subsections.
• ~ Is used for sub-subsections.
• ^ Is used for sub-sub-subsections.
Headings should not be nested more than 5 levels deep. Headings should be preceded and followed by a blank line.
Paragraphs
Paragraphs are simply blocks of text, with all the lines at the same level of indentation. Paragraphs should be separated
by one blank line.
Inline Markup
• One asterisk: text for emphasis (italics) We’ll use it for general highlighting/emphasis.
– *text*.
• Two asterisks: text for strong emphasis (boldface) We’ll use it for working directories, bullet list subject, table
names and excluding the following word “table”.
62 http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
63 http://en.wikipedia.org/wiki/ReStructuredText
Documentation 119
CakePHP Cookbook Documentation, Release 3.8
Lists
List markup is very similar to markdown. Unordered lists are indicated by starting a line with a single asterisk and a
space. Numbered lists can be created with either numerals, or # for auto numbering:
* This is a bullet
* So is this. But this line
has two lines.
1. First line
2. Second line
#. Automatic numbering
#. Will save you some time.
Indented lists can also be created, by indenting sections and separating them with an empty line:
* First line
* Second line
* Going deeper
* Whoah
term
definition
CakePHP
An MVC framework for PHP
Terms cannot be more than one line, but definitions can be multi-line and all lines should be indented consistently.
Links
There are several kinds of links, each with their own uses.
External Links
The resulting link would look like this: External Link to php.net64
:doc:
Other pages in the documentation can be linked to using the :doc: role. You can link to the specified doc-
ument using either an absolute or relative path reference. You should omit the .rst extension. For exam-
ple, if the reference :doc:`form` appears in the document core-helpers/html, then the link refer-
ences core-helpers/form. If the reference was :doc:`/core-helpers`, it would always reference
/core-helpers regardless of where it was used.
:ref:
You can cross reference any arbitrary title in any document using the :ref: role. Link label targets
must be unique across the entire documentation. When creating labels for class methods, it’s best to use
class-method as the format for your link label.
The most common use of labels is above a title. Example:
.. _label-name:
Section heading
---------------
Elsewhere you could reference the above section using :ref:`label-name`. The link’s text would
be the title that the link preceded. You can also provide custom link text using :ref:`Link text
<label-name>`.
Sphinx will output warnings if a file is not referenced in a toc-tree. It’s a great way to ensure that all files have a link
directed to them, but sometimes, you don’t need to insert a link for a file, eg. for our epub-contents and pdf-contents
files. In those cases, you can add :orphan: at the top of the file, to suppress warnings that the file is not in the
toc-tree.
The CakePHP documentation uses the phpdomain65 to provide custom directives for describing PHP objects and
constructs. Using these directives and roles is required to give proper indexing and cross referencing features.
64 http://php.net
65 http://pypi.python.org/pypi/sphinxcontrib-phpdomain
Documentation 121
CakePHP Cookbook Documentation, Release 3.8
.. php:class:: MyClass
Class description
.. php:method:: method($argument)
Method description
Attributes, methods and constants don’t need to be nested. They can also just follow the class declaration:
.. php:class:: MyClass
.. php:method:: methodName()
See also:
php:method, php:attr, php:const
.. php:method:: name(signature)
Describe a class method, its arguments, return value, and exceptions:
.. php:staticmethod:: ClassName::methodName(signature)
Describe a static method, its arguments, return value and exceptions, see php:method for options.
.. php:attr:: name
Describe an property/attribute on a class.
Sphinx will output warnings if a function is referenced in multiple files. It’s a great way to ensure that you did not
add a function two times, but sometimes, you actually want to write a function in two or more files, eg. debug object
is referenced in /development/debugging and in /core-libraries/global-constants-and-functions. In this case, you can
add :noindex: under the function debug to suppress warnings. Keep only one reference without :no-index:
to still have the function referenced:
Cross Referencing
The following roles refer to PHP objects and links are generated if a matching directive is found:
:php:func:
Reference a PHP function.
:php:global:
Reference a global variable whose name has $ prefix.
:php:const:
Reference either a global constant, or a class constant. Class constants should be preceded by the owning class:
:php:class:
Reference a class by name:
:php:class:`ClassName`
:php:meth:
Reference a method of a class. This role supports both kinds of methods:
:php:meth:`DateTime::setDate`
:php:meth:`Classname::staticMethod`
:php:attr:
Reference a property on an object:
:php:attr:`ClassName::$propertyName`
:php:exc:
Reference an exception.
Source Code
Literal code blocks are created by ending a paragraph with ::. The literal block must be indented, and like all
paragraphs be separated by single lines:
Documentation 123
CakePHP Cookbook Documentation, Release 3.8
This is a paragraph::
while ($i--) {
doStuff()
}
Literal text is not modified or formatted, save that one level of indentation is removed.
There are often times when you want to inform the reader of an important tip, special note or a potential hazard.
Admonitions in sphinx are used for just that. There are fives kinds of admonitions.
• .. tip:: Tips are used to document or re-iterate interesting or important information. The content of the
directive should be written in complete sentences and include all appropriate punctuation.
• .. note:: Notes are used to document an especially important piece of information. The content of the
directive should be written in complete sentences and include all appropriate punctuation.
• .. warning:: Warnings are used to document potential stumbling blocks, or information pertaining to secu-
rity. The content of the directive should be written in complete sentences and include all appropriate punctuation.
• .. versionadded:: X.Y.Z “Version added” admonitions are used to display notes specific to new fea-
tures added at a specific version, X.Y.Z being the version on which the said feature was added.
• .. deprecated:: X.Y.Z As opposed to “version added” admonitions, “deprecated” admonition are
used to notify of a deprecated feature, X.Y.Z being the version on which the said feature was deprecated.
All admonitions are made the same:
.. note::
Samples
New in version 2.6.3: This awesome feature was added on version 2.6.3
Deprecated since version 2.6.3: This old feature was deprecated on version 2.6.3
Tickets
Getting feedback and help from the community in the form of tickets is an extremely important part of the CakePHP
development process. All of CakePHP’s tickets are hosted on GitHub66 .
Reporting Bugs
Well written bug reports are very helpful. There are a few steps to help create the best bug report possible:
• Do: Please search67 for a similar existing ticket, and ensure someone hasn’t already reported your issue, or that
it hasn’t already been fixed in the repository.
• Do: Please include detailed instructions on how to reproduce the bug. This could be in the form of a test-case
or a snippet of code that demonstrates the issue. Not having a way to reproduce an issue means it’s less likely
to get fixed.
• Do: Please give as many details as possible about your environment: (OS, PHP version, CakePHP version).
• Don’t: Please don’t use the ticket system to ask support questions. The #cakephp IRC channel on Freenode68
has many developers available to help answer your questions. Also have a look at Stack Overflow69 .
If you’ve found a security issue in CakePHP, please use the following procedure instead of the normal bug reporting
system. Instead of using the bug tracker, mailing list or IRC please send an email to security [at] cakephp.org. Emails
sent to this address go to the CakePHP core team on a private mailing list.
For each report, we try to first confirm the vulnerability. Once confirmed, the CakePHP team will take the following
actions:
• Acknowledge to the reporter that we’ve received the issue, and are working on a fix. We ask that the reporter
keep the issue confidential until we announce it.
• Get a fix/patch prepared.
• Prepare a post describing the vulnerability, and the possible exploits.
• Release new versions of all affected versions.
• Prominently feature the problem in the release announcement.
Code
Patches and pull requests are a great way to contribute code back to CakePHP. Pull requests can be created in GitHub,
and are preferred over patch files in ticket comments.
Initial Setup
Before working on patches for CakePHP, it’s a good idea to get your environment setup. You’ll need the following
software:
66 https://github.com/cakephp/cakephp/issues
67 https://github.com/cakephp/cakephp/search?q=it+is+broken&ref=cmdform&type=Issues
68 https://webchat.freenode.net
69 https://stackoverflow.com/questions/tagged/cakephp
Tickets 125
CakePHP Cookbook Documentation, Release 3.8
• Git
• PHP 5.6.0 or greater
• PHPUnit 5.7.0 or greater
Set up your user information with your name/handle and working email address:
Note: If you are new to Git, we highly recommend you to read the excellent and free ProGit70 book.
Add the original CakePHP repository as a remote repository. You’ll use this later to fetch changes from the CakePHP
repository. This will let you stay up to date with CakePHP:
cd cakephp
git remote add upstream git://github.com/cakephp/cakephp.git
Now that you have CakePHP setup you should be able to define a $test database connection, and run all the tests.
Working on a Patch
Each time you want to work on a bug, feature or enhancement create a topic branch.
The branch you create should be based on the version that your fix/enhancement is for. For example if you are fixing
a bug in 3.x you would want to use the master branch as the base for your branch. If your change is a bug fix for
the 2.x release series, you should use the 2.x branch:
Tip: Use a descriptive name for your branch, referencing the ticket or feature name is a good convention. e.g.
ticket-1234, feature-awesome
The above will create a local branch based on the upstream (CakePHP) 2.x branch. Work on your fix, and make as
many commits as you need; but keep in mind the following:
70 http://git-scm.com/book/
71 http://github.com
72 http://github.com/cakephp/cakephp
Once your changes are done and you’re ready for them to be merged into CakePHP, you’ll want to update your branch:
This will fetch + merge in any changes that have happened in CakePHP since you started. It will then rebase - or
replay your changes on top of the current code. You might encounter a conflict during the rebase. If the rebase quits
early you can see which files are conflicted/un-merged with git status. Resolve each conflict, and then continue
the rebase:
Check that all your tests continue to pass. Then push your branch to your fork:
If you’ve rebased after pushing your branch, you’ll need to use force push:
Once your branch is on GitHub, you can submit a pull request on GitHub.
When making pull requests you should make sure you select the correct base branch, as you cannot edit it once the
pull request is created.
• If your change is a bugfix and doesn’t introduce new functionality and only corrects existing behavior that is
present in the current release. Then choose master as your merge target.
• If your change is a new feature or an addition to the framework, then you should choose the branch with the
next version number. For example if the current stable release is 3.6.0, the branch accepting new features will
be 3.next.
• If your change is a breaks existing functionality, or API’s then you’ll have to choose then next major release.
For example, if the current release is 3.6.0 then the next time existing behavior can be broken will be in 4.x
so you should target that branch.
Note: Remember that all code you contribute to CakePHP will be licensed under the MIT License, and the Cake
Software Foundation73 will become the owner of any contributed code. Contributors should follow the CakePHP
73 http://cakefoundation.org/pages/about
Code 127
CakePHP Cookbook Documentation, Release 3.8
Community Guidelines74 .
All bug fixes merged into a maintenance branch will also be merged into upcoming releases periodically by the core
team.
Coding Standards
CakePHP developers will use the PSR-2 coding style guide75 in addition to the following rules as coding standards.
It is recommended that others developing CakeIngredients follow the same standards.
You can use the CakePHP Code Sniffer76 to check that your code follows required standards.
No new features should be added, without having their own tests – which should be passed before committing them to
the repository.
IDE Setup
Please make sure your IDE is set up to “trim right” on whitespaces. There should be no trailing spaces per line.
Most modern IDEs also support an .editorconfig file. The CakePHP app skeleton ships with it by default. It
already contains best practise defaults.
We recommend to use the IdeHelper77 plugin if you want to maximize IDE compatibility. It will assist to keep the
annotations up-to-date which will make the IDE fully understand how all classes work together and provides better
type-hinting and auto-completion.
Indentation
// base level
// level 1
// level 2
// level 1
// base level
Or:
$booleanVariable = true;
$stringVariable = 'moose';
if ($booleanVariable) {
echo 'Boolean value is true';
if ($stringVariable === 'moose') {
echo 'We have encountered a moose';
74 http://community.cakephp.org/guidelines
75 http://www.php-fig.org/psr/psr-2/
76 https://github.com/cakephp/cakephp-codesniffer
77 https://github.com/dereuromark/cakephp-ide-helper
}
}
In cases where you’re using a multi-line function call use the following guidelines:
• Opening parenthesis of a multi-line function call must be the last content on the line.
• Only one argument is allowed per line in a multi-line function call.
• Closing parenthesis of a multi-line function call must be on a line by itself.
As an example, instead of using the following formatting:
$matches = array_intersect_key($this->_listeners,
array_flip(preg_grep($matchPattern,
array_keys($this->_listeners), 0)));
$matches = array_intersect_key(
$this->_listeners,
array_flip(
preg_grep($matchPattern, array_keys($this->_listeners), 0)
)
);
Line Length
It is recommended to keep lines at approximately 100 characters long for better code readability. A limit of 80 or 120
characters makes it necessary to distribute complex logic or expressions by function, as well as give functions and
objects shorter, more expressive names. Lines must not be longer than 120 characters.
In short:
• 100 characters is the soft limit.
• 120 characters is the hard limit.
Control Structures
Control structures are for example “if”, “for”, “foreach”, “while”, “switch” etc. Below, an example with
“if”:
if ((expr_1) || (expr_2)) {
// action_1;
} elseif (!(expr_3) && (expr_4)) {
// action_2;
} else {
// default_action;
}
• In the control structures there should be 1 (one) space before the first parenthesis and 1 (one) space between the
last parenthesis and the opening bracket.
• Always use curly brackets in control structures, even if they are not needed. They increase the readability of the
code, and they give you fewer logical errors.
• Opening curly brackets should be placed on the same line as the control structure. Closing curly brackets should
be placed on new lines, and they should have same indentation level as the control structure. The statement
included in curly brackets should begin on a new line, and code contained within it should gain a new level of
indentation.
• Inline assignments should not be used inside of the control structures.
// wrong = no brackets
if (expr)
statement;
// good
if (expr) {
statement;
}
// good
$variable = Class::function();
if ($variable) {
statement;
}
Ternary Operator
Ternary operators are permissible when the entire ternary operation fits on one line. Longer ternaries should be split
into if else statements. Ternary operators should not ever be nested. Optionally parentheses can be used around
the condition check of the ternary for clarity:
Template Files
In template files (.ctp files) developers should use keyword control structures. Keyword control structures are easier
to read in complex template files. Control structures can either be contained in a larger PHP block, or in separate PHP
tags:
<?php
if ($isAdmin):
echo '<p>You are the admin user.</p>';
endif;
?>
<p>The following is also acceptable:</p>
Comparison
Always try to be as strict as possible. If a non-strict test is deliberate it might be wise to comment it as such to avoid
confusing it for a mistake.
For testing if a variable is null, it is recommended to use a strict check:
// not recommended
if (null === $this->foo()) {
// ...
}
// recommended
if ($this->foo() === null) {
// ...
}
Function Calls
Functions should be called without space between function’s name and starting parenthesis. There should be one space
between every parameter of a function call:
As you can see above there should be one space on both sides of equals sign (=).
Method Definition
return $var;
}
Parameters with a default value, should be placed last in function definition. Try to make your functions return
something, at least true or false, so it can be determined whether the function call was successful:
if (!($dnsInfo) || !($dnsInfo['phpType'])) {
return $this->addError();
}
return true;
}
Bail Early
...
}
...
}
This helps to keep the code flow simple and easy to follow.
Typehinting
Arguments that expect objects, arrays or callbacks (callable) can be typehinted. We only typehint public methods,
though, as typehinting is not cost-free:
/**
* Some method description.
*
* @param \Cake\ORM\Table $table The table class to use.
* @param array $array Some array value.
* @param callable $callback Some callback.
* @param bool $boolean Some boolean value.
*/
public function foo(Table $table, array $array, callable $callback, $boolean)
{
}
Here $table must be an instance of \Cake\ORM\Table, $array must be an array and $callback must be
of type callable (a valid callback).
Note that if you want to allow $array to be also an instance of \ArrayObject you should not typehint as array
accepts only the primitive type:
/**
* Some method description.
*
* @param array|\ArrayObject $array Some array value.
*/
public function foo($array)
{
}
Defining anonymous functions follows the PSR-278 coding style guide, where they are declared with a space after the
function keyword, and a space before and after the use keyword:
$closure = function ($arg1, $arg2) use ($var1, $var2) {
// code
};
Method Chaining
Method chaining should have multiple methods spread across separate lines, and indented with four spaces:
$email->from('[email protected]')
->to('[email protected]')
->subject('A great message')
->send();
Commenting Code
All comments should be written in English, and should in a clear way describe the commented block of code.
Comments can include the following phpDocumentor79 tags:
• @author80
• @copyright81
• @deprecated82 Using the @version <vector> <description> format, where version and
description are mandatory. Version refers to the one it got deprecated in.
78 http://www.php-fig.org/psr/psr-2/
79 http://phpdoc.org
80 http://phpdoc.org/docs/latest/references/phpdoc/tags/author.html
81 http://phpdoc.org/docs/latest/references/phpdoc/tags/copyright.html
82 http://phpdoc.org/docs/latest/references/phpdoc/tags/deprecated.html
• @example83
• @ignore84
• @internal85
• @link86
• @see87
• @since88
• @version89
PhpDoc tags are very much like JavaDoc tags in Java. Tags are only processed if they are the first thing in a DocBlock
line, for example:
/**
* Tag example.
*
* @author this tag is parsed, but this @version is ignored
* @version 1.0 this tag is also parsed
*/
/**
* Example of inline phpDoc tags.
*
* This function works hard with foo() to rule the world.
*
* @return void
*/
function bar()
{
}
/**
* Foo function.
*
* @return void
*/
function foo()
{
}
Comment blocks, with the exception of the first block in a file, should always be preceded by a newline.
Variable Types
int|bool
For more than two types it is usually best to just use mixed.
When returning the object itself, e.g. for chaining, one should use $this instead:
/**
* Foo function.
*
* @return $this
*/
public function foo()
{
return $this;
}
Including Files
// wrong = parentheses
require_once('ClassFileName.php');
require_once ($class);
// good = no parentheses
require_once 'ClassFileName.php';
require_once $class;
When including files with classes or libraries, use only and always the require_once90 function.
PHP Tags
Always use long tags (<?php ?>) instead of short tags (<? ?>). The short echo should be used in template files
(.ctp) where appropriate.
90 http://php.net/require_once
Short Echo
The short echo should be used in template files in place of <?php echo. It should be immediately followed by a
single space, the variable or function value to echo, a single space, and the php closing tag:
As of PHP 5.4 the short echo tag (<?=) is no longer to be consider a ‘short tag’ is always available regardless of the
short_open_tag ini directive.
Naming Convention
Functions
function longFunctionName()
{
}
Classes
class ExampleClass
{
}
Variables
Variable names should be as descriptive as possible, but also as short as possible. All variables should start with a
lowercase letter, and should be written in camelBack in case of multiple words. Variables referencing objects should
in some way associate to the class the variable is an object of. Example:
$user = 'John';
$users = ['John', 'Hans', 'Arne'];
Member Visibility
Use PHP’s public, protected and private keywords for methods and variables.
Example Addresses
For all example URL and mail addresses use “example.com”, “example.org” and “example.net”, for example:
• Email: [email protected]
• WWW: http://www.example.com
• FTP: ftp://ftp.example.com
The “example.com” domain name has been reserved for this (see RFC 260691 ) and is recommended for use in docu-
mentation or as examples.
Files
File names which do not contain classes should be lowercased and underscored, for example:
long_file_name.php
Casting
Constants
define('CONSTANT', 1);
If a constant name consists of multiple words, they should be separated by an underscore character, for example:
define('LONG_NAMED_CONSTANT', 2);
While empty() is an easy to use function, it can mask errors and cause unintended effects when '0' and 0 are
given. When variables or properties are already defined, the usage of empty() is not recommended. When working
with variables, it is better to rely on type-coercion to boolean instead of empty():
91 https://tools.ietf.org/html/rfc2606.html
function manipulate($var)
{
// Not recommended, $var is already defined in the scope
if (empty($var)) {
// ...
}
When dealing with defined properties you should favour null checks over empty()/isset() checks:
class Thing
{
private $property; // Defined
}
}
}
When working with arrays, it is better to merge in defaults over using empty() checks. By merging in defaults, you
can ensure that required keys are defined:
// Recommended
if ($array['key'] !== null) {
// ...
}
}
Ensuring that you can upgrade your applications easily and smoothly is important to us. That’s why we only break
compatibility at major release milestones. You might be familiar with semantic versioning92 , which is the general
guideline we use on all CakePHP projects. In short, semantic versioning means that only major releases (such as 2.0,
3.0, 4.0) can break backwards compatibility. Minor releases (such as 2.1, 3.1, 3.2) may introduce new features, but
are not allowed to break compatibility. Bug fix releases (such as 2.1.2, 3.0.1) do not add new features, but fix bugs or
enhance performance only.
Note: Deprecations are removed with the next major version of the framework. It is advised to early on adapt your
code already each minor as outlined in the deprecation comments and the migration guides.
To clarify what changes you can expect in each release tier we have more detailed information for developers using
CakePHP, and for developers working on CakePHP that helps set expectations of what can be done in minor releases.
Major releases can have as many breaking changes as required.
Migration Guides
For each major and minor release, the CakePHP team will provide a migration guide. These guides explain the new
features and any breaking changes that are in each release. They can be found in the Appendices section of the
cookbook.
Using CakePHP
If you are building your application with CakePHP, the following guidelines explain the stability you can expect.
Interfaces
Outside of major releases, interfaces provided by CakePHP will not have any existing methods changed. New methods
may be added, but no existing methods will be changed.
Classes
Classes provided by CakePHP can be constructed and have their public methods and properties used by application
code and outside of major releases backwards compatibility is ensured.
Note: Some classes in CakePHP are marked with the @internal API doc tag. These classes are not stable and do
not have any backwards compatibility promises.
In minor releases, new methods may be added to classes, and existing methods may have new arguments added. Any
new arguments will have default values, but if you’ve overridden methods with a differing signature you may see fatal
errors. Methods that have new arguments added will be documented in the migration guide for that release.
The following table outlines several use cases and what compatibility you can expect from CakePHP:
92 http://semver.org/
Working on CakePHP
If you are helping make CakePHP even better please keep the following guidelines in mind when adding/changing
functionality:
In a minor release you can:
Deprecations
In each minor release, features may be deprecated. If features are deprecated, API documentation and runtime warn-
ings will be added. Runtime errors help you locate code that needs to be updated before it breaks. If you wish to
disable runtime warnings you can do so using the Error.errorLevel configuration value:
// in config/app.php
// ...
'Error' => [
'errorLevel' => E_ALL ^ E_USER_DEPRECATED,
]
// ...
2 You can change a class/method name as long as the old name remains available. This is generally avoided unless renaming has significant
benefit.
3 Avoid whenever possible. Any removals need to be documented in the migration guide.
Installation
Note: In XAMPP, intl extension is included but you have to uncomment extension=php_intl.dll in php.ini
and restart the server through the XAMPP Control Panel.
In WAMP, the intl extension is “activated” by default but not working. To make it work you have to go to php folder
(by default) C:\wamp\bin\php\php{version}, copy all the files that looks like icu*.dll and paste them into the apache
bin directory C:\wamp\bin\apache\apache{version}\bin. Then restart all services and it should be OK.
While a database engine isn’t required, we imagine that most applications will utilize one. CakePHP supports a variety
of database storage engines:
• MySQL (5.5.3 or greater)
• MariaDB (5.5 or greater)
• PostgreSQL
• Microsoft SQL Server (2008 or higher)
• SQLite 3
143
CakePHP Cookbook Documentation, Release 3.8
Note: All built-in drivers require PDO. You should make sure you have the correct PDO extensions installed.
Installing CakePHP
Before starting you should make sure that your PHP version is up to date:
php -v
You should have PHP 5.6.0 (CLI) or higher. Your webserver’s PHP version must also be of 5.6.0 or higher, and should
be the same version your command line interface (CLI) uses.
Installing Composer
CakePHP uses Composer93 , a dependency management tool, as the officially supported method for installation.
• Installing Composer on Linux and macOS
1. Run the installer script as described in the official Composer documentation94 and follow the instructions
to install Composer.
2. Execute the following command to move the composer.phar to a directory that is in your path:
mv composer.phar /usr/local/bin/composer
You can create a new CakePHP application using composer’s create-project command :
Once Composer finishes downloading the application skeleton and the core CakePHP library, you should have a
functioning CakePHP application installed via Composer. Be sure to keep the composer.json and composer.lock files
with the rest of your source code.
You can now visit the path to where you installed your CakePHP application and see the default home page. To change
the content of this page, edit src/Template/Pages/home.ctp.
Although composer is the recommended installation method, there are pre-installed downloads available on Github97 .
Those downloads contain the app skeleton with all vendor packages installed. Also it includes the composer.phar
so you have everything you need for further use.
93 http://getcomposer.org
94 https://getcomposer.org/download/
95 https://github.com/composer/windows-setup/releases/
96 https://github.com/composer/windows-setup
97 https://github.com/cakephp/cakephp/tags
"require": {
"cakephp/cakephp": "3.8.*"
}
Each time you run php composer.phar update you will receive patch releases for this minor version. You can
instead change this to ^3.8 to also receive the latest stable minor releases of the 3.x branch.
If you want to stay up to date with the latest unreleased changes in CakePHP, designate dev-master as the package
version in your application’s composer.json:
"require": {
"cakephp/cakephp": "dev-master"
}
Be aware that this is not recommended, as your application can break when the next major version is released. Addi-
tionally, composer does not cache development branches, so it slows down consecutive composer installs/updates.
Another quick way to install CakePHP is via Oven98 . It is a small PHP script which checks the necessary system
requirements, and creates a new CakePHP application.
Note: IMPORTANT: This is not a deployment script. It is aimed to help developers install CakePHP for the first time
and set up a development environment quickly. Production environments should consider several other factors, like
file permissions, virtualhost configuration, etc.
Permissions
CakePHP uses the tmp directory for a number of different operations. Model descriptions, cached views, and session
information are a few examples. The logs directory is used to write log files by the default FileLog engine.
As such, make sure the directories logs, tmp and all its subdirectories in your CakePHP installation are writable by
the web server user. Composer’s installation process makes tmp and its subfolders globally writeable to get things up
and running quickly but you can update the permissions for better security and keep them writable only for the web
server user.
One common issue is that logs and tmp directories and subdirectories must be writable both by the web server and the
command line user. On a UNIX system, if your web server user is different from your command line user, you can
run the following commands from your application directory just once in your project to ensure that permissions will
be setup properly:
98 https://github.com/CakeDC/oven
Permissions 145
CakePHP Cookbook Documentation, Release 3.8
In order to use the CakePHP console tools, you need to ensure that bin/cake file is executable. On *nix or macOS,
you can execute:
chmod +x bin/cake
On Windows, the .bat file should be executable already. If you are using a Vagrant, or any other virtualized environ-
ment, any shared directories need to be shared with execute permissions (Please refer to your virtualized environment’s
documentation on how to do this).
If, for whatever reason, you cannot change the permissions of the bin/cake file, you can run the CakePHP console
with:
php bin/cake.php
Development Server
A development installation is the fastest way to setup CakePHP. In this example, we use CakePHP’s console to run
PHP’s built-in web server which will make your application available at http://host:port. From the app directory,
execute:
bin/cake server
By default, without any arguments provided, this will serve your application at http://localhost:8765/.
If there is conflict with localhost or port 8765, you can tell the CakePHP console to run the web server on a specific
host and/or port utilizing the following arguments:
bin/cake server -H 192.168.13.37 -p 5673
Note: Try bin/cake server -H 0.0.0.0 if the server is unreachable from other hosts.
Warning: The development server should never be used in a production environment. It is only intended as a
basic development server.
If you’d prefer to use a real web server, you should be able to move your CakePHP install (including the hidden files)
inside your web server’s document root. You should then be able to point your web-browser at the directory you
moved the files into and see your application in action.
Production
A production installation is a more flexible way to setup CakePHP. Using this method allows an entire domain to act as
a single CakePHP application. This example will help you install CakePHP anywhere on your filesystem and make it
available at http://www.example.com. Note that this installation may require the rights to change the DocumentRoot
on Apache webservers.
After installing your application using one of the methods above into the directory of your choosing - we’ll assume
you chose /cake_install - your production setup will look like this on the file system:
/cake_install/
bin/
config/
logs/
plugins/
src/
tests/
tmp/
vendor/
webroot/ (this directory is set as DocumentRoot)
.gitignore
.htaccess
.travis.yml
composer.json
index.php
phpunit.xml.dist
README.md
Developers using Apache should set the DocumentRoot directive for the domain to:
DocumentRoot /cake_install/webroot
If your web server is configured correctly, you should now find your CakePHP application accessible at http://www.
example.com.
Fire It Up
Alright, let’s see CakePHP in action. Depending on which setup you used, you should point your browser to http:
//example.com/ or http://localhost:8765/. At this point, you’ll be presented with CakePHP’s default home, and a
message that tells you the status of your current database connection.
Congratulations! You are ready to create your first CakePHP application.
URL Rewriting
Apache
While CakePHP is built to work with mod_rewrite out of the box–and usually does–we’ve noticed that a few users
struggle with getting everything to play nicely on their systems.
Here are a few things you might try to get it running correctly. First look at your httpd.conf. (Make sure you are
editing the system httpd.conf rather than a user- or site-specific httpd.conf.)
These files can vary between different distributions and Apache versions. You may also take a look at http://wiki.
apache.org/httpd/DistrosDefaultLayout for further information.
1. Make sure that an .htaccess override is allowed and that AllowOverride is set to All for the correct Document-
Root. You should see something similar to:
# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories).
#
# First, we configure the "default" to be a very restrictive set of
Fire It Up 147
CakePHP Cookbook Documentation, Release 3.8
# features.
<Directory />
Options FollowSymLinks
AllowOverride All
# Order deny,allow
# Deny from all
</Directory>
2. Make sure you are loading mod_rewrite correctly. You should see something like:
In many systems these will be commented out by default, so you may just need to remove the leading # symbols.
After you make changes, restart Apache to make sure the settings are active.
Verify that your .htaccess files are actually in the right directories. Some operating systems treat files that start
with ‘.’ as hidden and therefore won’t copy them.
3. Make sure your copy of CakePHP comes from the downloads section of the site or our Git repository, and has
been unpacked correctly, by checking for .htaccess files.
CakePHP app directory (will be copied to the top directory of your application by bake):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
CakePHP webroot directory (will be copied to your application’s web root by bake):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If your CakePHP site still has problems with mod_rewrite, you might want to try modifying settings for Virtual
Hosts. On Ubuntu, edit the file /etc/apache2/sites-available/default (location is distribution-dependent). In this
file, ensure that AllowOverride None is changed to AllowOverride All, so you have:
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www>
Options FollowSymLinks
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
On macOS, another solution is to use the tool virtualhostx99 to make a Virtual Host to point to your folder.
For many hosting services (GoDaddy, 1and1), your web server is being served from a user directory that al-
ready uses mod_rewrite. If you are installing CakePHP into a user directory (http://example.com/~username/
99 http://clickontyler.com/virtualhostx/
cakephp/), or any other URL structure that already utilizes mod_rewrite, you’ll need to add RewriteBase state-
ments to the .htaccess files CakePHP uses (.htaccess, webroot/.htaccess).
This can be added to the same section with the RewriteEngine directive, so for example, your webroot .htaccess
file would look like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/to/app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
The details of those changes will depend on your setup, and can include additional things that are not related to
CakePHP. Please refer to Apache’s online documentation for more information.
4. (Optional) To improve production setup, you should prevent invalid assets from being parsed by CakePHP.
Modify your webroot .htaccess to something like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/to/app/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(webroot/)?(img|css|js)/(.*)$
RewriteRule ^ index.php [L]
</IfModule>
The above will prevent incorrect assets from being sent to index.php and instead display your web server’s 404
page.
Additionally you can create a matching HTML 404 page, or use the default built-in CakePHP 404 by adding an
ErrorDocument directive:
nginx
nginx does not make use of .htaccess files like Apache, so it is necessary to create those rewritten
URLs in the site-available configuration. This is usually found in /etc/nginx/sites-available/
your_virtual_host_conf_file. Depending on your setup, you will have to modify this, but at the very least,
you will need PHP running as a FastCGI instance. The following configuration redirects the request to webroot/
index.php:
location / {
try_files $uri $uri/ /index.php?$args;
}
server {
listen 80;
listen [::]:80;
server_name www.example.com;
return 301 http://example.com$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/example.com/public/webroot;
index index.php;
access_log /var/www/example.com/log/access.log;
error_log /var/www/example.com/log/error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Note: Recent configurations of PHP-FPM are set to listen to the unix php-fpm socket instead of TCP port 9000 on
address 127.0.0.1. If you get 502 bad gateway errors from the above configuration, try update fastcgi_pass to
use the unix socket path (eg: fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;) instead of the TCP port.
IIS7 does not natively support .htaccess files. While there are add-ons that can add this support, you can also import
htaccess rules into IIS to use CakePHP’s native rewrites. To do this, follow these steps:
1. Use Microsoft’s Web Platform Installer100 to install the URL Rewrite Module 2.0101 or download it directly
(32-bit102 / 64-bit103 ).
2. Create a new file called web.config in your CakePHP root folder.
3. Using Notepad or any XML-safe editor, copy the following code into your new web.config file:
100 http://www.microsoft.com/web/downloads/platform.aspx
101 http://www.iis.net/downloads/microsoft/url-rewrite
102 http://www.microsoft.com/en-us/download/details.aspx?id=5747
103 http://www.microsoft.com/en-us/download/details.aspx?id=7435
stopProcessing="true">
<match url="^(font|img|css|files|js|favicon.ico)(.*)$" />
<action type="Rewrite" url="webroot/{R:1}{R:2}"
appendQueryString="false" />
</rule>
<rule name="Rewrite requested file/folder to index.php"
stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Rewrite" url="index.php"
appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Once the web.config file is created with the correct IIS-friendly rewrite rules, CakePHP’s links, CSS, JavaScript, and
rerouting should work correctly.
If you don’t want or can’t get mod_rewrite (or some other compatible module) running on your server, you will need
to use CakePHP’s built in pretty URLs. In config/app.php, uncomment the line that looks like:
'App' => [
// ...
// 'baseUrl' => env('SCRIPT_NAME'),
]
/.htaccess
webroot/.htaccess
This will make your URLs look like www.example.com/index.php/controllername/actionname/param rather than
www.example.com/controllername/actionname/param.
Configuration
While conventions remove the need to configure all of CakePHP, you’ll still need to configure a few things like your
database credentials.
Additionally, there are optional configuration options that allow you to swap out default values & implementations
with ones tailored to your application.
Configuration is generally stored in either PHP or INI files, and loaded during the application bootstrap. CakePHP
comes with one configuration file by default, but if required you can add additional configuration files and load them
in your application’s bootstrap code. Cake\Core\Configure is used for global configuration, and classes like
Cache provide config() methods to make configuration simple and transparent.
If your application has many configuration options it can be helpful to split configuration into multiple files. After
creating each of the files in your config/ directory you can load them in bootstrap.php:
use Cake\Core\Configure;
use Cake\Core\Configure\Engine\PhpConfig;
You can also use additional configuration files to provide environment specific overrides. Each file loaded after
app.php can redefine previously declared values allowing you to customize configuration for development or staging
environments.
153
CakePHP Cookbook Documentation, Release 3.8
General Configuration
Below is a description of the variables and how they affect your CakePHP application.
debug Changes CakePHP debugging output. false = Production mode. No error messages, errors, or warnings
shown. true = Errors and warnings shown.
App.namespace The namespace to find app classes under.
Note: When changing the namespace in your configuration, you will also need to update your composer.json
file to use this namespace as well. Additionally, create a new autoloader by running php composer.phar
dumpautoload.
App.baseUrl Un-comment this definition if you don’t plan to use Apache’s mod_rewrite with CakePHP. Don’t forget
to remove your .htaccess files too.
App.base The base directory the app resides in. If false this will be auto detected. If not false, ensure your string
starts with a / and does NOT end with a /. E.g., /basedir is a valid App.base. Otherwise, the AuthComponent
will not work properly.
App.encoding Define what encoding your application uses. This encoding is used to generate the charset in the
layout, and encode entities. It should match the encoding values specified for your database.
App.webroot The webroot directory.
App.wwwRoot The file path to webroot.
App.fullBaseUrl The fully qualified domain name (including protocol) to your application’s root. This is used when
generating absolute URLs. By default this value is generated using the $_SERVER environment. However, you
should define it manually to optimize performance or if you are concerned about people manipulating the Host
header. In a CLI context (from shells) the fullBaseUrl cannot be read from $_SERVER, as there is no webserver
involved. You do need to specify it yourself if you do need to generate URLs from a shell (e.g. when sending
emails).
App.imageBaseUrl Web path to the public images directory under webroot. If you are using a CDN you should set
this value to the CDN’s location.
App.cssBaseUrl Web path to the public css directory under webroot. If you are using a CDN you should set this
value to the CDN’s location.
App.jsBaseUrl Web path to the public js directory under webroot. If you are using a CDN you should set this value
to the CDN’s location.
App.paths Configure paths for non class based resources. Supports the plugins, templates, locales subkeys,
which allow the definition of paths for plugins, view templates and locale files respectively.
Security.salt A random string used in hashing. This value is also used as the HMAC salt when doing symetric
encryption.
Asset.timestamp Appends a timestamp which is last modified time of the particular file at the end of asset files URLs
(CSS, JavaScript, Image) when using proper helpers. Valid values:
• (bool) false - Doesn’t do anything (default)
• (bool) true - Appends the timestamp when debug is true
• (string) ‘force’ - Always appends the timestamp.
Changed in version 3.6.0: As of 3.6.0, you can override this global setting when linking assets using the
timestamp option.
Asset.cacheTime Sets the asset cache time. This determines the http header Cache-Control’s max-age, and the
http header’s Expire’s time for assets. This can take anything that you version of php’s strtotime function104
can take. The default is +1 day.
Using a CDN
To use a CDN for loading your static assets, change App.imageBaseUrl, App.cssBaseUrl, App.
jsBaseUrl to point the CDN URI, for example: https://mycdn.example.com/ (note the trailing /).
All images, scripts and styles loaded via HtmlHelper will prepend the absolute CDN path, matching the same relative
path used in the application. Please note there is a specific use case when using plugin based assets: plugins will not
use the plugin’s prefix when absolute ...BaseUrl URI is used, for example By default:
• $this->Helper->assetUrl('TestPlugin.logo.png') resolves to test_plugin/logo.png
If you set App.imageBaseUrl to https://mycdn.example.com/:
• $this->Helper->assetUrl('TestPlugin.logo.png') resolves to https://mycdn.
example.com/logo.png.
Database Configuration
See the Database Configuration for information on configuring your database connections.
Caching Configuration
See the Error and Exception Configuration for information on configuring error and exception handlers.
Logging Configuration
Email Configuration
See the Email Configuration for information on configuring email presets in CakePHP.
Session Configuration
See the Session Configuration for information on configuring session handling in CakePHP.
Routing configuration
See the Routes Configuration for more information on configuring routing and creating routes for your application.
104 http://php.net/manual/en/function.strtotime.php
Additional class paths are setup through the autoloaders your application uses. When using composer to generate
your autoloader, you could do the following, to provide fallback paths for controllers in your application:
"autoload": {
"psr-4": {
"App\\Controller\\": "/path/to/directory/with/controller/folders/",
"App\\": "src/"
}
}
The above would setup paths for both the App and App\Controller namespace. The first key will be searched,
and if that path does not contain the class/file the second key will be searched. You can also map a single namespace
to multiple directories with the following:
"autoload": {
"psr-4": {
"App\\": ["src/", "/path/to/directory/"]
}
}
Since plugins, view templates and locales are not classes, they cannot have an autoloader configured. CakePHP
provides three Configure variables to setup additional paths for these resources. In your config/app.php you can set
these variables:
return [
// More configuration
'App' => [
'paths' => [
'plugins' => [
ROOT . DS . 'plugins' . DS,
'/path/to/other/plugins/'
],
'templates' => [
APP . 'Template' . DS,
APP . 'Template2' . DS
],
'locales' => [
APP . 'Locale' . DS
]
]
]
];
Paths should end with a directory separator, or they will not work properly.
Inflection Configuration
Environment Variables
Many modern cloud providers, like Heroku, let you define environment variables for configuration data. You can
configure your CakePHP through environment variables in the 12factor app style105 . Environment variables allow
your application to require less state making your application easier to manage when it is deployed across a number
of environments.
As you can see in your app.php, the env() function is used to read configuration from the environment, and build the
application configuration. CakePHP uses DSN strings for databases, logs, email transports and cache configurations
allowing you to easily vary these libraries in each environment.
For local development, CakePHP leverages dotenv106 to allow easy local development using environment variables.
You will see a config/.env.default in your application. By copying this file into config/.env and cus-
tomizing the values you can configure your application.
You should avoid committing the config/.env file to your repository and instead use the config/.env.
default as a template with placeholder values so everyone on your team knows what environment variables are
in use and what should go in each one.
Once your environment variables have been set, you can use env() to read data from the environment:
The second value passed to the env function is the default value. This value will be used if no environment variable
exists for the given key.
Changed in version 3.5.0: dotenv library support was added to the application skeleton.
Configure Class
class Cake\Core\Configure
CakePHP’s Configure class can be used to store and retrieve application or runtime specific values. Be careful, this
class allows you to store anything in it, then use it in any other part of your code: a sure temptation to break the
MVC pattern CakePHP was designed for. The main goal of Configure class is to keep centralized variables that can
be shared between many objects. Remember to try to live by “convention over configuration” and you won’t end up
breaking the MVC structure CakePHP provides.
Configure::write('Company.name','Pizza, Inc.');
Configure::write('Company.slogan','Pizza for your body and soul');
Note: The dot notation used in the $key parameter can be used to organize your configuration settings into logical
groups.
Configure::write('Company', [
'name' => 'Pizza, Inc.',
'slogan' => 'Pizza for your body and soul'
]);
You can use Configure::write('debug', $bool) to switch between debug and production modes on the
fly. This is especially handy for JSON interactions where debugging information can cause parsing problems.
Configure::read('Company');
// Returns:
['name' => 'Pizza, Inc.', 'slogan' => 'Pizza for your body and soul'];
Configure::readOrFail('Company');
// Yields:
['name' => 'Pizza, Inc.', 'slogan' => 'Pizza for your body and soul'];
static Cake\Core\Configure::check($key)
Used to check if a key/path exists and has non-null value:
$exists = Configure::check('Company.name');
static Cake\Core\Configure::delete($key)
Used to delete information from the application’s configuration:
Configure::delete('Company.name');
static Cake\Core\Configure::consume($key)
Read and delete a key from Configure. This is useful when you want to combine reading and deleting values in a
single operation.
static Cake\Core\Configure::consumeOrFail($key)
Consumes configuration data just like Cake\Core\Configure::consume but expects to find a key/value pair.
In case the requested pair does not exist, a RuntimeException will be thrown:
Configure::consumeOrFail('Company');
// Yields:
['name' => 'Pizza, Inc.', 'slogan' => 'Pizza for your body and soul'];
use Cake\Core\Configure\Engine\PhpConfig;
You can have multiple engines attached to Configure, each reading different kinds or sources of configuration files.
You can interact with attached engines using a few other methods on Configure. To check which engine aliases are
attached you can use Configure::configured():
107 http://php.net/parse_ini_file
static Cake\Core\Configure::drop($name)
You can also remove attached engines. Configure::drop('default') would remove the default engine alias.
Any future attempts to load configuration files with that engine would fail:
Configure::drop('default');
Loaded configuration files merge their data with the existing runtime configuration in Configure. This allows you to
overwrite and add new values into the existing runtime configuration. By setting $merge to true, values will not
ever overwrite the existing configuration.
Configure::dump('my_config', 'default');
Configure::dump() can be used to either modify or overwrite configuration files that are readable with
Configure::load()
// Store the current configuration in the 'user_1234' key in the 'default' cache.
Configure::store('user_1234', 'default');
Stored configuration data is persisted in the named cache configuration. See the Caching documentation for more
information on caching.
When restoring configuration information it’s important to restore it with the same key, and cache configuration as was
used to store it. Restored information is merged on top of the existing runtime configuration.
Configuration Engines
CakePHP provides the ability to load configuration files from a number of different sources, and features a pluggable
system for creating your own configuration engines108 . The built in configuration engines are:
• JsonConfig109
• IniConfig110
• PhpConfig111
By default your application will use PhpConfig.
Bootstrapping CakePHP
If you have any additional configuration needs, you should add them to your application’s config/bootstrap.php file.
This file is included before each request, and CLI command.
This file is ideal for a number of common bootstrapping tasks:
• Defining convenience functions.
• Declaring constants.
• Defining cache configuration.
• Defining logging configuration.
• Loading custom inflections.
• Loading configuration files.
It might be tempting to place formatting functions there in order to use them in your controllers. As you’ll see in the
Controllers and Views sections there are better ways you add custom logic to your application.
108 https://api.cakephp.org/3.x/class-Cake.Core.Configure.ConfigEngineInterface.html
109 https://api.cakephp.org/3.x/class-Cake.Core.Configure.Engine.JsonConfig.html
110 https://api.cakephp.org/3.x/class-Cake.Core.Configure.Engine.IniConfig.html
111 https://api.cakephp.org/3.x/class-Cake.Core.Configure.Engine.PhpConfig.html
Application::bootstrap()
In addition to the config/bootstrap.php file which should be used to configure low-level concerns of your application,
you can also use the Application::bootstrap() hook method to load/initialize plugins, and attach global
event listeners:
// in src/Application.php
namespace App;
use Cake\Core\Plugin;
use Cake\Http\BaseApplication;
While utilizing generic table classes - also called auto-tables - when quickly creating new applications and baking
models is useful, generic table class can make debugging more difficult in some scenarios.
You can check if any query was emitted from a generic table class via DebugKit via the SQL panel in DebugKit. If
you’re still having trouble diagnosing an issue that could be caused by auto-tables, you can throw an exception when
CakePHP implicitly uses a generic Cake\ORM\Table instead of your concrete class like so:
// In your bootstrap.php
use Cake\Event\EventManager;
// Prior to 3.6 use Cake\Network\Exception\InteralErrorException
use Cake\Http\Exception\InternalErrorException;
$isCakeBakeShellRunning = (PHP_SAPI === 'cli' && isset($argv[1]) && $argv[1] === 'bake
˓→');
if (!$isCakeBakeShellRunning) {
EventManager::instance()->on('Model.initialize', function($event) {
$subject = $event->getSubject();
if (get_class($subject) === 'Cake\ORM\Table') {
$msg = sprintf(
'Missing table class or incorrect alias when registering table class
˓→for database table %s.',
$subject->getTable());
throw new InternalErrorException($msg);
}
});
}
Routing
class Cake\Routing\Router
Routing provides you tools that map URLs to controller actions. By defining routes, you can separate how your
application is implemented from how its URL’s are structured.
Routing in CakePHP also encompasses the idea of reverse routing, where an array of parameters can be transformed
into a URL string. By using reverse routing, you can re-factor your application’s URL structure without having to
update all your code.
Quick Tour
This section will teach you by example the most common uses of the CakePHP Router. Typically you want to display
something as a landing page, so you add this to your routes.php file:
use Cake\Routing\Router;
Router provides two interfaces for connecting routes. The static method is a backwards compatible interface, while
the scoped builders offer more terse syntax when building multiple routes, and better performance.
This will execute the index method in the ArticlesController when the homepage of your site is visited.
Sometimes you need dynamic routes that will accept multiple parameters, this would be the case, for example of a
route for viewing an article’s content:
163
CakePHP Cookbook Documentation, Release 3.8
The above route will accept any URL looking like /articles/15 and invoke the method view(15) in the
ArticlesController. This will not, though, prevent people from trying to access URLs looking like /
articles/foobar. If you wish, you can restrict some parameters to conform to a regular expression:
$routes->connect(
'/articles/:id',
['controller' => 'Articles', 'action' => 'view']
)
->setPatterns(['id' => '\d+'])
->setPass(['id']);
The previous example changed the star matcher by a new placeholder :id. Using placeholders allows us to validate
parts of the URL, in this case we used the \d+ regular expression so that only digits are matched. Finally, we told
the Router to treat the id placeholder as a function argument to the view() function by specifying the pass option.
More on using this option later.
The CakePHP Router can also reverse match routes. That means that from an array containing matching parameters,
it is capable of generating a URL string:
use Cake\Routing\Router;
echo Router::url(['controller' => 'Articles', 'action' => 'view', 'id' => 15]);
// Will output
/articles/15
Routes can also be labelled with a unique name, this allows you to quickly reference them when building links instead
of specifying each of the routing parameters:
// In routes.php
$routes->connect(
'/login',
['controller' => 'Users', 'action' => 'login'],
['_name' => 'login']
);
use Cake\Routing\Router;
To help keep your routing code DRY, the Router has the concept of ‘scopes’. A scope defines a common path segment,
and optionally route defaults. Any routes connected inside a scope will inherit the path/defaults from their wrapping
scopes:
Connecting Routes
To keep your code DRY you should use ‘routing scopes’. Routing scopes not only let you keep your code DRY, they
also help Router optimize its operation. This method defaults to the / scope. To create a scope and connect some
routes we’ll use the scope() method:
// In config/routes.php
use Cake\Routing\Route\DashedRoute;
The connect() method takes up to three parameters: the URL template you wish to match, the default values for
your route elements, and the options for the route. Options frequently include regular expression rules to help the
router match elements in the URL.
The basic format for a route definition is:
$routes->connect(
'/url/template',
['targetKey' => 'targetValue'],
['option' => 'matchingRegex']
);
The first parameter is used to tell the router what sort of URL you’re trying to control. The URL is a normal slash
delimited string, but can also contain a wildcard (*) or Route Elements. Using a wildcard tells the router that you
are willing to accept any additional arguments supplied. Routes without a * only match the exact template pattern
supplied.
Once you’ve specified a URL, you use the last two parameters of connect() to tell CakePHP what to do with a
request once it has been matched. The second parameter defines the route ‘target’. This can be defined either as an
array, or as a destination string. A few examples of route targets are:
);
// String target to a prefixed plugin controller. requires >=3.6.0
$routes->connect('/admin/cms/articles', 'Cms.Admin/Articles::index');
The first route we connect matches URLs starting with /users/view and maps those requests to the
UsersController->view(). The trailing /* tells the router to pass any additional segments as method ar-
guments. For example, /users/view/123 would map to UsersController->view(123).
The above example also illustrates string targets. String targets provide a compact way to define a route’s destination.
String targets have the following syntax:
[Plugin].[Prefix]/[Controller]::[action]
// Application controller
'Bookmarks::view'
// Plugin controller
Cms.Articles::edit
$routes->connect(
'/pages/**',
['controller' => 'Pages', 'action' => 'show']
);
$routes->connect(
'/government',
['controller' => 'Pages', 'action' => 'display', 5]
);
This example uses the second parameter of connect() to define default parameters. If you built an application that
features products for different categories of customers, you might consider creating a route. This allows you to link to
/government rather than /pages/display/5.
A common use for routing is to rename controllers and their actions. Instead of accessing our users controller at
/users/some_action/5, we’d like to be able to access it through /cooks/some_action/5. The following
route takes care of that:
$routes->connect(
'/cooks/:action/*', ['controller' => 'Users']
);
This is telling the Router that any URL beginning with /cooks/ should be sent to the UsersController. The
action called will depend on the value of the :action parameter. By using Route Elements, you can create variable
routes, that accept user input or variables. The above route also uses the greedy star. The greedy star indicates that this
route should accept any additional positional arguments given. These arguments will be made available in the Passed
Arguments array.
When generating URLs, routes are used too. Using ['controller' => 'Users', 'action' =>
'some_action', 5] as a URL will output /cooks/some_action/5 if the above route is the first match
found.
The routes we’ve connected so far will match any HTTP verb. If you are building a REST API you’ll often want to
map HTTP actions to different controller methods. The RouteBuilder provides helper methods that make defining
routes for specific HTTP verbs simpler:
The above routes map the same URL to different controller actions based on the HTTP verb used. GET requests will
go to the ‘view’ action, while PUT requests will go to the ‘update’ action. There are HTTP helper methods for:
• GET
• POST
• PUT
• PATCH
• DELETE
• OPTIONS
• HEAD
All of these methods return the route instance allowing you to leverage the fluent setters to further configure your
route.
New in version 3.5.0: The HTTP verb helper methods were added.
Route Elements
You can specify your own route elements and doing so gives you the power to define places in the URL where
parameters for controller actions should lie. When a request is made, the values for these route elements are found in
$this->request->getParam() in the controller. When you define a custom route element, you can optionally
specify a regular expression - this tells CakePHP how to know if the URL is correctly formed or not. If you choose to
not provide a regular expression, any non / character will be treated as part of the parameter:
$routes->connect(
'/:controller/:id',
['action' => 'view']
)->setPatterns(['id' => '[0-9]+']);
The above example illustrates how to create a quick way to view models from any controller by crafting a URL
that looks like /controllername/:id. The URL provided to connect() specifies two route elements:
:controller and :id. The :controller element is a CakePHP default route element, so the router knows
how to match and identify controller names in URLs. The :id element is a custom route element, and must be further
clarified by specifying a matching regular expression in the third parameter of connect().
CakePHP does not automatically produce lowercased and dashed URLs when using the :controller parameter.
If you need this, the above example could be rewritten like so:
use Cake\Routing\Route\DashedRoute;
The DashedRoute class will make sure that the :controller and :plugin parameters are correctly lowercased
and dashed.
If you need lowercased and underscored URLs while migrating from a CakePHP 2.x application, you can instead use
the InflectedRoute class.
Note: Patterns used for route elements must not contain any capturing groups. If they do, Router will not function
correctly.
Once this route has been defined, requesting /apples/5 would call the view() method of the ApplesController.
Inside the view() method, you would need to access the passed ID at $this->request->getParam('id').
If you have a single controller in your application and you do not want the controller name to appear in the URL, you
can map all URLs to actions in your controller. For example, to map all URLs to actions of the home controller, e.g
have URLs like /demo instead of /home/demo, you can do the following:
$routes->connect('/:action', ['controller' => 'Home']);
If you would like to provide a case insensitive URL, you can use regular expression inline modifiers:
// Prior to 3.5 use the options array instead of setPatterns()
$routes->connect(
'/:userShortcut',
['controller' => 'Teachers', 'action' => 'profile', 1],
)->setPatterns(['userShortcut' => '(?i:principal)']);
This is rather involved, but shows how powerful routes can be. The URL supplied has four route elements. The first is
familiar to us: it’s a default route element that tells CakePHP to expect a controller name.
Next, we specify some default values. Regardless of the controller, we want the index() action to be called.
Finally, we specify some regular expressions that will match years, months and days in numerical form. Note that
parenthesis (capturing groups) are not supported in the regular expressions. You can still specify alternates, as above,
but not grouped with parenthesis.
Once defined, this route will match /articles/2007/02/01, /articles/2004/11/16, hand-
ing the requests to the index() actions of their respective controllers, with the date parameters in
$this->request->getParam().
As of 3.6.0 you can use {var} for route elements instead of :var. This new parameter style enables route elements
to be embedded in non-capturing blocks. For example:
$routes->connect(
'/images/resize/{id}/{width}x{height}',
['controller' => 'Images', 'action' => 'view']
);
There are several route elements that have special meaning in CakePHP, and should not be used unless you want the
special meaning
• controller Used to name the controller for a route.
• action Used to name the controller action for a route.
• plugin Used to name the plugin a controller is located in.
• prefix Used for Prefix Routing
• _ext Used for File extentions routing.
• _base Set to false to remove the base path from the generated URL. If your application is not in the root
directory, this can be used to generate URLs that are ‘cake relative’.
• _scheme Set to create links on different schemes like webcal or ftp. Defaults to the current scheme.
• _host Set the host to use for the link. Defaults to the current host.
• _port Set the port if you need to create links on non-standard ports.
• _full If true the FULL_BASE_URL constant will be prepended to generated URLs.
There are a number of route options that can be set on each route. After connecting a route you can use its fluent
builder methods to further configure the route. These methods replace many of the keys in the $options parameter
of connect():
$routes->connect(
'/:lang/articles/:slug',
['controller' => 'Articles', 'action' => 'view']
)
// Allow GET and POST requests.
->setMethods(['GET', 'POST'])
When connecting routes using Route Elements you may want to have routed elements be passed arguments instead.
The pass option whitelists which route elements should also be made available as arguments passed into the controller
functions:
// src/Controller/BlogsController.php
public function view($articleId = null, $slug = null)
{
// Some code here...
}
// routes.php
Router::scope('/', function ($routes) {
$routes->connect(
'/blog/:id-:slug', // E.g. /blog/3-CakePHP_Rocks
Now thanks to the reverse routing capabilities, you can pass in the URL array like below and CakePHP will know how
to form the URL as defined in the routes:
// view.ctp
// This will return a link to /blog/3-CakePHP_Rocks
echo $this->Html->link('CakePHP Rocks', [
'controller' => 'Blog',
'action' => 'view',
'id' => 3,
'slug' => 'CakePHP_Rocks'
]);
Sometimes you’ll find typing out all the URL parameters for a route too verbose, or you’d like to take advantage of
the performance improvements that named routes have. When connecting routes you can specifiy a _name option,
this option can be used in reverse routing to identify the route you want to use:
If your route template contains any route elements like :controller you’ll need to supply those as part of the
options to Router::url().
Note: Route names must be unique across your entire application. The same _name cannot be used twice, even if
the names occur inside a different routing scope.
When building named routes, you will probably want to stick to some conventions for the route names. CakePHP
makes building up route names easier by allowing you to define name prefixes in each scope:
// Or with prefix()
Router::prefix('Admin', ['_namePrefix' => 'admin:'], function ($routes) {
// Connect routes.
});
You can also use the _namePrefix option inside nested scopes and it works as you’d expect:
Routes connected in named scopes will only have names added if the route is also named. Nameless routes will not
have the _namePrefix applied to them.
New in version 3.1: The _namePrefix option was added in 3.1
Prefix Routing
use Cake\Routing\Route\DashedRoute;
Prefixes are mapped to sub-namespaces in your application’s Controller namespace. By having pre-
fixes as separate controllers you can create smaller and simpler controllers. Behavior that is common to
the prefixed and non-prefixed controllers can be encapsulated using inheritance, Components, or traits. Us-
ing our users example, accessing the URL /admin/users/edit/5 would call the edit() method of our
src/Controller/Admin/UsersController.php passing 5 as the first parameter. The view file used would be
src/Template/Admin/Users/edit.ctp
You can map the URL /admin to your index() action of pages controller using following route:
When creating prefix routes, you can set additional route parameters using the $options argument:
The above would create a route template like /debug_kit/admin/:controller. The connected route would
have the plugin and prefix route elements set.
When defining prefixes, you can nest multiple prefixes if necessary:
The above would create a route template like /manager/admin/:controller. The connected route would have
the prefix route element set to manager/admin.
The current prefix will be available from the controller methods through
$this->request->getParam('prefix')
When using prefix routes it’s important to set the prefix option. Here’s how to build this link using the HTML helper:
// Leave a prefix
echo $this->Html->link(
'View Post',
['prefix' => false, 'controller' => 'Articles', 'action' => 'view', 5]
);
Note: You should connect prefix routes before you connect fallback routes.
Plugin Routing
When creating plugin scopes, you can customize the path element used with the path option:
When using scopes you can nest plugin scopes within prefix scopes:
The above would create a route that looks like /admin/debug_kit/:controller. It would have the prefix,
and plugin route elements set. The Plugin Routes section has more information on building plugin routes.
You can create links that point to a plugin, by adding the plugin key to your URL array:
echo $this->Html->link(
'New todo',
Conversely if the active request is a plugin request and you want to create a link that has no plugin you can do the
following:
echo $this->Html->link(
'New todo',
['plugin' => null, 'controller' => 'Users', 'action' => 'profile']
);
By setting 'plugin' => null you tell the Router that you want to create a link that is not part of a plugin.
SEO-Friendly Routing
Some developers prefer to use dashes in URLs, as it’s perceived to give better search engine rankings. The
DashedRoute class can be used in your application with the ability to route plugin, controller, and camelized action
names to a dashed URL.
For example, if we had a ToDo plugin, with a TodoItems controller, and a showItems() action, it could be
accessed at /to-do/todo-items/show-items with the following router connection:
use Cake\Routing\Route\DashedRoute;
Routes can match specific HTTP methods using the HTTP verb helper methods:
Router::scope('/', function($routes) {
// This route only matches on POST requests.
$routes->post(
'/reviews/start',
['controller' => 'Reviews', 'action' => 'start']
);
You can match multiple HTTP methods by using an array. Because the _method parameter is a routing key, it
participates in both URL parsing and URL generation. To generate URLs for method specific routes you’ll need to
include the _method key when generating the URL:
$url = Router::url([
'controller' => 'Reviews',
'action' => 'start',
'_method' => 'POST',
]);
Routes can use the _host option to only match specific hosts. You can use the *. wildcard to match any subdomain:
Router::scope('/', function($routes) {
// This route only matches on http://images.example.com
// Prior to 3.5 use the _host option
$routes->connect(
'/images/default-logo.png',
['controller' => 'Images', 'action' => 'default']
)->setHost('images.example.com');
The _host option is also used in URL generation. If your _host option specifies an exact domain, that domain will
be included in the generated URL. However, if you use a wildcard, then you will need to provide the _host parameter
when generating URLs:
Router::extensions(['json', 'xml']);
// ...
This will affect all routes that are being connected afterwards, no matter their scope.
In order to restrict extensions to specific scopes, you can define them using the
Cake\Routing\RouteBuilder::setExtensions() method:
This will enable the named extensions for all routes that are being connected in that scope after the
setExtensions() call, including those that are being connected in nested scopes. Similar to the global
Router::extensions() method, any routes connected prior to the call will not inherit the extensions.
Note: Setting the extensions should be the first thing you do in a scope, as the extensions will only be applied to
routes connected after the extensions are set.
Also be aware that re-opened scopes will not inherit extensions defined in previously opened scopes.
By using extensions, you tell the router to remove any matching file extensions, and then parse what remains. If you
want to create a URL such as /page/title-of-page.html you would create your route using:
Then to create links which map back to the routes simply use:
$this->Html->link(
'Link title',
['controller' => 'Pages', 'action' => 'view', 'title' => 'super-article', '_ext'
˓→=> 'html']
);
File extensions are used by Request Handling to do automatic view switching based on content types.
While Middleware can be applied to your entire application, applying middleware to specific routing scopes offers
more flexibility, as you can apply middleware only where it is needed allowing your middleware to not concern itself
with how/where it is being applied.
Note: Applied scoped middleware will be run by RoutingMiddleware, normally at the end of your application’s
middleware queue.
Before middleware can be applied to a scope, it needs to be registered into the route collection:
// in config/routes.php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Http\Middleware\EncryptedCookieMiddleware;
In situations where you have nested scopes, inner scopes will inherit the middleware applied in the containing scope:
In the above example, the routes defined in /v1 will have ‘ratelimit’, ‘auth.api’, and ‘v1compat’ middleware applied.
If you re-open a scope, the middleware applied to routes in each scope will be isolated:
In the above example, the two uses of the /blog scope do not share middleware. However, both of these scopes will
inherit middleware defined in their enclosing scopes.
Grouping Middleware
To help keep your route code DRY (Do not Repeat Yourself) middleware can be combined into groups. Once com-
bined groups can be applied like middleware can:
New in version 3.5.0: Scoped middleware & middleware groups were added in 3.5.0
RESTful Routing
Router makes it easy to generate RESTful routes for your controllers. RESTful routes are helpful when you are
creating API endpoints for your application. If we wanted to allow REST access to a recipe controller, we’d do
something like this:
// In config/routes.php...
The first line sets up a number of default routes for easy REST access where method specifies the desired result format
(e.g. xml, json, rss). These routes are HTTP Request Method sensitive.
The HTTP method being used is detected from a few different sources. The sources in order of preference are:
1. The _method POST variable
2. The X_HTTP_METHOD_OVERRIDE header.
3. The REQUEST_METHOD header
The _method POST variable is helpful in using a browser as a REST client (or anything else that can do POST). Just
set the value of _method to the name of the HTTP request method you wish to emulate.
Once you have connected resources in a scope, you can connect routes for sub-resources as well. Sub-resource routes
will be prepended by the original resource name and a id parameter. For example:
Will generate resource routes for both articles and comments. The comments routes will look like:
/api/articles/:article_id/comments
/api/articles/:article_id/comments/:id
$this->request->getParam('article_id');
By default resource routes map to the same prefix as the containing scope. If you have both nested and non-nested
resource controllers you can use a different controller in each context by using prefixes:
Router::scope('/api', function ($routes) {
$routes->resources('Articles', function ($routes) {
$routes->resources('Comments', ['prefix' => 'articles']);
});
});
Note: While you can nest resources as deeply as you require, it is not recommended to nest more than 2 resources
together.
New in version 3.3: The prefix option was added to resources() in 3.3.
By default CakePHP will connect 6 routes for each resource. If you’d like to only connect specific resource routes you
can use the only option:
$routes->resources('Articles', [
'only' => ['index', 'view']
]);
Would create read only resource routes. The route names are create, update, view, index, and delete.
You may need to change the controller action names that are used when connecting routes. For example, if your
edit() action is called put() you can use the actions key to rename the actions used:
$routes->resources('Articles', [
'actions' => ['update' => 'put', 'create' => 'add']
]);
The above would use put() for the edit() action, and add() instead of create().
You can map additional resource methods using the map option:
$routes->resources('Articles', [
'map' => [
'deleteAll' => [
'action' => 'deleteAll',
'method' => 'DELETE'
]
]
]);
// This would connect /articles/deleteAll
In addition to the default routes, this would also connect a route for /articles/delete_all. By default the path segment
will match the key name. You can use the ‘path’ key inside the resource definition to customize the path name:
$routes->resources('Articles', [
'map' => [
'updateAll' => [
'action' => 'updateAll',
'method' => 'DELETE',
'path' => '/update_many'
],
]
]);
// This would connect /articles/update_many
If you define ‘only’ and ‘map’, make sure that your mapped methods are also in the ‘only’ list.
You can provide connectOptions key in the $options array for resources() to provide custom setting used
by connect():
By default, multi-worded controllers’ URL fragments are the underscored form of the controller’s name. E.g.,
BlogPostsController’s URL fragment would be /blog_posts.
You can specify an alternative inflection type using the inflect option:
Note: As of CakePHP 3.1 the official app skeleton uses DashedRoute as its default route class. Using the
'inflect' => 'dasherize' option when connecting resource routes is recommended for URL consistency.
By default resource routes use an inflected form of the resource name for the URL segment. You can set a custom
URL segment with the path option:
Passed Arguments
Passed arguments are additional arguments or path segments that are used when making a request. They are often used
to pass parameters to your controller methods.
http://localhost/calendars/view/recent/mark
In the above example, both recent and mark are passed arguments to CalendarsController::view().
Passed arguments are given to your controllers in three ways. First as arguments to the action method called, and
secondly they are available in $this->request->getParam('pass') as a numerically indexed array. When
using custom routes you can force particular parameters to go into the passed arguments as well.
If you were to visit the previously mentioned URL, and you had a controller action that looked like:
Array
(
[0] => recent
[1] => mark
)
This same data is also available at $this->request->getParam('pass') in your controllers, views, and
helpers. The values in the pass array are numerically indexed based on the order they appear in the called URL:
debug($this->request->getParam('pass'));
Array
(
[0] => recent
[1] => mark
)
When generating URLs, using a routing array you add passed arguments as values without string keys in the array:
Generating URLs
And then later decide that /articles should really be called ‘posts’ instead, you would have to go through your
entire application renaming URLs. However, if you defined your link like:
$this->Html->link(
'View',
['controller' => 'Articles', 'action' => 'view', $id]
);
Then when you decided to change your URLs, you could do so by defining a route. This would change both the
incoming URL mapping, as well as the generated URLs.
When using array URLs, you can define both query string parameters and document fragments using special keys:
Router::url([
'controller' => 'Articles',
'action' => 'index',
'?' => ['page' => 1],
'#' => 'top'
]);
Router will also convert any unknown parameters in a routing array to querystring parameters. The ? is offered for
backwards compatibility with older versions of CakePHP.
You can also use any of the special route elements when generating URLs:
• _ext Used for Routing File Extensions routing.
• _base Set to false to remove the base path from the generated URL. If your application is not in the root
directory, this can be used to generate URLs that are ‘cake relative’.
• _scheme Set to create links on different schemes like webcal or ftp. Defaults to the current scheme.
• _host Set the host to use for the link. Defaults to the current host.
• _port Set the port if you need to create links on non-standard ports.
• _method Define the HTTP verb the URL is for.
• _full If true the FULL_BASE_URL constant will be prepended to generated URLs.
• _ssl Set to true to convert the generated URL to https or false to force http.
• _name Name of route. If you have setup named routes, you can use this key to specify it.
Redirect Routing
Redirect routing allows you to issue HTTP status 30x redirects for incoming routes, and point them at different URLs.
This is useful when you want to inform client applications that a resource has moved and you don’t want to expose
two URLs for the same content.
Redirection routes are different from normal routes as they perform an actual header redirection if a match is found.
The redirection can occur to a destination within your application or an outside location:
Router::scope('/', function ($routes) {
$routes->redirect(
'/home/*',
['controller' => 'Articles', 'action' => 'view'],
['persist' => true]
// Or ['persist'=>['id']] for default routing where the
// view action expects $id as an argument.
);
})
Redirects /home/* to /articles/view and passes the parameters to /articles/view. Using an array as
the redirect destination allows you to use other routes to define where a URL string should be redirected to. You can
redirect to external locations using string URLs as the destination:
Router::scope('/', function ($routes) {
$routes->redirect('/articles/*', 'http://google.com', ['status' => 302]);
});
Entity Routing
Entity routing allows you to use an entity, an array or object implement ArrayAccess as the source of routing
parameters. This allows you to refactor routes more easily, and generate URLs with less code. For example, if you
start off with a route that looks like:
$routes->get(
'/view/:id',
['controller' => 'Articles', 'action' => 'view'],
'articles:view'
);
Later on, you may want to expose the article slug in the URL for SEO purposes. In order to do this you would need to
update everywhere you generate a URL to the articles:view route, which could take some time. If we use entity
routes we pass the entire article entity into URL generation allowing us to skip any rework when URLs require more
parameters:
use Cake\Routing\Route\EntityRoute;
This will extract both the id property and the slug property out of the provided entity.
New in version 3.6.0: Entity routing was added in 3.6.0
Custom route classes allow you to extend and change how individual routes parse requests and handle reverse routing.
Route classes have a few conventions:
• Route classes are expected to be found in the Routing\\Route namespace of your application or plugin.
• Route classes should extend Cake\Routing\Route.
• Route classes should implement one or both of match() and/or parse().
The parse() method is used to parse an incoming URL. It should generate an array of request parameters that can
be resolved into a controller & action. Return false from this method to indicate a match failure.
The match() method is used to match an array of URL parameters and create a string URL. If the URL parameters
do not match the route false should be returned.
You can use a custom route class when making a route by using the routeClass option:
$routes->connect(
'/:slug',
['controller' => 'Articles', 'action' => 'view'],
['routeClass' => 'SlugRoute']
);
This route would create an instance of SlugRoute and allow you to implement custom parameter handling. You can
use plugin route classes using standard plugin syntax.
If you want to use an alternate route class for all your routes besides the default Route, you can do so by calling
Router::defaultRouteClass() before setting up any routes and avoid having to specify the routeClass
option for each route. For example using:
use Cake\Routing\Route\InflectedRoute;
Router::defaultRouteClass(InflectedRoute::class);
will cause all routes connected after this to use the InflectedRoute route class. Calling the method without an
argument will return current default route class.
Fallbacks Method
Cake\Routing\Router::fallbacks($routeClass = null)
The fallbacks method is a simple shortcut for defining default routes. The method uses the passed routing class for the
defined rules or if no class is provided the class returned by Router::defaultRouteClass() is used.
Calling fallbacks like so:
use Cake\Routing\Route\DashedRoute;
$routes->fallbacks(DashedRoute::class);
use Cake\Routing\Route\DashedRoute;
Note: Using the default route class (Route) with fallbacks, or any route with :plugin and/or :controller
route elements will result in inconsistent URL case.
You can hook into the URL generation process using URL filter functions. Filter functions are called before the URLs
are matched against the routes, this allows you to prepare URLs before routing.
Callback filter functions should expect the following parameters:
• $params The URL parameters being processed.
• $request The current request.
The URL filter function should always return the parameters even if unmodified.
URL filters allow you to implement features like persistent parameters:
return $params;
});
return $params;
}
if ($params['controller'] === 'Languages' && $params['action'] === 'view') {
$params['controller'] = 'Locations';
$params['action'] = 'index';
$params['language'] = $params[0];
unset($params[0]);
}
return $params;
});
into this:
Warning: If you are using the caching features of routing-middleware you must define the URL filters in your
application bootstrap() as filters are not part of the cached data.
Although named parameters were removed in CakePHP 3.0, applications may have published URLs containing them.
You can continue to accept URLs containing named parameters.
In your controller’s beforeFilter() method you can call parseNamedParams() to extract any named param-
eters from the passed arguments:
This will populate $this->request->getParam('named') with any named parameters found in the passed
arguments. Any passed argument that was interpreted as a named parameter, will be removed from the list of passed
arguments.
Dispatcher Filters
Deprecated since version 3.3.0: As of 3.3.0 Dispatcher Filters are deprecated. You should use /controllers/middleware
instead now.
There are several reasons to want a piece of code to be run before any controller code is executed or right before the
response is sent to the client, such as response caching, header tuning, special authentication or just to provide access
to a mission-critical API response in lesser time than a complete request dispatching cycle would take.
CakePHP provides a clean interface for attaching filters to the dispatch cycle. It is similar to a middleware layer, but
re-uses the existing event subsystem used in other parts of CakePHP. Since they do not work exactly like traditional
middleware, we refer to them as Dispatcher Filters.
Built-in Filters
CakePHP comes with several dispatcher filters built-in. They handle common features that all applications are likely
to need. The built-in filters are:
• AssetFilter checks whether the request is referring to a theme or plugin asset file, such as a CSS, JavaScript
or image file stored in either a plugin’s webroot folder or the corresponding one for a Theme. It will serve the
file accordingly if found, stopping the rest of the dispatching cycle:
Using Filters
Filters are usually enabled in your application’s bootstrap.php file, but you could load them any time before the
request is dispatched. Adding and removing filters is done through Cake\Routing\DispatcherFactory. By
default, the CakePHP application template comes with a couple filter classes already enabled for all requests; let’s take
a look at how they are added:
DispatcherFactory::add('Routing');
DispatcherFactory::add('ControllerFactory');
Dispatcher filters with higher priority (lower numbers) - will be executed first. Priority defaults to 10.
While using the string name is convenient, you can also pass instances into add():
use Cake\Routing\Filter\RoutingFilter;
DispatcherFactory::add(new RoutingFilter());
When adding filters, you can control the order they are invoked in using event handler priorities. While filters can
define a default priority using the $_priority property, you can set a specific priority when attaching the filter:
The higher the priority the later this filter will be invoked.
If you don’t want to run a filter on every request, you can use conditions to only apply it some of the time. You can
apply conditions using the for and when options. The for option lets you match on URL substrings, while the
when option allows you to run a callable:
The callable provided to when should return true when the filter should run. The callable can expect to get the
current request and response as arguments.
Building a Filter
To create a filter, define a class in src/Routing/Filter. In this example, we’ll be making a filter that adds a tracking
cookie for the first landing page. First, create the file. Its contents should look like:
namespace App\Routing\Filter;
use Cake\Event\Event;
use Cake\Routing\DispatcherFilter;
Save this file into src/Routing/Filter/TrackingCookieFilter.php. As you can see, like other classes in CakePHP,
dispatcher filters have a few conventions:
• Class names end in Filter.
• Classes are in the Routing\Filter namespace. For example, App\Routing\Filter.
• Generally filters extend Cake\Routing\DispatcherFilter.
DispatcherFilter exposes two methods that can be overridden in subclasses, they are beforeDispatch()
and afterDispatch(). These methods are executed before or after any controller is executed respectively.
Both methods receive a Cake\Event\Event object containing the ServerRequest and Response objects
(Cake\Http\ServerRequest and Cake\Http\Response instances) inside the $data property.
While our filter was pretty simple, there are a few other interesting things we can do in filter methods. By returning
an Response object, you can short-circuit the dispatch process and prevent the controller from being called. When
returning a response, you should also remember to call $event->stopPropagation() so other filters are not
called.
Note: When a beforeDispatch method returns a response, the controller, and afterDispatch event will not be invoked.
Let’s now create another filter for altering response headers in any public page, in our case it would be anything served
from the PagesController:
namespace App\Routing\Filter;
use Cake\Event\Event;
use Cake\Routing\DispatcherFilter;
// In our bootstrap.php
DispatcherFactory::add('HttpCache', ['for' => '/pages'])
This filter will send a expiration header to 1 day in the future for all responses produced by the pages controller. You
could of course do the same in the controller, this is just an example of what could be done with filters. For instance,
instead of altering the response, you could cache it using Cake\Cache\Cache and serve the response from the
beforeDispatch() callback.
While powerful, dispatcher filters have the potential to make your application more difficult to maintain. Filters are
an extremely powerful tool when used wisely and adding response handlers for each URL in your app is not a good
use for them. Keep in mind that not everything needs to be a filter; Controllers and Components are usually a more
accurate choice for adding any request handling code to your app.
The request and response objects provide an abstraction around HTTP requests and responses. The request object in
CakePHP allows you to introspect an incoming request, while the response object allows you to effortlessly create
HTTP responses from your controllers.
Request
class Cake\Http\ServerRequest
ServerRequest is the default request object used in CakePHP. It centralizes a number of features for interrogating
and interacting with request data. On each request one Request is created and then passed by reference to the various
layers of an application that use request data. By default the request is assigned to $this->request, and is
available in Controllers, Cells, Views and Helpers. You can also access it in Components using the controller reference.
Some of the duties ServerRequest performs include:
• Processing the GET, POST, and FILES arrays into the data structures you are familiar with.
• Providing environment introspection pertaining to the request. Information like the headers sent, the client’s IP
address, and the subdomain/domain names the server your application is running on.
• Providing access to request parameters both as array indexes and object properties.
As of 3.4.0, CakePHP’s request object implements the PSR-7 ServerRequestInterface112 making it easier to use li-
braries from outside of CakePHP.
Request Parameters
193
CakePHP Cookbook Documentation, Release 3.8
$controllerName = $this->request->getParam('controller');
// Prior to 3.4.0
$controllerName = $this->request->param('controller');
$parameters = $this->request->getAttribute('params');
// Passed arguments
$passedArgs = $this->request->getParam('pass');
Will all provide you access to the passed arguments. There are several important/useful parameters that CakePHP uses
internally, these are also all found in the routing parameters:
• plugin The plugin handling the request. Will be null when there is no plugin.
• controller The controller handling the current request.
• action The action handling the current request.
• prefix The prefix for the current action. See Prefix Routing for more information.
Cake\Http\ServerRequest::getQuery($name)
Query string parameters can be read using the getQuery() method:
// URL is /posts/index?page=1&sort=title
$page = $this->request->getQuery('page');
// Prior to 3.4.0
$page = $this->request->query('page');
You can either directly access the query property, or you can use getQuery() method to read the URL query array
in an error-free manner. Any keys that do not exist will return null:
$foo = $this->request->getQuery('value_that_does_not_exist');
// $foo === null
If you want to access all the query parameters you can use getQueryParams():
$query = $this->request->getQueryParams();
$foo = $this->request->getData('Value.that.does.not.exist');
// $foo == null
Cake\Http\ServerRequest::input($callback[, $options ])
When building REST services, you often accept request data on PUT and DELETE requests. Any application/
x-www-form-urlencoded request body data will automatically be parsed and set to $this->data for PUT and
DELETE requests. If you are accepting JSON or XML data, see below for how you can access those request bodies.
When accessing the input data, you can decode it with an optional function. This is useful when interacting with
XML or JSON request body content. Additional parameters for the decoding function can be passed as arguments to
input():
$jsonData = $this->request->input('json_decode');
$env = $this->request->getServerParams();
Applications employing REST often exchange data in non-URL-encoded post bodies. You can read input data in any
format using Http\ServerRequest::input(). By providing a decoding function, you can receive the content
in a deserialized format:
Request 195
CakePHP Cookbook Documentation, Release 3.8
Some deserializing methods require additional parameters when called, such as the ‘as array’ parameter on
json_decode. If you want XML converted into a DOMDocument object, Http\ServerRequest::input()
supports passing in additional parameters as well:
Path Information
The request object also provides useful information about the paths in your application. The base and webroot
attributes are useful for generating URLs, and determining whether or not your application is in a subdirectory. The
attributes you can use are:
// Holds /subdir/articles/edit/1?page=1
$here = $request->getRequestTarget();
// Holds /subdir
$base = $request->getAttribute('base');
// Holds /subdir/
$base = $request->getAttribute('webroot');
// Prior to 3.4.0
$webroot = $request->webroot;
$base = $request->base;
$here = $request->here();
Cake\Http\ServerRequest::is($type, $args...)
The request object provides an easy way to inspect certain conditions in a given request. By using the is() method
you can check a number of common conditions, as well as inspect other application specific request criteria:
$isPost = $this->request->is('post');
You can also extend the request detectors that are available, by using
Cake\Http\ServerRequest::addDetector() to create new kinds of detectors. There are different
types of detectors that you can create:
• Environment value comparison - Compares a value fetched from env() for equality with the provided value.
• Header value comparison - If the specified header exists with the specified value, or if the callable returns true.
• Pattern value comparison - Pattern value comparison allows you to compare a value fetched from env() to a
regular expression.
• Option based comparison - Option based comparisons use a list of options to create a regular expression. Sub-
sequent calls to add an already defined options detector will merge the options.
• Callback detectors - Callback detectors allow you to provide a ‘callback’ type to handle the check. The callback
will receive the request object as its only parameter.
Cake\Http\ServerRequest::addDetector($name, $options)
Some examples would be:
Request 197
CakePHP Cookbook Documentation, Release 3.8
Session Data
To access the session for a given request use the session() method:
$userName = $this->request->session()->read('Auth.User.name');
For more information, see the Sessions documentation for how to use the session object.
Cake\Http\ServerRequest::domain($tldLength = 1)
Returns the domain name your application is running on:
// Prints 'example.org'
echo $request->domain();
Cake\Http\ServerRequest::subdomains($tldLength = 1)
Returns the subdomains your application is running on as an array:
Cake\Http\ServerRequest::host()
Returns the host your application is on:
// Prints 'my.dev.example.org'
echo $request->host();
Cake\Http\ServerRequest::getMethod()
Returns the HTTP method the request was made with:
// Output POST
echo $request->getMethod();
// Prior to 3.4.0
echo $request->method();
Cake\Http\ServerRequest::allowMethod($methods)
Set allowed HTTP methods. If not matched, will throw MethodNotAllowedException. The 405 response will
include the required Allow header with the passed methods:
Allows you to access any of the HTTP_* headers that were used for the request. For example:
// Prior to 3.4.0
$userAgent = $this->request->header('User-Agent');
While some apache installs don’t make the Authorization header accessible, CakePHP will make it available
through apache specific methods as required.
Cake\Http\ServerRequest::referer($local = false)
Returns the referring address for the request.
Cake\Http\ServerRequest::clientIp()
Returns the current visitor’s IP address.
Request 199
CakePHP Cookbook Documentation, Release 3.8
If your application is behind a load balancer or running on a cloud service, you will often get the load balancer host,
port and scheme in your requests. Often load balancers will also send HTTP-X-Forwarded-* headers with the
original values. The forwarded headers will not be used by CakePHP out of the box. To have the request object use
these headers set the trustProxy property to true:
$this->request->trustProxy = true;
Once proxies are trusted the clientIp() method will use the last IP address in the X-Forwarded-For header.
If your application is behind multiple proxies, you can use setTrustedProxies() to define the IP addresses of
proxies in your control:
$request->setTrustedProxies(['127.1.1.1', '127.8.1.3']);
After proxies are trusted clientIp() will use the first IP address in the X-Forwarded-For header providing it
is the only value that isn’t from a trusted proxy.
New in version 3.7.0: setTrustedProxies() was added.
Cake\Http\ServerRequest::accepts($type = null)
Find out which content types the client accepts, or check whether it accepts a particular type of content.
Get all types:
$accepts = $this->request->accepts();
$acceptsJson = $this->request->accepts('application/json');
Cake\Http\ServerRequest::acceptLanguage($language = null)
Get all the languages accepted by the client, or check whether a specific language is accepted.
Get the list of accepted languages:
$acceptsLanguages = $this->request->acceptLanguage();
$acceptsSpanish = $this->request->acceptLanguage('es-es');
Cookies
See the Cake\Http\Cookie\CookieCollection documentation for how to work with cookie collection.
New in version 3.5.0: ServerRequest::getCookieCollection() was added in 3.5.0
Response
class Cake\Http\Response
Cake\Http\Response is the default response class in CakePHP. It encapsulates a number of features and func-
tionality for generating HTTP responses in your application. It also assists in testing, as it can be mocked/stubbed
allowing you to inspect headers that will be sent. Like Cake\Http\ServerRequest, Cake\Http\Response
consolidates a number of methods previously found on Controller, RequestHandlerComponent and
Dispatcher. The old methods are deprecated in favour of using Cake\Http\Response.
Response provides an interface to wrap the common response-related tasks such as:
• Sending headers for redirects.
• Sending content type headers.
• Sending any header.
• Sending the response body.
Cake\Http\Response::withType($contentType = null)
You can control the Content-Type of your application’s responses with Cake\Http\Response::withType().
If your application needs to deal with content types that are not built into Response, you can map them with type()
as well:
// Prior to 3.4.0
$this->response->type('vcf');
Usually, you’ll want to map additional content types in your controller’s beforeFilter() callback, so you can
leverage the automatic view switching features of RequestHandlerComponent if you are using it.
Response 201
CakePHP Cookbook Documentation, Release 3.8
Sending Files
// Prior to 3.4.0
$file = $this->Attachments->getFile($id);
$this->response->file($file['path']);
// Return the response to prevent controller from trying to render
// a view.
return $this->response;
As shown in the above example, you must pass the file path to the method. CakePHP will send a proper content type
header if it’s a known file type listed in Cake\Http\Response::$_mimeTypes. You can add new types prior to calling
Cake\Http\Response::withFile() by using the Cake\Http\Response::withType() method.
If you want, you can also force a file to be downloaded instead of displayed in the browser by specifying the options:
$response = $this->response->withFile(
$file['path'],
['download' => true, 'name' => 'foo']
);
// Prior to 3.4.0
$this->response->file(
$file['path'],
['download' => true, 'name' => 'foo']
);
You can respond with a file that does not exist on the disk, such as a pdf or an ics generated on the fly from a string:
$response = $response->withType('ics');
$path = '/some/file.png';
$this->response->body(function () use ($path) {
return file_get_contents($path);
});
Setting Headers
Cake\Http\Response::withHeader($header, $value)
Setting headers is done with the Cake\Http\Response::withHeader() method. Like all of the PSR-7 inter-
face methods, this method returns a new instance with the new header:
// Add/replace a header
$response = $response->withHeader('X-Extra', 'My header');
Headers are not sent when set. Instead, they are held until the response is emitted by Cake\Http\Server.
You can now use the convenience method Cake\Http\Response::withLocation() to directly set or get the
redirect location header.
Cake\Http\Response::withStringBody($string)
To set a string as the response body, do the following:
Response 203
CakePHP Cookbook Documentation, Release 3.8
$response = $response->withType('application/json')
->withStringBody(json_encode(['Foo' => 'bar']));
$response = $response->withBody($stream);
Be sure that $stream is a Psr\Http\Message\StreamInterface object. See below on how to create a new
stream.
You can also stream responses from files using Zend\Diactoros\Stream streams:
You can also stream responses from a callback using the CallbackStream. This is useful when you have resources
like images, CSV files or PDFs you need to stream to the client:
// Create an image.
$img = imagecreate(100, 100);
// ...
// Prior to 3.4.0 you can use the following to create streaming responses.
$file = fopen('/some/file.png', 'r');
$this->response->body(function () use ($file) {
rewind($file);
fpassthru($file);
fclose($file);
});
Cake\Http\Response::withCharset($charset)
Sets the charset that will be used in the response:
$this->response = $this->response->withCharset('UTF-8');
// Prior to 3.4.0
$this->response->charset('UTF-8');
Cake\Http\Response::withDisabledCache()
You sometimes need to force browsers not to cache the results of a controller action.
Cake\Http\Response::withDisabledCache() is intended for just that:
// Prior to 3.4.0
$this->response->disableCache();
}
Warning: Disabling caching from SSL domains while trying to send files to Internet Explorer can result in errors.
The above would tell clients to cache the resulting response for 5 days, hopefully speeding up your visitors’ experi-
ence. The withCache() method sets the Last-Modified value to the first argument. Expires header and the
max-age directive are set based on the second parameter. Cache-Control’s public directive is set as well.
One of the best and easiest ways of speeding up your application is to use HTTP cache. Under this caching model,
you are only required to help clients decide if they should use a cached copy of the response by setting a few headers
such as modified time and response entity tag.
Rather than forcing you to code the logic for caching and for invalidating (refreshing) it once the data has changed,
HTTP uses two models, expiration and validation, which usually are much simpler to use.
Apart from using Cake\Http\Response::withCache(), you can also use many other methods to fine-tune
HTTP cache headers to take advantage of browser or reverse proxy caching.
Response 205
CakePHP Cookbook Documentation, Release 3.8
Used under the expiration model, this header contains multiple indicators that can change the way browsers or proxies
use the cached content. A Cache-Control header can look like this:
Response class helps you set this header with some utility methods that will produce a final valid Cache-Control
header. The first is the withSharable() method, which indicates whether a response is to be considered sharable
across different users or clients. This method actually controls the public or private part of this header. Setting
a response as private indicates that all or part of it is intended for a single user. To take advantage of shared caches,
the control directive must be set as public.
The second parameter of this method is used to specify a max-age for the cache, which is the number of seconds
after which the response is no longer considered fresh:
Response exposes separate methods for setting each of the directives in the Cache-Control header.
Cake\Http\Response::withExpires($time)
You can set the Expires header to a date and time after which the response is no longer considered fresh. This
header can be set using the withExpires() method:
This method also accepts a DateTime instance or any string that can be parsed by the DateTime class.
To take advantage of this header, you must either call the checkNotModified() method manually or include the
Request Handling in your controller:
Note: Most proxy users should probably consider using the Last Modified Header instead of Etags for performance
and compatibility reasons.
Cake\Http\Response::withModified($time)
Also, under the HTTP cache validation model, you can set the Last-Modified header to indicate the date and time
at which the resource was modified for the last time. Setting this header helps CakePHP tell caching clients whether
the response was modified or not based on their cache.
To take advantage of this header, you must either call the checkNotModified() method manually or include the
Request Handling in your controller:
Cake\Http\Response::withVary($header)
In some cases, you might want to serve different content using the same URL. This is often the case if you have a
multilingual page or respond with different HTML depending on the browser. Under such circumstances you can use
the Vary header:
$response = $this->response->withVary('User-Agent');
$response = $this->response->withVary('Accept-Encoding', 'User-Agent');
$response = $this->response->withVary('Accept-Language');
Response 207
CakePHP Cookbook Documentation, Release 3.8
Cake\Http\Response::checkNotModified(Request $request)
Compares the cache headers for the request object with the cache header from the response and determines whether it
can still be considered fresh. If so, deletes the response content, and sends the 304 Not Modified header:
// In a controller action.
if ($this->response->checkNotModified($this->request)) {
return $this->response;
}
Setting Cookies
// Before 3.4.0
$this->response->cookie('remember', [
'value' => 'yes',
'path' => '/',
'httpOnly' => true,
'secure' => false,
'expire' => strtotime('+1 year')
]);
See the Creating Cookies section for how to use the cookie object. You can use withExpiredCookie() to send
an expired cookie in the response. This will make the browser remove its local cookie:
// As of 3.5.0
$this->response = $this->response->withExpiredCookie('remember_me');
As of 3.2 you can use the cors() method to define HTTP Access Control113 related headers with a fluent interface:
$this->response = $this->response->cors($this->request)
->allowOrigin(['*.cakephp.org'])
->allowMethods(['GET', 'POST'])
113 https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
->allowHeaders(['X-CSRF-Token'])
->allowCredentials()
->exposeHeaders(['Link'])
->maxAge(300)
->build();
CORS related headers will only be applied to the response if the following criteria are met:
1. The request has an Origin header.
2. The request’s Origin value matches one of the allowed Origin values.
New in version 3.2: The CorsBuilder was added in 3.2
As of CakePHP 3.4.0, response objects offer a number of methods that treat responses as immutable objects. Im-
mutable objects help prevent difficult to track accidental side-effects, and reduce mistakes caused by method calls
caused by refactoring that change ordering. While they offer a number of benefits, immutable objects can take some
getting used to. Any method that starts with with operates on the response in an immutable fashion, and will always
return a new instance. Forgetting to retain the modified instance is the most frequent mistake people make when
working with immutable objects:
$this->response->withHeader('X-CakePHP', 'yes!');
In the above code, the response will be lacking the X-CakePHP header, as the return value of the withHeader()
method was not retained. To correct the above code you would write:
Cookie Collections
class Cake\Http\Cookie\CookieCollection
CookieCollection objects are accessible from the request and response objects. They let you interact with groups
of cookies using immutable patterns, which allow the immutability of the request and response to be preserved.
Creating Cookies
class Cake\Http\Cookie\Cookie
Cookie objects can be defined through constructor objects, or by using the fluent interface that follows immutable
patterns:
use Cake\Http\Cookie\Cookie;
Once you have created a cookie, you can add it to a new or existing CookieCollection:
use Cake\Http\Cookie\CookieCollection;
Note: Remember that collections are immutable and adding cookies into, or removing cookies from a collection,
creates a new collection object.
Reading Cookies
Once you have a CookieCollection instance, you can access the cookies it contains:
Once you have a Cookie object you can interact with it’s state and modify it. Keep in mind that cookies are im-
mutable, so you’ll need to update the collection if you modify a cookie:
// Check state
$cookie->isHttpOnly();
$cookie->isSecure();
Controllers
class Cake\Controller\Controller
Controllers are the ‘C’ in MVC. After routing has been applied and the correct controller has been found, your con-
troller’s action is called. Your controller should handle interpreting the request data, making sure the correct models
are called, and the right response or view is rendered. Controllers can be thought of as middle layer between the Model
and View. You want to keep your controllers thin, and your models fat. This will help you reuse your code and makes
your code easier to test.
Commonly, a controller is used to manage the logic around a single model. For example, if you were building a site for
an online bakery, you might have a RecipesController managing your recipes and an IngredientsController managing
your ingredients. However, it’s also possible to have controllers work with more than one model. In CakePHP, a
controller is named after the primary model it handles.
Your application’s controllers extend the AppController class, which in turn extends the core Controller class.
The AppController class can be defined in src/Controller/AppController.php and it should contain methods that
are shared between all of your application’s controllers.
Controllers provide a number of methods that handle requests. These are called actions. By default, each public
method in a controller is an action, and is accessible from a URL. An action is responsible for interpreting the request
and creating the response. Usually responses are in the form of a rendered view, but there are other ways to create
responses as well.
As stated in the introduction, the AppController class is the parent class to all of your application’s con-
trollers. AppController itself extends the Cake\Controller\Controller class included in CakePHP.
AppController is defined in src/Controller/AppController.php as follows:
namespace App\Controller;
use Cake\Controller\Controller;
213
CakePHP Cookbook Documentation, Release 3.8
Controller attributes and methods created in your AppController will be available in all controllers that extend
it. Components (which you’ll learn about later) are best used for code that is used in many (but not necessarily all)
controllers.
You can use your AppController to load components that will be used in every controller in your application.
CakePHP provides a initialize() method that is invoked at the end of a Controller’s constructor for this kind of
use:
namespace App\Controller;
use Cake\Controller\Controller;
In addition to the initialize() method, the older $components property will also allow you to declare which
components should be loaded. While normal object-oriented inheritance rules apply, the components and helpers
used by a controller are treated specially. In these cases, AppController property values are merged with child
controller class arrays. The values in the child class will always override those in AppController.
Request Flow
Controller Actions
Controller actions are responsible for converting the request parameters into a response for the browser/user making the
request. CakePHP uses conventions to automate this process and remove some boilerplate code you would otherwise
need to write.
By convention, CakePHP renders a view with an inflected version of the action name. Returning to our online bakery
example, our RecipesController might contain the view(), share(), and search() actions. The controller would
be found in src/Controller/RecipesController.php and contain:
// src/Controller/RecipesController.php
The template files for these actions would be src/Template/Recipes/view.ctp, src/Template/Recipes/share.ctp, and
src/Template/Recipes/search.ctp. The conventional view file name is the lowercased and underscored version of the
action name.
Controller actions generally use Controller::set() to create a context that View uses to render the view layer.
Because of the conventions that CakePHP uses, you don’t need to create and render the view manually. Instead, once
a controller action has completed, CakePHP will handle rendering and delivering the View.
If for some reason you’d like to skip the default behavior, you can return a Cake\Http\Response object from the
action with the fully created response.
In order for you to use a controller effectively in your own application, we’ll cover some of the core attributes and
methods provided by CakePHP’s controllers.
Controllers interact with views in a number of ways. First, they are able to pass data to the views, using
Controller::set(). You can also decide which view class to use, and which view file should be rendered
from the controller.
$this->set('color', 'pink');
You have selected <?= h($color) ?> icing for the cake.
The Controller::set() method also takes an associative array as its first parameter. This can often be a quick
way to assign a set of information to the view:
$data = [
'color' => 'pink',
'type' => 'sugar',
'base_price' => 23.95
];
$this->set($data);
Keep in mind that view vars are shared among all parts rendered by your view. They will be available in all parts of
the view: the template, the layout and all elements inside the former two.
If you want to customize the view class, layout/template paths, helpers or the theme that will be used when rendering
the view, you can use the viewBuilder() method to get a builder. This builder can be used to define properties of
the view before it is created:
$this->viewBuilder()
->helpers(['MyCustom'])
->theme('Modern')
->className('Modern.Admin');
The above shows how you can load custom helpers, set the theme and use a custom view class.
New in version 3.1: ViewBuilder was added in 3.1
Rendering a View
namespace App\Controller;
Although CakePHP will automatically call it after every action’s logic (unless you’ve set $this->autoRender
to false), you can use it to specify an alternate view file by specifying a view file name as first argument of
Controller::render() method.
If $view starts with ‘/’, it is assumed to be a view or element file relative to the src/Template folder. This allows
direct rendering of elements, very useful in AJAX calls:
The second parameter $layout of Controller::render() allows you to specify the layout with which the
view is rendered.
In your controller, you may want to render a different view than the conventional one. You can do this by calling
Controller::render() directly. Once you have called Controller::render(), CakePHP will not try to
re-render the view:
namespace App\Controller;
namespace App\Controller;
The method will return the response instance with appropriate headers set. You should return the response instance
from your action to prevent view rendering and let the dispatcher handle actual redirection.
You can also use a relative or absolute URL as the $url argument:
return $this->redirect('/orders/thanks');
return $this->redirect('http://www.example.com');
The second parameter of Controller::redirect() allows you to define an HTTP status code to accompany
the redirect. You may want to use 301 (moved permanently) or 303 (see other), depending on the nature of the redirect.
If you need to redirect to the referer page you can use:
return $this->redirect($this->referer());
return $this->redirect([
'controller' => 'Orders',
'action' => 'confirm',
'?' => [
'product' => 'pizza',
'quantity' => 5
],
'#' => 'top'
]);
http://www.example.com/orders/confirm?product=pizza&quantity=5#top
Cake\Controller\Controller::setAction($action, $args...)
If you need to forward the current action to a different action on the same controller, you can use
Controller::setAction() to update the request object, modify the view template that will be rendered and
forward execution to the named action:
// In a controller method.
$this->loadModel('Articles');
$recentArticles = $this->Articles->find('all', [
'limit' => 5,
'order' => 'Articles.created DESC'
]);
If you are using a table provider other than the built-in ORM you can link that table system into CakePHP’s controllers
by connecting its factory method:
// In a controller method.
$this->modelFactory(
'ElasticIndex',
['ElasticIndexes', 'factory']
);
After registering a table factory, you can use loadModel to load instances:
// In a controller method.
$this->loadModel('Locations', 'ElasticIndex');
Note: The built-in ORM’s TableRegistry is connected by default as the ‘Table’ provider.
Paginating a Model
Cake\Controller\Controller::paginate()
This method is used for paginating results fetched by your models. You can specify page sizes, model find conditions
and more. See the pagination section for more details on how to use paginate().
The $paginate attribute gives you an easy way to customize how paginate() behaves:
property Cake\Controller\Controller::$components
The $components property on your controllers allows you to configure components. Configured components and
their dependencies will be created by CakePHP for you. Read the Configuring Components section for more infor-
mation. As mentioned earlier the $components property will be merged with the property defined in each of your
controller’s parent classes.
property Cake\Controller\Controller::$helpers
Let’s look at how to tell a CakePHP Controller that you plan to use additional MVC classes:
Each of these variables are merged with their inherited values, therefore it is not necessary (for example) to redeclare
the FormHelper, or anything that is declared in your AppController.
Deprecated since version 3.0: Loading Helpers from the controller is provided for backwards compatibility reasons.
You should see Configuring Helpers for how to load helpers.
CakePHP controllers trigger several events/callbacks that you can use to insert logic around the request life-cycle:
Event List
• Controller.initialize
• Controller.startup
• Controller.beforeRedirect
• Controller.beforeRender
• Controller.shutdown
By default the following callback methods are connected to related events if the methods are implemented by your
controllers
Cake\Controller\Controller::beforeFilter(Event $event)
Called during the Controller.initialize event which occurs before every action in the controller. It’s
a handy place to check for an active session or inspect user permissions.
Returning a response from a beforeFilter method will not prevent other listeners of the same event from
being called. You must explicitly stop the event.
Cake\Controller\Controller::beforeRender(Event $event)
Called during the Controller.beforeRender event which occurs after controller action logic, but
before the view is rendered. This callback is not used often, but may be needed if you are calling
Controller\Controller::render() manually before the end of a given action.
Cake\Controller\Controller::afterFilter(Event $event)
Called during the Controller.shutdown event which is triggered after every controller action, and after
rendering is complete. This is the last controller method to run.
In addition to controller life-cycle callbacks, Components also provide a similar set of callbacks.
Remember to call AppController’s callbacks within child controller callbacks for best results:
//use Cake\Event\Event;
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
}
More on Controllers
CakePHP’s official skeleton app ships with a default controller PagesController.php. This is a simple and optional
controller for serving up static content. The home page you see after installation is generated using this controller and
the view file src/Template/Pages/home.ctp. If you make the view file src/Template/Pages/about_us.ctp you can
access it using the URL http://example.com/pages/about_us. You are free to modify the Pages Controller to meet
your needs.
When you “bake” an app using Composer the Pages Controller is created in your src/Controller/ folder.
Components
Components are packages of logic that are shared between controllers. CakePHP comes with a fantastic set of core
components you can use to aid in various common tasks. You can also create your own components. If you find
yourself wanting to copy and paste things between controllers, you should consider creating your own component to
contain the functionality. Creating components keeps controller code clean and allows you to reuse code between
different controllers.
For more information on the components included in CakePHP, check out the chapter for each component:
AuthComponent
Configuring authentication requires several steps including defining a users table, creating a model, controller & views,
etc.
This is all covered step by step in the CMS Tutorial.
If you are looking for existing authentication and/or authorization solutions for CakePHP, have a look at the Authen-
tication and Authorization116 section of the Awesome CakePHP list.
Authentication
Authentication is the process of identifying users by provided credentials and ensuring that users are who they say
they are. Generally, this is done through a username and password, that are checked against a known list of users. In
CakePHP, there are several built-in ways of authenticating users stored in your application.
• FormAuthenticate allows you to authenticate users based on form POST data. Usually, this is a login form
that users enter information into.
• BasicAuthenticate allows you to authenticate users using Basic HTTP authentication.
• DigestAuthenticate allows you to authenticate users using Digest HTTP authentication.
By default AuthComponent uses FormAuthenticate.
Generally, you’ll want to offer form based authentication. It is the easiest for users using a web-browser to use. If
you are building an API or webservice, you may want to consider basic authentication or digest authentication. The
key differences between digest and basic authentication are mostly related to how passwords are handled. In basic
authentication, the username and password are transmitted as plain-text to the server. This makes basic authentication
un-suitable for applications without SSL, as you would end up exposing sensitive passwords. Digest authentication
uses a digest hash of the username, password, and a few other details. This makes digest authentication more appro-
priate for applications without SSL encryption.
You can also use authentication systems like OpenID as well; however, OpenID is not part of CakePHP core.
114 https://book.cakephp.org/authorization/
115 https://book.cakephp.org/authentication/
116 https://github.com/FriendsOfCake/awesome-cakephp/blob/master/README.md#authentication-and-authorization
You configure authentication handlers using the authenticate config. You can configure one or many handlers for
authentication. Using multiple handlers allows you to support different ways of logging users in. When logging users
in, authentication handlers are checked in the order they are declared. Once one handler is able to identify the user, no
other handlers will be checked. Conversely, you can halt all authentication by throwing an exception. You will need
to catch any thrown exceptions and handle them as needed.
You can configure authentication handlers in your controller’s beforeFilter() or initialize() methods.
You can pass configuration information into each authentication object using an array:
// Simple setup
$this->Auth->config('authenticate', ['Form']);
// Pass settings in
$this->Auth->config('authenticate', [
'Basic' => ['userModel' => 'Members'],
'Form' => ['userModel' => 'Members']
]);
In the second example, you’ll notice that we had to declare the userModel key twice. To help you keep your code
DRY, you can use the all key. This special key allows you to set settings that are passed to every attached object.
The all key is also exposed as AuthComponent::ALL:
In the above example, both Form and Basic will get the settings defined for the ‘all’ key. Any settings passed to a
specific authentication object will override the matching key in the ‘all’ key. The core authentication objects support
the following configuration keys.
• fields The fields to use to identify a user by. You can use keys username and password to specify your
username and password fields respectively.
• userModel The model name of the users table; defaults to Users.
• finder The finder method to use to fetch a user record. Defaults to ‘all’.
• passwordHasher Password hasher class; Defaults to Default.
• The scope and contain options have been deprecated as of 3.1. Use a custom finder instead to modify the
query to fetch a user record.
• The userFields option has been deprecated as of 3.1. Use select() in your custom finder.
To configure different fields for user in your initialize() method:
]);
}
Do not put other Auth configuration keys, such as authError, loginAction, etc., within the authenticate
or Form element. They should be at the same level as the authenticate key. The setup above with other Auth configu-
ration should look like:
In addition to the common configuration, Basic authentication supports the following keys:
• realm The realm being authenticated. Defaults to env('SERVER_NAME').
In addition to the common configuration Digest authentication supports the following keys:
• realm The realm authentication is for. Defaults to the servername.
• nonce A nonce used for authentication. Defaults to uniqid().
• qop Defaults to auth; no other values are supported at this time.
• opaque A string that must be returned unchanged by clients. Defaults to md5($config['realm']).
Note: To find the user record, the database is queried only using the username. The password check is done in PHP.
This is necessary because hashing algorithms like bcrypt (which is used by default) generate a new hash each time,
even for the same string and you can’t just do simple string comparison in SQL to check if the password matches.
You can customize the query used to fetch the user record using the finder option in authenticate class config:
],
]);
}
This will require your UsersTable to have finder method findAuth(). In the example shown below the query
is modified to fetch only required fields and add a condition. You must ensure that you select the fields you need to
authenticate a user, such as username and password:
return $query;
}
Note: finder option is available since 3.1. Prior to that you can use scope and contain options to modify a
query.
AuthComponent::identify()
You need to manually call $this->Auth->identify() to identify the user using credentials provided in request.
Then use $this->Auth->setUser() to log the user in, i.e., save user info to session.
When authenticating users, attached authentication objects are checked in the order they are attached. Once one of
the objects can identify the user, no other objects are checked. A sample login function for working with a login form
could look like:
The above code will attempt to first identify a user by using the POST data. If successful we set the user info to the
session so that it persists across requests and then redirect to either the last page they were visiting or a URL specified
in the loginRedirect config. If the login is unsuccessful, a flash message is set.
Warning: $this->Auth->setUser($data) will log the user in with whatever data is passed to the method.
It won’t actually check the credentials against an authentication class.
AuthComponent::redirectUrl()
After logging a user in, you’ll generally want to redirect them back to where they came from. Pass a URL in to set the
destination a user should be redirected to after logging in.
If no parameter is passed, the returned URL will use the following rules:
• Returns the normalized URL from the redirect query string value if it is present and for the same domain
the current app is running on. Before 3.4.0, the Auth.redirect session value was used.
• If there is no query string/session value and there is a config with loginRedirect, the loginRedirect
value is returned.
• If there is no redirect value and no loginRedirect, / is returned.
Basic and digest are stateless authentication schemes and don’t require an initial POST or a form. If using only
basic/digest authenticators you don’t require a login action in your controller. Stateless authentication will re-verify
the user’s credentials on each request, this creates a small amount of additional overhead, but allows clients to login
without using cookies and makes AuthComponent more suitable for building APIs.
For stateless authenticators, the storage config should be set to Memory so that AuthComponent does not use
a session to store user record. You may also want to set config unauthorizedRedirect to false so that
AuthComponent throws a ForbiddenException instead of the default behavior of redirecting to referrer.
The unauthorizedRedirect option only applies to authenticated users. When a user is not yet authenticated and
you do not want the user to be redirected, you will need to load one or more stateless authenticators, like Basic or
Digest.
Authentication objects can implement a getUser() method that can be used to support user login systems that
don’t rely on cookies. A typical getUser method looks at the request/environment and uses the information there to
confirm the identity of the user. HTTP Basic authentication for example uses $_SERVER['PHP_AUTH_USER']
and $_SERVER['PHP_AUTH_PW'] for the username and password fields.
Note: In case authentication does not work like expected, check if queries are executed at
all (see BaseAuthenticate::_query($username)). In case no queries are executed check if
$_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] do get populated by the webserver. If
you are using Apache with FastCGI-PHP you might need to add this line to your .htaccess file in webroot:
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
On each request, these values, PHP_AUTH_USER and PHP_AUTH_PW, are used to re-identify the user and ensure
they are the valid user. As with authentication object’s authenticate() method, the getUser() method should
return an array of user information on the success or false on failure.
if (empty($username) || empty($pass)) {
return false;
}
The above is how you could implement the getUser method for HTTP basic authentication. The _findUser()
method is part of BaseAuthenticate and identifies a user based on a username and password.
Basic authentication allows you to create a stateless authentication that can be used in intranet applications or for
simple API scenarios. Basic authentication credentials will be rechecked on each request.
Warning: Basic authentication transmits credentials in plain-text. You should use HTTPS when using Basic
authentication.
Here we’re using username + API key as our fields and use the Users model.
Because basic HTTP sends credentials in plain-text, it is unwise to have users send their login password. Instead, an
opaque API key is generally used. You can generate these API tokens randomly using libraries from CakePHP:
namespace App\Model\Table;
use Cake\Auth\DefaultPasswordHasher;
use Cake\Utility\Text;
use Cake\Event\Event;
use Cake\ORM\Table;
use Cake\Utility\Security;
if ($entity->isNew()) {
$hasher = new DefaultPasswordHasher();
The above generates a random hash for each user as they are saved. The above code assumes you have two columns
api_key - to store the hashed API key, and api_key_plain - to the plaintext version of the API key, so we can
display it to the user later on. Using a key instead of a password means that even over plain HTTP, your users can use
an opaque token instead of their original password. It is also wise to include logic allowing API keys to be regenerated
at a user’s request.
Digest authentication offers an improved security model over basic authentication, as the user’s credentials are never
sent in the request header. Instead, a hash is sent.
To use digest authentication, you’ll need to configure AuthComponent:
$this->loadComponent('Auth', [
'authenticate' => [
'Digest' => [
'fields' => ['username' => 'username', 'password' => 'digest_hash'],
'userModel' => 'Users'
],
],
'storage' => 'Memory',
'unauthorizedRedirect' => false
]);
Here we’re using username + digest_hash as our fields and use the Users model.
Because Digest authentication requires a password hashed in the format defined by the RFC, in order to cor-
rectly hash a password for use with Digest authentication you should use the special password hashing function
on DigestAuthenticate. If you are going to be combining digest authentication with any other authentication
strategies, it’s also recommended that you store the digest password in a separate column, from the normal password
hash:
namespace App\Model\Table;
use Cake\Auth\DigestAuthenticate;
use Cake\Event\Event;
use Cake\ORM\Table;
Passwords for digest authentication need a bit more information than other password hashes, based on the RFC for
digest authentication.
Note: The third parameter of DigestAuthenticate::password() must match the ‘realm’ config value
defined when DigestAuthentication was configured in AuthComponent::$authenticate. This defaults to
env('SCRIPT_NAME'). You may wish to use a static string if you want consistent hashes in multiple environments.
Because authentication objects are pluggable, you can create custom authentication objects in your application or
plugins. If for example, you wanted to create an OpenID authentication object. In src/Auth/OpenidAuthenticate.php
you could put the following:
namespace App\Auth;
use Cake\Auth\BaseAuthenticate;
use Cake\Http\ServerRequest;
use Cake\Http\Response;
Authentication objects should return false if they cannot identify the user and an array of user information if
they can. It’s not required that you extend BaseAuthenticate, only that your authentication object implements
Cake\Event\EventListenerInterface. The BaseAuthenticate class provides a number of helpful
methods that are commonly used. You can also implement a getUser() method if your authentication object needs
to support stateless or cookie-less authentication. See the sections on basic and digest authentication below for more
information.
AuthComponent triggers two events, Auth.afterIdentify and Auth.logout, after a user has been identi-
fied and before a user is logged out respectively. You can set callback functions for these events by returning a mapping
array from implementedEvents() method of your authenticate class:
Once you’ve created your custom authentication objects, you can use them by including them in AuthComponent’s
authenticate array:
$this->Auth->config('authenticate', [
'Openid', // app authentication object.
'AuthBag.Openid', // plugin authentication object.
]);
Note: Note that when using simple notation there’s no ‘Authenticate’ word when initiating the authentication object.
Instead, if using namespaces, you’ll need to set the full namespace of the class, including the ‘Authenticate’ word.
When an unauthenticated user tries to access a protected page first the unauthenticated() method of the last
authenticator in the chain is called. The authenticate object can handle sending response or redirection by returning a
response object to indicate no further action is necessary. Due to this, the order in which you specify the authentication
provider in authenticate config matters.
If authenticator returns null, AuthComponent redirects user to the login action. If it’s an AJAX request and config
ajaxLogin is specified that element is rendered else a 403 HTTP status code is returned.
In order to display the session error messages that Auth generates, you need to add the following code to your layout.
Add the following two lines to the src/Template/Layout/default.ctp file in the body section:
You can customize the error messages and flash settings AuthComponent uses. Using flash config you can
configure the parameters AuthComponent uses for setting flash messages. The available keys are
• key - The key to use, defaults to ‘default’. Prior to 3.4.0, the key defaulted to ‘auth’.
• element - The element name to use for rendering, defaults to null.
• params - The array of additional parameters to use, defaults to [].
In addition to the flash message settings you can customize other error messages AuthComponent uses. In your
controller’s beforeFilter(), or component settings you can use authError to customize the error used for
when authorization fails:
Sometimes, you want to display the authorization error only after the user has already logged-in. You can suppress
this message by setting its value to boolean false.
In your controller’s beforeFilter() or component settings:
if (!$this->Auth->user()) {
$this->Auth->config('authError', false);
}
Hashing Passwords
You are responsible for hashing the passwords before they are persisted to the database, the easiest way is to use a
setter function in your User entity:
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
// ...
// ...
}
AuthComponent is configured by default to use the DefaultPasswordHasher when validating user credentials
so no additional configuration is required in order to authenticate users.
DefaultPasswordHasher uses the bcrypt hashing algorithm internally, which is one of the stronger password
hashing solutions used in the industry. While it is recommended that you use this password hasher class, the case may
be that you are managing a database of users whose password was hashed differently.
In order to use a different password hasher, you need to create the class in src/Auth/LegacyPasswordHasher.php
and implement the hash() and check() methods. This class needs to extend the AbstractPasswordHasher
class:
namespace App\Auth;
use Cake\Auth\AbstractPasswordHasher;
Then you are required to configure the AuthComponent to use your own password hasher:
Supporting legacy systems is a good idea, but it is even better to keep your database with the latest security advance-
ments. The following section will explain how to migrate from one hashing algorithm to CakePHP’s default.
CakePHP provides a clean way to migrate your users’ passwords from one algorithm to another, this is achieved
through the FallbackPasswordHasher class. Assuming you are migrating your app from CakePHP 2.x which
uses sha1 password hashes, you can configure the AuthComponent as follows:
The first name appearing in the hashers key indicates which of the classes is the preferred one, but it will fallback
to the others in the list if the check was unsuccessful.
When using the WeakPasswordHasher you will need to set the Security.salt configure the value to ensure
passwords are salted.
In order to update old users’ passwords on the fly, you can change the login function accordingly:
As you can see we are just setting the plain password again so the setter function in the entity will hash the password
as shown in the previous example and then save the entity.
AuthComponent::setUser(array $user)
Sometimes the need arises where you need to manually log a user in, such as just after they registered for your
application. You can do this by calling $this->Auth->setUser() with the user data you want to ‘login’:
Warning: Be sure to manually add the new User id to the array passed to the setUser() method. Otherwise,
you won’t have the user id available.
AuthComponent::user($key = null)
Once a user is logged in, you will often need some particular information about the current user. You can access the
currently logged in user using AuthComponent::user():
If the current user is not logged in or the key doesn’t exist, null will be returned.
AuthComponent::logout()
Eventually, you’ll want a quick way to de-authenticate someone and redirect them to where they need to go. This
method is also useful if you want to provide a ‘Log me out’ link inside a members’ area of your application:
Logging out users that logged in with Digest or Basic auth is difficult to accomplish for all clients. Most browsers
will retain credentials for the duration they are still open. Some clients can be forced to logout by sending a 401 status
code. Changing the authentication realm is another solution that works for some clients.
In some cases you may want to use $this->Auth->user() in the beforeFilter(Event $event) method.
This is achievable by using the checkAuthIn config key. The following changes which event for which initial
authentication checks should be done:
Authorization
Authorization is the process of ensuring that an identified/authenticated user is allowed to access the resources they
are requesting. If enabled AuthComponent can automatically check authorization handlers and ensure that logged
in users are allowed to access the resources they are requesting. There are several built-in authorization handlers and
you can create custom ones for your application or as part of a plugin.
• ControllerAuthorize Calls isAuthorized() on the active controller, and uses the return of that to
authorize a user. This is often the most simple way to authorize users.
Note: The ActionsAuthorize & CrudAuthorize adapter available in CakePHP 2.x have now been moved
to a separate plugin cakephp/acl117 .
117 https://github.com/cakephp/acl
You configure authorization handlers using the authorize config key. You can configure one or many handlers
for authorization. Using multiple handlers allows you to support different ways of checking authorization. When
authorization handlers are checked, they will be called in the order they are declared. Handlers should return false,
if they are unable to check authorization, or the check has failed. Handlers should return true if they were able to
check authorization successfully. Handlers will be called in sequence until one passes. If all checks fail, the user will
be redirected to the page they came from. Additionally, you can halt all authorization by throwing an exception. You
will need to catch any thrown exceptions and handle them.
You can configure authorization handlers in your controller’s beforeFilter() or initialize() methods. You
can pass configuration information into each authorization object, using an array:
// Basic setup
$this->Auth->config('authorize', ['Controller']);
// Pass settings in
$this->Auth->config('authorize', [
'Actions' => ['actionPath' => 'controllers/'],
'Controller'
]);
Much like authenticate, authorize, helps you keep your code DRY, by using the all key. This spe-
cial key allows you to set settings that are passed to every attached object. The all key is also exposed as
AuthComponent::ALL:
In the above example, both the Actions and Controller will get the settings defined for the ‘all’ key. Any
settings passed to a specific authorization object will override the matching key in the ‘all’ key.
If an authenticated user tries to go to a URL they are not authorized to access, they will be redirected back to
the referrer. If you do not want such redirection (mostly needed when using stateless authentication adapter)
you can set config option unauthorizedRedirect to false. This causes AuthComponent to throw a
ForbiddenException instead of redirecting.
Because authorize objects are pluggable, you can create custom authorize objects in your application or plugins. If
for example, you wanted to create an LDAP authorize object. In src/Auth/LdapAuthorize.php you could put the
following:
namespace App\Auth;
use Cake\Auth\BaseAuthorize;
use Cake\Http\ServerRequest;
Authorize objects should return false if the user is denied access, or if the object is unable to perform a check.
If the object is able to verify the user’s access, true should be returned. It’s not required that you extend
BaseAuthorize, only that your authorize object implements an authorize() method. The BaseAuthorize
class provides a number of helpful methods that are commonly used.
Once you’ve created your custom authorize object, you can use them by including them in your AuthComponent’s
authorize array:
$this->Auth->config('authorize', [
'Ldap', // app authorize object.
'AuthBag.Combo', // plugin authorize object.
]);
Using No Authorization
If you’d like to not use any of the built-in authorization objects and want to handle things entirely out-
side of AuthComponent, you can set $this->Auth->config('authorize', false);. By default
AuthComponent starts off with authorize set to false. If you don’t use an authorization scheme, make sure
to check authorization yourself in your controller’s beforeFilter() or with another component.
AuthComponent::allow($actions = null)
There are often times controller actions that you wish to remain entirely public or that don’t require users to be logged
in. AuthComponent is pessimistic and defaults to denying access. You can mark actions as public actions by using
AuthComponent::allow(). By marking actions as public, AuthComponent will not check for a logged in
user nor will authorize objects to be checked:
By calling it empty you allow all actions to be public. For a single action, you can provide the action name as a string.
Otherwise, use an array.
Note: You should not add the “login” action of your UsersController to allow list. Doing so would cause
problems with the normal functioning of AuthComponent.
AuthComponent::deny($actions = null)
By default all actions require authorization. However, after making actions public you want to revoke the public
access. You can do so using AuthComponent::deny():
By calling it empty you deny all actions. For a single action, you can provide the action name as a string. Otherwise,
use an array.
Using ControllerAuthorize
ControllerAuthorize allows you to handle authorization checks in a controller callback. This is ideal when you have
very simple authorization or you need to use a combination of models and components to do your authorization and
don’t want to create a custom authorize object.
The callback is always called isAuthorized() and it should return a boolean as to whether or not the user is
allowed to access resources in the request. The callback is passed the active user so it can be checked:
// Default deny
return false;
}
}
The above callback would provide a very simple authorization system where only users with role = admin could access
actions that were in the admin prefix.
Configuration options
The following settings can all be defined either in your controller’s initialize() method or using
$this->Auth->config() in your beforeFilter():
ajaxLogin The name of an optional view element to render when an AJAX request is made with an invalid or expired
session.
allowedActions Controller actions for which user validation is not required.
authenticate Set to an array of Authentication objects you want to use when logging users in. There are several core
authentication objects; see the section on Suggested Reading Before Continuing.
authError Error to display when user attempts to access an object or action to which they do not have access.
You can suppress authError message from being displayed by setting this value to boolean false.
authorize Set to an array of Authorization objects you want to use when authorizing users on each request; see the
section on Authorization.
flash Settings to use when Auth needs to do a flash message with FlashComponent::set(). Available keys are:
• element - The element to use; defaults to ‘default’.
• key - The key to use; defaults to ‘auth’.
• params - The array of additional parameters to use; defaults to ‘[]’.
loginAction A URL (defined as a string or array) to the controller action that handles logins. Defaults to /users/
login.
loginRedirect The URL (defined as a string or array) to the controller action users should be redirected to after
logging in. This value will be ignored if the user has an Auth.redirect value in their session.
logoutRedirect The default action to redirect to after the user is logged out. While AuthComponent does not han-
dle post-logout redirection, a redirect URL will be returned from AuthComponent::logout(). Defaults
to loginAction.
unauthorizedRedirect Controls handling of unauthorized access. By default unauthorized user is redirected to the
referrer URL or loginAction or ‘/’. If set to false, a ForbiddenException exception is thrown instead of
redirecting.
storage Storage class to use for persisting user record. When using stateless authenticator you should set this to
Memory. Defaults to Session. You can pass config options to storage class using array format. For e.g. to use
a custom session key you can set storage to ['className' => 'Session', 'key' => 'Auth.
Admin'].
checkAuthIn Name of the event in which initial auth checks should be done. Defaults to Controller.
startup. You can set it to Controller.initialize if you want the check to be done before controller’s
beforeFilter() method is run.
You can get current configuration values by calling $this->Auth->config():: only the configuration option:
$this->Auth->config('loginAction');
$this->redirect($this->Auth->config('loginAction'));
This is useful if you want to redirect a user to the login route for example. Without a parameter, the full configuration
will be returned.
See the Testing Actions That Require Authentication section for tips on how to test controller actions that are protected
by AuthComponent.
Cookie
Configuring Cookies
Cookies can be configured either globally or per top-level name. The global configuration data will be merged with
the top-level configuration. So only need to override the parts that are different. To configure the global settings use
the config() method:
$this->Cookie->config('path', '/');
$this->Cookie->config([
'expires' => '+10 days',
'httpOnly' => true
]);
$this->Cookie->write('name', 'Larry');
You can also group your variables by using dot notation in the key parameter:
$this->Cookie->write('User.name', 'Larry');
$this->Cookie->write('User.role', 'Lead');
If you want to write more than one value to the cookie at a time, you can pass an array:
$this->Cookie->write('User',
['name' => 'Larry', 'role' => 'Lead']
);
All values in the cookie are encrypted with AES by default. If you want to store the values as plain text, be sure
to configure the key space:
// Outputs "Larry"
echo $this->Cookie->read('name');
// This outputs something like ['name' => 'Larry', 'role' => 'Lead']
Warning: CookieComponent cannot interact with bare strings values that contain ,. The compo-
nent will attempt to interpret these values as arrays, leading to incorrect results. Instead you should use
$request->getCookie().
Cake\Controller\Component\CookieComponent::check($key)
Parameters
• $key (string) – The key to check.
Used to check whether a key/path exists and has a non-null value.
Cake\Controller\Component\CookieComponent::delete(mixed $key)
Deletes a cookie variable of the name in $key. Works with dot notation:
// Delete a variable
$this->Cookie->delete('bar');
// Delete the cookie variable bar, but not everything under foo
$this->Cookie->delete('foo.bar');
By enabling the CSRF Component you get protection against attacks. CSRF118 or Cross Site Request Forgery is
a common vulnerability in web applications. It allows an attacker to capture and replay a previous request, and
sometimes submit data requests using image tags or resources on other domains.
The CsrfComponent works by setting a cookie to the user’s browser. When forms are created with the
Cake\View\Helper\FormHelper, a hidden field is added containing the CSRF token. During the
Controller.startup event, if the request is a POST, PUT, DELETE, PATCH request the component will com-
pare the request data & cookie value. If either is missing or the two values mismatch the component will throw a
Cake\Network\Exception\InvalidCsrfTokenException.
Note: You should always verify the HTTP method being used before executing to avoid side-effects. You should
check the HTTP method or use Cake\Http\ServerRequest::allowMethod() to ensure the correct HTTP
method is used.
Simply by adding the CsrfComponent to your components array, you can benefit from the CSRF protection it
provides:
Settings can be passed into the component through your component’s settings. The available configuration options
are:
• cookieName The name of the cookie to send. Defaults to csrfToken.
• expiry How long the CSRF token should last. Defaults to browser session. Accepts strtotime values as
of 3.1
• secure Whether or not the cookie will be set with the Secure flag. That is, the cookie will only be set on a
HTTPS connection and any attempt over normal HTTP will fail. Defaults to false.
• field The form field to check. Defaults to _csrfToken. Changing this will also require configuring
FormHelper.
When enabled, you can access the current CSRF token on the request object:
118 http://en.wikipedia.org/wiki/Cross-site_request_forgery
$token = $this->request->getParam('_csrfToken');
The CsrfComponent integrates seamlessly with FormHelper. Each time you create a form with FormHelper, it will
insert a hidden field containing the CSRF token.
Note: When using the CsrfComponent you should always start your forms with the FormHelper. If you do not, you
will need to manually create hidden inputs in each of your forms.
In addition to request data parameters, CSRF tokens can be submitted through a special X-CSRF-Token header.
Using a header often makes it easier to integrate a CSRF token with JavaScript heavy applications, or XML/JSON
based API endpoints.
While not recommended, you may want to disable the CsrfComponent on certain requests. You can do this using the
controller’s event dispatcher, during the beforeFilter() method:
Flash
FlashComponent provides two ways to set flash messages: its __call() magic method and its set() method.
To furnish your application with verbosity, FlashComponent’s __call() magic method allows you use a method
name that maps to an element located under the src/Template/Element/Flash directory. By convention, camelcased
methods will map to the lowercased and underscored element name:
// Uses src/Template/Element/Flash/success.ctp
$this->Flash->success('This was successful');
// Uses src/Template/Element/Flash/great_success.ctp
$this->Flash->greatSuccess('This was greatly successful');
Alternatively, to set a plain-text message without rendering an element, you can use the set() method:
$this->Flash->set('This is a message');
New in version 3.1: Flash messages now stack. Successive calls to set() or __call() with the same key will
append the messages in the $_SESSION. If you want to keep the old behavior (one message even after consecutive
calls), set the clear parameter to true when configuring the Component.
FlashComponent’s __call() and set() methods optionally take a second parameter, an array of options:
• key Defaults to ‘flash’. The array key found under the Flash key in the session.
• element Defaults to null, but will automatically be set when using the __call() magic method. The
element name to use for rendering.
• params An optional array of keys/values to make available as variables within an element.
New in version 3.1: A new key clear was added. This key expects a bool and allows you to delete all messages in
the current stack and start a new one.
An example of using these options:
// In your Controller
$this->Flash->success('The user has been saved', [
'key' => 'positive',
'params' => [
'name' => $user->name,
'email' => $user->email
]
]);
// In your View
<?= $this->Flash->render('positive') ?>
Note that the parameter element will be always overridden while using __call(). In order to retrieve a specific
element from a plugin, you should set the plugin parameter. For example:
// In your Controller
$this->Flash->warning('My message', ['plugin' => 'PluginName']);
The code above will use the warning.ctp element under plugins/PluginName/src/Template/Element/Flash for ren-
dering the flash message.
Note: By default, CakePHP escapes the content in flash messages to prevent cross site scripting. User data in your
flash messages will be HTML encoded and safe to be printed. If you want to include HTML in your flash messages,
you need to pass the escape option and adjust your flash message templates to allow disabling escaping when the
escape option is passed.
It is possible to output HTML in flash messages by using the 'escape' option key:
Make sure that you escape the input manually, then. In the above example $highlight and $message are non-
HTML input and therefore escaped.
For more information about rendering your flash messages, please refer to the FlashHelper section.
Security
Note: When using the Security Component you must use the FormHelper to create your forms. In addition, you
must not override any of the fields’ “name” attributes. The Security Component looks for certain indicators that are
created and managed by the FormHelper (especially those created in View\Helper\FormHelper::create()
and View\Helper\FormHelper::end()). Dynamically altering the fields that are submitted in a POST request
(e.g. disabling, deleting or creating new fields via JavaScript) is likely to cause the request to be send to the blackhole
callback.
You should always verify the HTTP method being used before executing to avoid side-effects. You should check the
HTTP method or use Cake\Http\ServerRequest::allowMethod() to ensure the correct HTTP method is
used.
$this->Security->setConfig('blackHoleCallback', 'blackhole');
}
SecurityComponent::requireSecure()
Sets the actions that require a SSL-secured request. Takes any number of arguments. Can be called with no
arguments to force all actions to require a SSL-secured.
SecurityComponent::requireAuth()
Sets the actions that require a valid Security Component generated token. Takes any number of arguments. Can
be called with no arguments to force all actions to require a valid authentication.
allowedControllers A list of controllers which can send requests to this controller. This can be used to control cross
controller requests.
allowedActions A list of actions which are allowed to send requests to this controller’s actions. This can be used to
control cross controller requests.
These configuration options allow you to restrict cross controller communication. Set them with the setConfig()
method, or config() if you are using a CakePHP version below 3.4.
By default the SecurityComponent prevents users from tampering with forms in specific ways. The
SecurityComponent will prevent the following things:
• Unknown fields cannot be added to the form.
• Fields cannot be removed from the form.
• Values in hidden inputs cannot be modified.
Preventing these types of tampering is accomplished by working with the FormHelper and tracking which fields are
in a form. The values for hidden fields are tracked as well. All of this data is combined and turned into a hash. When
a form is submitted, the SecurityComponent will use the POST data to build the same structure and compare the
hash.
Note: The SecurityComponent will not prevent select options from being added/changed. Nor will it prevent radio
options from being added/changed.
unlockedFields Set to a list of form fields to exclude from POST validation. Fields can be unlocked either in the
Component, or with FormHelper::unlockField(). Fields that have been unlocked are not required to
be part of the POST and hidden unlocked fields do not have their values checked.
validatePost Set to false to completely skip the validation of POST requests, essentially turning off form validation.
The above configuration options can be set with setConfig() or config() for CakePHP versions below 3.4.
Usage
Using the security component is generally done in the controller’s beforeFilter(). You would specify the secu-
rity restrictions you want and the Security Component will enforce them on its startup:
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
if ($this->request->getParam('admin')) {
$this->Security->requireSecure();
}
}
}
The above example would force all actions that had admin routing to require secure SSL requests:
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
if ($this->request->getParam('admin')) {
$this->Security->requireSecure();
}
}
throw $exception;
}
}
This example would force all actions that had admin routing to require secure SSL requests. When the request is black
holed, it will call the nominated forceSSL() callback which will redirect non-secure requests to secure requests
automatically.
CSRF Protection
CSRF or Cross Site Request Forgery is a common vulnerability in web applications. It allows an attacker to capture
and replay a previous request, and sometimes submit data requests using image tags or resources on other domains.
To enable CSRF protection features use the Cross Site Request Forgery.
There may be cases where you want to disable all security checks for an action (ex. AJAX requests). You may “unlock”
these actions by listing them in $this->Security->unlockedActions in your beforeFilter(). The
unlockedActions property will not affect other features of SecurityComponent:
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
$this->Security->setConfig('unlockedActions', ['edit']);
}
}
This example would disable all security checks for the edit action.
Pagination
class Cake\Controller\Component\PaginatorComponent
One of the main obstacles of creating flexible and user-friendly web applications is designing an intuitive user interface.
Many applications tend to grow in size and complexity quickly, and designers and programmers alike find they are
unable to cope with displaying hundreds or thousands of records. Refactoring takes time, and performance and user
satisfaction can suffer.
Displaying a reasonable number of records per page has always been a critical part of every application and used to
cause many headaches for developers. CakePHP eases the burden on the developer by providing a quick, easy way to
paginate data.
Pagination in CakePHP is offered by a component in the controller, to make building paginated queries easier. In the
View View\Helper\PaginatorHelper is used to make the generation of pagination links & buttons simple.
Using Controller::paginate()
In the controller, we start by defining the default query conditions pagination will use in the $paginate con-
troller variable. These conditions, serve as the basis for your pagination queries. They are augmented by the sort,
direction, limit, and page parameters passed in from the URL. It is important to note that the order key must
be defined in an array structure like below:
class ArticlesController extends AppController
{
public $paginate = [
'limit' => 25,
'order' => [
'Articles.title' => 'asc'
]
];
You can also include any of the options supported by ORM\Table::find(), such as fields:
class ArticlesController extends AppController
{
public $paginate = [
'fields' => ['Articles.id', 'Articles.created'],
'limit' => 25,
'order' => [
'Articles.title' => 'asc'
]
];
While you can pass most of the query options from the paginate property it is often cleaner and simpler to bundle
up your pagination options into a Custom Finder Methods. You can define the finder pagination uses by setting the
finder option:
class ArticlesController extends AppController
{
public $paginate = [
'finder' => 'published',
];
}
Because custom finder methods can also take in options, this is how you pass in options into a custom finder method
within the paginate property:
class ArticlesController extends AppController
{
// find articles by tag
public function tags()
{
$tags = $this->request->getParam('pass');
$customFinderOptions = [
'tags' => $tags
];
// the custom finder method is called findTagged inside ArticlesTable.php
// it should look like this:
// public function findTagged(Query $query, array $options) {
// hence you use tagged as the key
$this->paginate = [
'finder' => [
'tagged' => $customFinderOptions
]
];
$articles = $this->paginate($this->Articles);
$this->set(compact('articles', 'tags'));
}
}
In addition to defining general pagination values, you can define more than one set of pagination defaults in the
controller, you just name the keys of the array after the model you wish to configure:
The values of the Articles and Authors keys could contain all the properties that a model/key less $paginate
array could.
Once the $paginate property has been defined, we can use the Controller\Controller::paginate()
method to create the pagination data, and add the PaginatorHelper if it hasn’t already been added. The con-
troller’s paginate method will return the result set of the paginated query, and set pagination metadata to the request.
You can access the pagination metadata at $this->request->getParam('paging'). A more complete ex-
ample of using paginate() would be:
By default the paginate() method will use the default model for a controller. You can also pass the resulting query
of a find method:
If you want to paginate a different model you can provide a query for it, the table object itself, or its name:
// Using a query
$comments = $this->paginate($commentsTable->find());
If you need to paginate data from another component you may want to use the PaginatorComponent directly. It features
a similar API to the controller method:
$articles = $this->Paginator->paginate($articleTable->find(), $config);
// Or
$articles = $this->Paginator->paginate($articleTable, $config);
The first parameter should be the query object from a find on table object you wish to paginate results from. Optionally,
you can pass the table object and let the query be constructed for you. The second parameter should be the array of
settings to use for pagination. This array should have the same structure as the $paginate property on a controller.
When paginating a Query object, the finder option will be ignored. It is assumed that you are passing in the query
you want paginated.
You can paginate multiple models in a single controller action, using the scope option both in the controller’s
$paginate property and in the call to the paginate() method:
// Paginate property
public $paginate = [
'Articles' => ['scope' => 'article'],
'Tags' => ['scope' => 'tag']
];
// In a controller action
$articles = $this->paginate($this->Articles, ['scope' => 'article']);
$tags = $this->paginate($this->Tags, ['scope' => 'tag']);
$this->set(compact('articles', 'tags'));
The scope option will result in PaginatorComponent looking in scoped query string parameters. For example,
the following URL could be used to paginate both tags and articles at the same time:
/dashboard?article[page]=1&tag[page]=3
See the Paginating Multiple Results section for how to generate scoped HTML elements and URLs for pagination.
New in version 3.3.0: Multiple Pagination was added in 3.3.0
To paginate the same model multiple times within a single controller action you need to define an alias for the model.
See Using the TableRegistry for additional details on how to use the table registry:
// In a controller action
$this->paginate = [
'ArticlesTable' => [
'scope' => 'published_articles',
'limit' => 10,
'order' => [
'id' => 'desc',
],
],
'UnpublishedArticlesTable' => [
'scope' => 'unpublished_articles',
'limit' => 10,
'order' => [
'id' => 'desc',
],
],
];
TableRegistry::config('UnpublishedArticles', [
'className' => 'App\Model\Table\ArticlesTable',
'table' => 'articles',
'entityClass' => 'App\Model\Entity\Article',
]);
$publishedArticles = $this->paginate(
$this->Articles->find('all', [
'scope' => 'published_articles'
])->where(['published' => true])
);
$unpublishedArticles = $this->paginate(
TableRegistry::getTableLocator()->get('UnpublishedArticles')->find('all', [
'scope' => 'unpublished_articles'
])->where(['published' => false])
);
By default sorting can be done on any non-virtual column a table has. This is sometimes undesirable as it allows
users to sort on un-indexed columns that can be expensive to order by. You can set the whitelist of fields that can be
sorted using the sortWhitelist option. This option is required when you want to sort on any associated data, or
computed fields that may be part of your pagination query:
public $paginate = [
'sortWhitelist' => [
'id', 'title', 'Users.username', 'created'
]
];
Any requests that attempt to sort on fields not in the whitelist will be ignored.
The number of results that are fetched per page is exposed to the user as the limit parameter. It is generally
undesirable to allow users to fetch all rows in a paginated set. The maxLimit option asserts that no one can set this
limit too high from the outside. By default CakePHP limits the maximum number of rows that can be fetched to 100.
If this default is not appropriate for your application, you can adjust it as part of the pagination options, for example
reducing it to 10:
public $paginate = [
// Other keys here.
'maxLimit' => 10
];
If the request’s limit param is greater than this value, it will be reduced to the maxLimit value.
Additional associations can be loaded to the paginated table by using the contain parameter:
public function index()
{
$this->paginate = [
'contain' => ['Authors', 'Comments']
];
$this->set('articles', $this->paginate($this->Articles));
}
The PaginatorComponent will throw a NotFoundException when trying to access a non-existent page, i.e. page
number requested is greater than total page count.
So you could either let the normal error page be rendered or use a try catch block and take appropriate action when a
NotFoundException is caught:
// Prior to 3.6 use Cake\Network\Exception\NotFoundException
use Cake\Http\Exception\NotFoundException;
Check the View\Helper\PaginatorHelper documentation for how to create links for pagination navigation.
Request Handling
// Rest of controller
}
Request Handler has several methods that provide information about the client and its request.
RequestHandlerComponent::accepts($type = null)
$type can be a string, or an array, or null. If a string, accepts() will return true if the client accepts the
content type. If an array is specified, accepts() return true if any one of the content types is accepted by
the client. If null returns an array of the content-types that the client accepts. For example:
RequestHandlerComponent::isRss()
Returns true if the current request accepts RSS as a response.
RequestHandlerComponent::isAtom()
Returns true if the current call accepts an Atom response, false otherwise.
RequestHandlerComponent::isMobile()
Returns true if user agent string matches a mobile web browser, or if the client accepts WAP content. The
supported Mobile User Agent strings are:
• Android
• AvantGo
• BlackBerry
• DoCoMo
• Fennec
• iPad
• iPhone
• iPod
• J2ME
• MIDP
• NetFront
• Nokia
• Opera Mini
• Opera Mobi
• PalmOS
• PalmSource
• portalmmm
• Plucker
• ReqwirelessWeb
• SonyEricsson
• Symbian
• UP.Browser
• webOS
• Windows CE
• Windows Phone OS
• Xiino
RequestHandlerComponent::isWap()
Returns true if the client accepts WAP content.
All of the above request detection methods can be used in a similar fashion to filter functionality intended for specific
content types. For example when responding to AJAX requests, you often will want to disable browser caching, and
change the debug level. However, you want to allow caching for non-AJAX requests. The following would accomplish
that:
if ($this->request->is('ajax')) {
$this->response = $this->response->withDisabledCache();
// Prior to 3.4.0
$this->response->disableCache();
}
// Continue Controller action
Add a request data decoder. The handler should contain a callback, and any additional arguments for the callback. The
callback should return an array of data contained in the request input. For example adding a CSV handler could look
like:
You can use any callable119 for the handling function. You can also pass additional arguments to the callback, this is
useful for callbacks like json_decode:
The above will make $this->request->getData() an array of the JSON input data, without the additional
true you’d get a set of stdClass objects.
Deprecated since version 3.1.0: As of 3.1.0 the addInputType() method is deprecated. You should use
config() to add input types at runtime.
Changed in version 3.6.0: You should prefer using body-parser-middleware instead of RequestHandlerComponent.
RequestHandlerComponent::prefers($type = null)
119 http://php.net/callback
Determines which content-types the client prefers. If no parameter is given the most likely content type is returned.
If $type is an array the first type the client accepts will be returned. Preference is determined primarily by the file
extension parsed by Router if one has been provided, and secondly by the list of content-types in HTTP_ACCEPT:
$this->RequestHandler->prefers('json');
Responding To Requests
RequestHandlerComponent::renderAs($controller, $type)
Change the render mode of a controller to the specified type. Will also append the appropriate helper to the controller’s
helper array if available and not already in the array:
This method will also attempt to add a helper that matches your current content type. For example if you render as
rss, the RssHelper will be added.
RequestHandlerComponent::respondAs($type, $options)
Sets the response header based on content-type map names. This method lets you set a number of response properties
at once:
$this->RequestHandler->respondAs('xml', [
// Force download
'attachment' => true,
'charset' => 'UTF-8'
]);
RequestHandlerComponent::responseType()
Returns the current response type Content-type header or null if one has yet to be set.
The HTTP cache validation model is one of the processes used for cache gateways, also known as reverse proxies, to
determine if they can serve a stored copy of a response to the client. Under this model, you mostly save bandwidth,
but when used correctly you can also save some CPU processing, reducing this way response times.
Enabling the RequestHandlerComponent in your controller automatically activates a check done before rendering the
view. This check compares the response object against the original request to determine whether the response was not
modified since the last time the client asked for it.
If response is evaluated as not modified, then the view rendering process is stopped, saving processing time, saving
bandwidth and no content is returned to the client. The response status code is then set to 304 Not Modified.
You can opt-out this automatic checking by setting the checkHttpCache setting to false:
When using JsonView/XmlView you might want to override the default serialization with a custom View class, or add
View classes for other types.
You can map existing and new types to your custom classes. You can also set this automatically by using the
viewClassMap setting:
Deprecated since version 3.1.0: As of 3.1.0 the viewClassMap() method is deprecated. You should use
config() to change the viewClassMap at runtime.
Configuring Components
Many of the core components require configuration. Some examples of components requiring configuration are Au-
thComponent and Cookie. Configuration for these components, and for components in general, is usually done via
loadComponent() in your Controller’s initialize() method or via the $components array:
You can configure components at runtime using the config() method. Often, this is done in your controller’s
beforeFilter() method. The above could also be expressed as:
$this->Cookie->config('name', 'CookieMonster');
}
Like helpers, components implement a config() method that is used to get and set any configuration data for a
component:
// Set config
$this->Csrf->config('cookieName', 'token');
As with helpers, components will automatically merge their $_defaultConfig property with constructor config-
uration to create the $_config property which is accessible with config().
Aliasing Components
One common setting to use is the className option, which allows you to alias components. This feature is useful
when you want to replace $this->Auth or another common Component reference with a custom implementation:
// src/Controller/PostsController.php
class PostsController extends AppController
{
public function initialize()
{
$this->loadComponent('Auth', [
'className' => 'MyAuth'
]);
}
}
// src/Controller/Component/MyAuthComponent.php
use Cake\Controller\Component\AuthComponent;
Note: Aliasing a component replaces that instance anywhere that component is used, including inside other Compo-
nents.
You might not need all of your components available on every controller action. In situations like this you can load a
component at runtime using the loadComponent() method in your controller:
// In a controller action
$this->loadComponent('OneTimer');
$time = $this->OneTimer->getTime();
Note: Keep in mind that components loaded on the fly will not have missed callbacks called. If you rely on the
beforeFilter or startup callbacks being called, you may need to call them manually depending on when you
load your component.
Using Components
Once you’ve included some components in your controller, using them is pretty simple. Each
component you use is exposed as a property on your controller. If you had loaded up the
Cake\Controller\Component\FlashComponent in your controller, you could access it like so:
Note: Since both Models and Components are added to Controllers as properties they share the same ‘namespace’.
Be sure to not give a component and a model the same name.
Creating a Component
Suppose our application needs to perform a complex mathematical operation in many different parts of the application.
We could create a component to house this shared logic for use in many different controllers.
The first step is to create a new component file and class. Create the file in
src/Controller/Component/MathComponent.php. The basic structure for the component would look some-
thing like this:
namespace App\Controller\Component;
use Cake\Controller\Component;
Note: All components must extend Cake\Controller\Component. Failing to do this will trigger an exception.
Once our component is finished, we can use it in the application’s controllers by loading it during the controller’s
initialize() method. Once loaded, the controller will be given a new attribute named after the component,
through which we can access an instance of it:
// In a controller
// Make the new component available at $this->Math,
// as well as the standard $this->Csrf
public function initialize()
{
parent::initialize();
$this->loadComponent('Math');
$this->loadComponent('Csrf');
}
When including Components in a Controller you can also declare a set of parameters that will be passed on to the
Component’s constructor. These parameters can then be handled by the Component:
// In your controller.
public function initialize()
{
parent::initialize();
$this->loadComponent('Math', [
'precision' => 2,
'randomGenerator' => 'srand'
]);
$this->loadComponent('Csrf');
}
The above would pass the array containing precision and randomGenerator to MathComponent::initialize()
in the $config parameter.
Sometimes one of your components may need to use another component. In this case you can include other compo-
nents in your component the exact same way you include them in controllers - using the $components var:
// src/Controller/Component/CustomComponent.php
namespace App\Controller\Component;
use Cake\Controller\Component;
// ...
}
}
// src/Controller/Component/ExistingComponent.php
namespace App\Controller\Component;
use Cake\Controller\Component;
Note: In contrast to a component included in a controller no callbacks will be triggered on a component’s component.
From within a Component you can access the current controller through the registry:
$controller = $this->_registry->getController();
You can access the controller in any callback method from the event object:
$controller = $event->getSubject();
Component Callbacks
Components also offer a few request life-cycle callbacks that allow them to augment the request cycle.
beforeFilter(Event $event)
Is called before the controller’s beforeFilter method, but after the controller’s initialize() method.
startup(Event $event)
Is called after the controller’s beforeFilter method but before the controller executes the current action handler.
beforeRender(Event $event)
Is called after the controller executes the requested action’s logic, but before the controller renders views and
layout.
shutdown(Event $event)
Is called before output is sent to the browser.
beforeRedirect(Event $event, $url, Response $response)
Is invoked when the controller’s redirect method is called but before any further action. If this method returns
false the controller will not continue on to redirect the request. The $url, and $response parameters allow you
to inspect and modify the location or any other headers in the response.
Views
class Cake\View\View
Views are the V in MVC. Views are responsible for generating the specific output required for the request. Often this
is in the form of HTML, XML, or JSON, but streaming files and creating PDF’s that users can download are also
responsibilities of the View Layer.
CakePHP comes with a few built-in View classes for handling the most common rendering scenarios:
• To create XML or JSON webservices you can use the JSON and XML views.
• To serve protected files, or dynamically generated files, you can use Sending Files.
• To create multiple themed views, you can use Themes.
AppView is your application’s default View class. AppView itself extends the Cake\View\View class included
in CakePHP and is defined in src/View/AppView.php as follows:
<?php
namespace App\View;
use Cake\View\View;
You can use your AppView to load helpers that will be used for every view rendered in your application. CakePHP
provides an initialize() method that is invoked at the end of a View’s constructor for this kind of use:
263
CakePHP Cookbook Documentation, Release 3.8
<?php
namespace App\View;
use Cake\View\View;
View Templates
The view layer of CakePHP is how you speak to your users. Most of the time your views will be rendering
HTML/XHTML documents to browsers, but you might also need to reply to a remote application via JSON, or output
a CSV file for a user.
CakePHP template files have a default extension of .ctp (CakePHP Template) and utilize the alternative PHP syntax120
for control structures and output. These files contain the logic necessary to prepare the data received from the controller
into a presentation format that is ready for your audience.
Alternative Echos
Control structures, like if, for, foreach, switch, and while can be written in a simplified format. Notice that
there are no braces. Instead, the end brace for the foreach is replaced with endforeach. Each of the control
structures listed above has a similar closing syntax: endif, endfor, endforeach, and endwhile. Also notice
that instead of using a semicolon after each structure (except the last one), there is a colon.
The following is an example using foreach:
<ul>
<?php foreach ($todo as $item): ?>
<li><?= $item ?></li>
<?php endforeach; ?>
</ul>
120 http://php.net/manual/en/control-structures.alternative-syntax.php
If you’d prefer using a templating language like Twig121 , a subclass of View will bridge your templating language and
CakePHP.
Template files are stored in src/Template/, in a folder named after the controller that uses the files, and named after the
action it corresponds to. For example, the view file for the Products controller’s view() action, would normally
be found in src/Template/Products/view.ctp.
The view layer in CakePHP can be made up of a number of different parts. Each part has different uses, and will be
covered in this chapter:
• templates: Templates are the part of the page that is unique to the action being run. They form the meat of your
application’s response.
• elements: small, reusable bits of view code. Elements are usually rendered inside views.
• layouts: template files that contain presentational code that wraps many interfaces in your application. Most
views are rendered inside a layout.
• helpers: these classes encapsulate view logic that is needed in many places in the view layer. Among other
things, helpers in CakePHP can help you build forms, build AJAX functionality, paginate model data, or serve
RSS feeds.
• cells: these classes provide miniature controller-like features for creating self contained UI components. See the
View Cells documentation for more information.
View Variables
Any variables you set in your controller with set() will be available in both the view and the layout your action
renders. In addition, any set variables will also be available in any element. If you need to pass additional variables
from the view to the layout you can either call set() in the view template, or use a Using View Blocks.
You should remember to always escape any user data before outputting it as CakePHP does not automatically escape
output. You can escape user content with the h() function:
$this->set('activeMenuButton', 'posts');
Then, in your layout, the $activeMenuButton variable will be available and contain the value ‘posts’.
Extending Views
View extending allows you to wrap one view in another. Combining this with view blocks gives you a powerful way
to keep your views DRY. For example, your application has a sidebar that needs to change depending on the specific
view being rendered. By extending a common view file, you can avoid repeating the common markup for your sidebar,
and only define the parts that change:
<!-- src/Template/Common/view.ctp -->
<h1><?= h($this->fetch('title')) ?></h1>
<?= $this->fetch('content') ?>
<div class="actions">
<h3>Related actions</h3>
<ul>
<?= $this->fetch('sidebar') ?>
</ul>
</div>
The above view file could be used as a parent view. It expects that the view extending it will define the sidebar
and title blocks. The content block is a special block that CakePHP creates. It will contain all the uncaptured
content from the extending view. Assuming our view file has a $post variable with the data about our post, the view
could look like:
<!-- src/Template/Posts/view.ctp -->
<?php
$this->extend('/Common/view');
$this->assign('title', $post->title);
$this->start('sidebar');
?>
<li>
<?php
echo $this->Html->link('edit', [
'action' => 'edit',
$post->id
]);
?>
</li>
<?php $this->end(); ?>
The post view above shows how you can extend a view, and populate a set of blocks. Any content not already in
a defined block will be captured and put into a special block named content. When a view contains a call to
extend(), execution continues to the bottom of the current view file. Once it is complete, the extended view will be
rendered. Calling extend() more than once in a view file will override the parent view that will be processed next:
$this->extend('/Common/view');
$this->extend('/Common/index');
The above will result in /Common/index.ctp being rendered as the parent view to the current view.
You can nest extended views as many times as necessary. Each view can extend another view if desired. Each parent
view will get the previous view’s content as the content block.
Note: You should avoid using content as a block name in your application. CakePHP uses this for uncaptured
content in extended views.
You can get the list of all populated blocks using the blocks() method:
$list = $this->blocks();
View blocks provide a flexible API that allows you to define slots or blocks in your views/layouts that will be defined
elsewhere. For example, blocks are ideal for implementing things such as sidebars, or regions to load assets at the
bottom/top of the layout. Blocks can be defined in two ways: either as a capturing block, or by direct assignment.
The start(), append(), prepend(), assign(), fetch(), and end() methods allow you to work with
capturing blocks:
// Create the sidebar block.
$this->start('sidebar');
echo $this->element('sidebar/recent_topics');
echo $this->element('sidebar/recent_comments');
$this->end();
If you need to clear or overwrite a block there are a couple of alternatives. The reset() method will clear or
overwrite a block at any time. The assign() method with an empty content string can also be used to clear the
specified block.:
// Clear the previous content from the sidebar block.
$this->reset('sidebar');
// Prepend to sidebar
$this->prepend('sidebar', 'this content goes on top of sidebar');
Displaying Blocks
You can display blocks using the fetch() method. fetch() will output a block, returning ‘’ if a block does not
exist:
You can also use fetch to conditionally show content that should surround a block should it exist. This is helpful in
layouts, or extended views where you want to conditionally show headings or other markup:
// In src/Template/Layout/default.ctp
<?php if ($this->fetch('menu')): ?>
<div class="menu">
<h3>Menu options</h3>
<?= $this->fetch('menu') ?>
</div>
<?php endif; ?>
You can also provide a default value for a block if it does not exist. This allows you to add placeholder content when
a block does not exist. You can provide a default value using the second argument:
<div class="shopping-cart">
<h3>Your Cart</h3>
<?= $this->fetch('cart', 'Your cart is empty') ?>
</div>
The HtmlHelper ties into view blocks, and its script(), css(), and meta() methods each update a block
with the same name when used with the block = true option:
<?php
// In your view file
$this->Html->script('carousel', ['block' => true]);
$this->Html->css('carousel', ['block' => true]);
?>
The Cake\View\Helper\HtmlHelper also allows you to control which block the scripts and CSS go to:
// In your view
$this->Html->script('carousel', ['block' => 'scriptBottom']);
// In your layout
<?= $this->fetch('scriptBottom') ?>
Layouts
A layout contains presentation code that wraps around a view. Anything you want to see in all of your views should
be placed in a layout.
CakePHP’s default layout is located at src/Template/Layout/default.ctp. If you want to change the overall look of
your application, then this is the right place to start, because controller-rendered view code is placed inside of the
default layout when the page is rendered.
Other layout files should be placed in src/Template/Layout. When you create a layout, you need to tell
CakePHP where to place the output of your views. To do so, make sure your layout includes a place for
$this->fetch('content') Here’s an example of what a default layout might look like:
<!DOCTYPE html>
<html lang="en">
<head>
<title><?= h($this->fetch('title')) ?></title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<!-- Include external files and scripts here (See HTML helper for more info.) -->
<?php
echo $this->fetch('meta');
echo $this->fetch('css');
echo $this->fetch('script');
?>
</head>
<body>
</body>
</html>
The script, css and meta blocks contain any content defined in the views using the built-in HTML helper. Useful
for including JavaScript and CSS files from views.
Layouts 269
CakePHP Cookbook Documentation, Release 3.8
Empty values for the title block will be automatically replaced with a representation of the current template path,
such as 'Admin/Articles'.
You can create as many layouts as you wish: just place them in the src/Template/Layout directory, and switch between
them inside of your controller actions using the controller or view’s $layout property:
// From a controller
public function view()
{
// Set the layout.
$this->viewBuilder()->setLayout('admin');
// Before 3.4
$this->viewBuilder()->layout('admin');
// Before 3.1
$this->layout = 'admin';
}
For example, if a section of my site included a smaller ad banner space, I might create a new layout with the smaller
advertising space and specify it as the layout for all controllers’ actions using something like:
namespace App\Controller;
Besides a default layout CakePHP’s official skeleton app also has an ‘ajax’ layout. The Ajax layout is handy for
crafting AJAX responses - it’s an empty layout. (Most AJAX calls only require a bit of markup in return, rather than a
fully-rendered interface.)
The skeleton app also has a default layout to help generate RSS.
If you want to use a layout that exists in a plugin, you can use plugin syntax. For example, to use the contact layout
from the Contacts plugin:
namespace App\Controller;
Elements
echo $this->element('helpbox');
You can pass data to an element through the element’s second argument:
echo $this->element('helpbox', [
"helptext" => "Oh, this text is very helpful."
]);
Inside the element file, all the passed variables are available as members of the parameter array. In the above example,
the src/Template/Element/helpbox.ctp file can use the $helptext variable:
// Inside src/Template/Element/helpbox.ctp
echo $helptext; // Outputs "Oh, this text is very helpful."
Keep in mind that in those view vars are merged with the view vars from the view itself. So all view vars set using
Controller::set() in the controller and View::set() in the view itself are also available inside the element.
Elements 271
CakePHP Cookbook Documentation, Release 3.8
The View::element() method also supports options for the element. The options supported are ‘cache’ and
‘callbacks’. An example:
echo $this->element('helpbox', [
"helptext" => "This is passed to the element as $helptext",
"foobar" => "This is passed to the element as $foobar",
],
[
// uses the "long_view" cache configuration
"cache" => "long_view",
// set to true to have before/afterRender called for the element
"callbacks" => true
]
);
Element caching is facilitated through the Cache class. You can configure elements to be stored in any Cache
configuration you’ve set up. This gives you a great amount of flexibility to decide where and for how long elements
are stored. To cache different versions of the same element in an application, provide a unique cache key value using
the following format:
$this->element('helpbox', [], [
"cache" => ['config' => 'short', 'key' => 'unique value']
]
);
If you need more logic in your element, such as dynamic data from a datasource, consider using a View Cell instead
of an element. Find out more about View Cells.
Caching Elements
You can take advantage of CakePHP view caching if you supply a cache parameter. If set to true, it will cache the
element in the ‘default’ Cache configuration. Otherwise, you can set which cache configuration should be used. See
Caching for more information on configuring Cache. A simple example of caching an element would be:
If you render the same element more than once in a view and have caching enabled, be sure to set the ‘key’ parameter
to a different name each time. This will prevent each successive call from overwriting the previous element() call’s
cached result. For example:
echo $this->element(
'helpbox',
['var' => $var],
['cache' => ['key' => 'first_use', 'config' => 'view_long']]
);
echo $this->element(
'helpbox',
['var' => $differenVar],
['cache' => ['key' => 'second_use', 'config' => 'view_long']]
);
The above will ensure that both element results are cached separately. If you want all element caching to use the same
cache configuration, you can avoid some repetition by setting View::$elementCache to the cache configuration
you want to use. CakePHP will use this configuration when none is given.
If you are using a plugin and wish to use elements from within the plugin, just use the familiar plugin syntax. If the
view is being rendered for a plugin controller/action, the plugin name will automatically be prefixed onto all elements
used, unless another plugin name is present. If the element doesn’t exist in the plugin, it will look in the main APP
folder:
echo $this->element('Contacts.helpbox');
If your view is a part of a plugin, you can omit the plugin name. For example, if you are in the
ContactsController of the Contacts plugin, the following:
echo $this->element('helpbox');
// and
echo $this->element('Contacts.helpbox');
are equivalent and will result in the same element being rendered.
For elements inside subfolder of a plugin (e.g., plugins/Contacts/Template/Element/sidebar/helpbox.ctp), use the
following:
echo $this->element('Contacts.sidebar/helpbox');
If you are within a plugin’s template file and want to render an element residing in your main application rather than
this or another plugin, use the following:
echo $this->element('my_element');
The element first be looked for in src/Template/Admin/Element/. If such a file does not exist, it will be looked for in
the default location.
Elements 273
CakePHP Cookbook Documentation, Release 3.8
By default cached view content will go into the View::$elementCache cache config, but you can use the config
option to change this.
View Events
Like Controller, view trigger several events/callbacks that you can use to insert logic around the rendering life-cycle:
Event List
• View.beforeRender
• View.beforeRenderFile
• View.afterRenderFile
• View.afterRender
• View.beforeLayout
• View.afterLayout
You can attach application event listeners to these events or use Helper Callbacks.
You may need to create custom view classes to enable new types of data views, or add additional custom view-
rendering logic to your application. Like most components of CakePHP, view classes have a few conventions:
• View class files should be put in src/View. For example: src/View/PdfView.php
• View classes should be suffixed with View. For example: PdfView.
• When referencing view class names you should omit the View suffix. For example:
$this->viewBuilder()->setClassName('Pdf');.
You’ll also want to extend View to ensure things work correctly:
// In src/View/PdfView.php
namespace App\View;
use Cake\View\View;
Replacing the render method lets you take full control over how your content is rendered.
View Cells
View cells are small mini-controllers that can invoke view logic and render out templates. The idea of cells is borrowed
from cells in Ruby122 , where they fulfill a similar role and purpose.
Cells are ideal for building reusable page components that require interaction with models, view logic, and rendering
logic. A simple example would be the cart in an online store, or a data-driven navigation menu in a CMS.
Creating a Cell
To create a cell, define a class in src/View/Cell and a template in src/Template/Cell/. In this example, we’ll be making
a cell to display the number of messages in a user’s notification inbox. First, create the class file. Its contents should
look like:
namespace App\View\Cell;
use Cake\View\Cell;
Save this file into src/View/Cell/InboxCell.php. As you can see, like other classes in CakePHP, Cells have a few
conventions:
• Cells live in the App\View\Cell namespace. If you are making a cell in a plugin, the namespace would be
PluginName\View\Cell.
• Class names should end in Cell.
• Classes should inherit from Cake\View\Cell.
We added an empty display() method to our cell; this is the conventional default method when rendering a cell.
We’ll cover how to use other methods later in the docs. Now, create the file src/Template/Cell/Inbox/display.ctp.
This will be our template for our new cell.
You can generate this stub code quickly using bake:
Assume that we are working on an application that allows users to send messages to each other. We have a Messages
model, and we want to show the count of unread messages without having to pollute AppController. This is a perfect
use case for a cell. In the class we just made, add the following:
namespace App\View\Cell;
use Cake\View\Cell;
Because Cells use the ModelAwareTrait and ViewVarsTrait, they behave very much like a controller would.
We can use the loadModel() and set() methods just like we would in a controller. In our template file, add the
following:
Note: Cell templates have an isolated scope that does not share the same View instance as the one used to render
template and layout for the current controller action or other cells. Hence they are unaware of any helper calls made
or blocks set in the action’s template / layout and vice versa.
Loading Cells
Cells can be loaded from views using the cell() method and works the same in both contexts:
The above will load the named cell class and execute the display() method. You can execute other methods using
the following:
If you need controller logic to decide which cells to load in a request, you can use the CellTrait in your controller
to enable the cell() method there:
namespace App\Controller;
use App\Controller\AppController;
use Cake\View\CellTrait;
// More code.
}
You will often want to parameterize cell methods to make cells more flexible. By using the second and third arguments
of cell(), you can pass action parameters and additional options to your cell classes, as an indexed array:
Rendering a Cell
Once a cell has been loaded and executed, you’ll probably want to render it. The easiest way to render a cell is to echo
it:
This will render the template matching the lowercased and underscored version of our action name, e.g. display.ctp.
Because cells use View to render templates, you can load additional cells within a cell template if required.
Note: Echoing a cell uses the PHP __toString() magic method which prevents PHP from showing the file-
name and line number for any fatal errors raised. To obtain a meanful error message, it is recommended to use the
Cell::render() method, for example <?= $cell->render() ?>.
By convention cells render templates that match the action they are executing. If you need to render a different view
template, you can specify the template to use when rendering the cell:
// Before 3.4
$cell->viewBuilder()->template('messages');
// Before 3.1
$cell->template = 'messages';
echo $cell;
When rendering a cell you may want to cache the rendered output if the contents don’t change often or to help improve
performance of your application. You can define the cache option when creating a cell to enable & configure caching:
// Cache using the default config and a generated key
$cell = $this->cell('Inbox', [], ['cache' => true]);
If a key is generated the underscored version of the cell class and template name will be used.
Note: A new View instance is used to render each cell and these new objects do not share context with the main tem-
plate / layout. Each cell is self-contained and only has access to variables passed as arguments to the View::cell()
call.
Creating a cell that renders a paginated result set can be done by leveraging the Paginator class of the ORM. An
example of paginating a user’s favorite messages could look like:
namespace App\View\Cell;
use Cake\View\Cell;
use Cake\Datasource\Paginator;
// Create a paginator
$paginator = new Paginator();
$this->set('favorites', $results);
}
}
The above cell would paginate the Messages model using scoped pagination parameters.
New in version 3.5.0: Cake\Datasource\Paginator was added in 3.5.0.
Cell Options
Cells can declare constructor options that are converted into properties when creating a cell object:
namespace App\View\Cell;
use Cake\View\Cell;
use Cake\Datasource\Paginator;
protected $limit = 3;
Here we have defined a $limit property and add limit as a cell option. This will allow us to define the option
when creating the cell:
$cell = $this->cell('Favorites', [$user->id], ['limit' => 10])
Cell options are handy when you want data available as properties allowing you to override default values.
Themes
Themes in CakePHP are simply plugins that focus on providing template files. See the section on Creating Your Own
Plugins. You can take advantage of themes, making it easy to switch the look and feel of your page quickly. In addition
to template files, they can also provide helpers and cells if your theming requires that. When using cells and helpers
from your theme, you will need to continue using the plugin syntax.
To use themes, set the theme name in your controller’s action or beforeRender() callback:
Theme template files need to be within a plugin with the same name. For example, the above theme would be
found in plugins/Modern/src/Template. It’s important to remember that CakePHP expects PascalCase plugin/theme
names. Beyond that, the folder structure within the plugins/Modern/src/Template folder is exactly the same as
src/Template/.
For example, the view file for an edit action of a Posts controller would reside at plug-
ins/Modern/src/Template/Posts/edit.ctp. Layout files would reside in plugins/Modern/src/Template/Layout/. You
can provide customized templates for plugins with a theme as well. If you had a plugin named ‘Cms’, that contained
a TagsController, the Modern theme could provide plugins/Modern/src/Template/Plugin/Cms/Tags/edit.ctp to
replace the edit template in the plugin.
If a view file can’t be found in the theme, CakePHP will try to locate the view file in the src/Template/ folder. This
way, you can create master template files and simply override them on a case-by-case basis within your theme folder.
If your theme also acts as a plugin, don’t forget to ensure it is loaded in your application’s bootstrap method. For
example:
Theme Assets
Because themes are standard CakePHP plugins, they can include any necessary assets in their webroot directory. This
allows for easy packaging and distribution of themes. Whilst in development, requests for theme assets will be handled
by Cake\Routing\Dispatcher. To improve performance for production environments, it’s recommended that
you Improve Your Application’s Performance.
All of CakePHP’s built-in helpers are aware of themes and will create the correct paths automatically. Like template
files, if a file isn’t in the theme folder, it will default to the main webroot folder:
// and links to
plugins/PurpleCupcake/webroot/css/main.css
The JsonView and XmlView let you create JSON and XML responses, and integrate with the
Cake\Controller\Component\RequestHandlerComponent.
By enabling RequestHandlerComponent in your application, and enabling support for the json and/or xml
extensions, you can automatically leverage the new view classes. JsonView and XmlView will be referred to as
data views for the rest of this page.
There are two ways you can generate data views. The first is by using the _serialize key, and the second is by
creating normal template files.
Before you can use the data view classes, you’ll first need to load the
Cake\Controller\Component\RequestHandlerComponent in your controller:
public function initialize()
{
...
$this->loadComponent('RequestHandler');
}
This can be done in your AppController and will enable automatic view class switching on content types. You
can also set the component up with the viewClassMap setting, to map types to your custom classes and/or map
other data types.
You can optionally enable the json and/or xml extensions with Routing File Extensions. This will allow you to access
the JSON, XML or any other special format views by using a custom URL ending with the name of the response type
as a file extension such as http://example.com/articles.json.
By default, when not enabling Routing File Extensions, the request, the Accept header is used for, selecting which
type of format should be rendered to the user. An example Accept format that is used to render JSON responses is
application/json.
The _serialize key is a special view variable that indicates which other view variable(s) should be serialized when
using a data view. This lets you skip defining template files for your controller actions if you don’t need to do any
custom formatting before your data is converted into json/xml.
If you need to do any formatting or manipulation of your view variables before generating the response, you should
use template files. The value of _serialize can be either a string or an array of view variables to serialize:
namespace App\Controller;
$this->set('articles', $this->paginate());
// Specify which view vars JsonView should serialize.
$this->set('_serialize', 'articles');
}
}
namespace App\Controller;
Defining _serialize as an array has added the benefit of automatically appending a top-level <response>
element when using XmlView. If you use a string value for _serialize and XmlView, make sure that your view
variable has a single top-level element. Without a single top-level element the Xml will fail to generate.
You should use template files if you need to do some manipulation of your view content before creating the final
output. For example if we had articles, that had a field containing generated HTML, we would probably want to omit
that from a JSON response. This is a situation where a view file would be useful:
// Controller code
class ArticlesController extends AppController
{
public function index()
{
$articles = $this->paginate('Articles');
$this->set(compact('articles'));
}
}
You can do more complex manipulations, or use helpers to do formatting as well. The data view classes don’t support
layouts. They assume that the view file will output the serialized content.
Note: As of 3.1.0 until 3.5.0, AppController in the application skeleton automatically added '_serialize'
=> true to all XML/JSON requests. You will need to remove this code from the beforeRender callback or set
'_serialize' => false in your controller’s action if you want to use view files.
class XmlView
By default when using _serialize the XmlView will wrap your serialized view variables with a <response>
node. You can set a custom name for this node using the _rootNode view variable.
The XmlView class supports the _xmlOptions variable that allows you to customize the options used to generate
XML, e.g. tags vs attributes.
An example of using XmlView would be to generate a sitemap.xml123 . This document type requires that you change
_rootNode and set attributes. Attributes are defined using the @ prefix:
class JsonView
The JsonView class supports the _jsonOptions variable that allows you to customize the bit-mask used to generate
JSON. See the json_encode124 documentation for the valid values of this option.
For example, to serialize validation error output of CakePHP entities in a consistent form of JSON do:
123 https://www.sitemaps.org/protocol.html
124 http://php.net/json_encode
JSONP Responses
When using JsonView you can use the special view variable _jsonp to enable returning a JSONP response. Setting
it to true makes the view class check if query string parameter named “callback” is set and if so wrap the json
response in the function name provided. If you want to use a custom query string parameter name instead of “callback”
set _jsonp to required name instead of true.
Example Usage
While the RequestHandlerComponent can automatically set the view based on the request content-type or extension,
you could also handle view mappings in your controller:
// src/Controller/VideosController.php
namespace App\Controller;
use App\Controller\AppController;
// Get data
$videos = $this->Videos->find('latest');
Helpers
Helpers are the component-like classes for the presentation layer of your application. They contain presentational
logic that is shared between many views, elements, or layouts. This chapter will show you how to configure helpers.
How to load helpers and use those helpers, and outline the simple steps for creating your own custom helpers.
CakePHP includes a number of helpers that aid in view creation. They assist in creating well-formed markup (including
forms), aid in formatting text, times and numbers, and can even speed up AJAX functionality. For more information
on the helpers included in CakePHP, check out the chapter for each helper:
Breadcrumbs
You can add a crumb to the list using the add() method. It takes three arguments:
• title The string to be displayed as a the title of the crumb
• url A string or an array of parameters that will be given to the Url
• options An array of attributes for the item and itemWithoutLink templates. See the section about defining
attributes for the item for more information.
In addition to adding to the end of the trail, you can do a variety of operations:
// Add at the end of the trail
$this->Breadcrumbs->add(
'Products',
['controller' => 'products', 'action' => 'index']
);
['title' => 'Product name', 'url' => ['controller' => 'products', 'action' =>
˓→'view', 1234]]
]);
// Prepend multiple crumbs at the top of the trail, in the order given
$this->Breadcrumbs->prepend([
['title' => 'Products', 'url' => ['controller' => 'products', 'action' => 'index
˓→']],
['title' => 'Product name', 'url' => ['controller' => 'products', 'action' =>
˓→'view', 1234]]
]);
Using these methods gives you the ability to work with CakePHP’s 2-step rendering process. Since templates and
layouts are rendered from the inside out (meaning, included elements are rendered first), this allows you to define
precisely where you want to add a breadcrumb.
After adding crumbs to the trail, you can easily render it using the render() method. This method accepts two array
arguments:
• $attributes : An array of attributes that will applied to the wrapper template. This gives you the ability
to add attributes to the HTML tag. It accepts the special templateVars key to allow the insertion of custom
template variables in the template.
• $separator : An array of attributes for the separator template. Possible properties are:
– separator The string to be displayed as a separator
– innerAttrs To provide attributes in case your separator is divided in two elements
– templateVars Allows the insertion of custom template variable in the template
All other properties will be converted as HTML attributes and will replace the attrs key in the template. If
you use the default for this option (empty), it will not render a separator.
Here is an example of how to render a trail:
echo $this->Breadcrumbs->render(
['class' => 'breadcrumbs-trail'],
['separator' => '<i class="fa fa-angle-right"></i>']
);
The BreadcrumbsHelper internally uses the StringTemplateTrait, which gives the ability to easily customize
output of various HTML strings. It includes four templates, with the following default declaration:
[
'wrapper' => '<ul{{attrs}}>{{content}}</ul>',
'item' => '<li{{attrs}}><a href="{{url}}"{{innerAttrs}}>{{title}}</a></li>{
˓→{separator}}',
You can easily customize them using the templates() method from the StringTemplateTrait:
$this->Breadcrumbs->templates([
'wrapper' => '<nav class="breadcrumbs"><ul{{attrs}}>{{content}}</ul></nav>',
]);
Since your templates will be rendered, the templateVars option allows you to add your own template variables in
the various templates:
$this->Breadcrumbs->templates([
'item' => '<li{{attrs}}>{{icon}}<a href="{{url}}"{{innerAttrs}}>{{title}}</a></li>
˓→{{separator}}'
]);
And to define the {{icon}} parameter, just specify it when adding the crumb to the trail:
$this->Breadcrumbs->add(
'Products',
['controller' => 'products', 'action' => 'index'],
[
'templateVars' => [
'icon' => '<i class="fa fa-money"></i>'
]
]
);
If you want to apply specific HTML attributes to both the item and its sub-item , you can leverage the innerAttrs
key, which the $options argument provides. Everything except innerAttrs and templateVars will be ren-
dered as HTML attributes:
$this->Breadcrumbs->add(
'Products',
['controller' => 'products', 'action' => 'index'],
[
'class' => 'products-crumb',
'data-foo' => 'bar',
'innerAttrs' => [
'class' => 'inner-products-crumb',
'id' => 'the-products-crumb'
]
]
);
// Based on the default template, this will render the following HTML :
<li class="products-crumb" data-foo="bar">
<a href="/products/index" class="inner-products-crumb" id="the-products-crumb">
˓→Products</a>
</li>
You can clear the bread crumbs using the reset() method. This can be useful when you want to transform the
crumbs and overwrite the list:
$crumbs = $this->Breadcrumbs->getCrumbs();
$crumbs = collection($crumbs)->map(function ($crumb) {
$crumb['options']['class'] = 'breadcrumb-item';
return $crumb;
})->toArray();
$this->Breadcrumbs->reset()->add($crumbs);
Flash
To render a flash message, you can simply use FlashHelper’s render() method in your template file:
By default, CakePHP uses a “flash” key for flash messages in a session. But, if you’ve specified a key when setting
the flash message in FlashComponent, you can specify which flash key to render:
You can also override any of the options that were set in FlashComponent:
// In your Controller
$this->Flash->set('The user has been saved.', [
'element' => 'success'
]);
Note: When building custom flash message templates, be sure to properly HTML encode any user data. CakePHP
won’t escape flash message parameters for you.
New in version 3.1: The FlashComponent now stacks messages. If you set multiple flash messages, when you call
render(), each message will be rendered in its own elements, in the order they were set.
For more information about the available array options, please refer to the FlashComponent section.
The FlashHelper uses normal elements to render the messages and will therefore obey any theme you might have
specified. So when your theme has a src/Template/Element/Flash/error.ctp file it will be used, just as with any
Elements and Views.
Form
Starting a Form
All parameters are optional. If create() is called with no parameters supplied, it assumes you are building a
form that submits to the current controller, via the current URL. The default method for form submission is POST. If
you were to call create() inside the view for UsersController::add(), you would see something like the
following output in the rendered view:
The $context argument is used as the form’s ‘context’. There are several built-in form contexts and you can add
your own, which we’ll cover below, in a following section. The built-in providers map to the following values of
$context:
• An Entity instance or an iterator will map to EntityContext125 ; this context class allows FormHelper to work
with results from the built-in ORM.
• An array containing the 'schema' key, will map to ArrayContext126 which allows you to create simple data
structures to build forms against.
• null and false will map to NullContext127 ; this context class simply satisfies the interface FormHelper
requires. This context is useful if you want to build a short form that doesn’t require ORM persistence.
All context classes also have access to the request data, making it simpler to build forms.
Once a form has been created with a context, all controls you create will use the active context. In the case of an ORM
backed form, FormHelper can access associated data, validation errors and schema metadata. You can close the active
context using the end() method, or by calling create() again.
To create a form for an entity, do the following:
Output:
This will POST the form data to the add() action of ArticlesController. However, you can also use the same logic to
create an edit form. The FormHelper uses the Entity object to automatically detect whether to create an add or edit
form. If the provided entity is not ‘new’, the form will be created as an edit form.
For example, if we browse to http://example.org/articles/edit/5, we could do the following:
// src/Controller/ArticlesController.php:
public function edit($id = null)
{
if (empty($id)) {
throw new NotFoundException;
}
$article = $this->Articles->get($id);
// Save logic goes here
$this->set('article', $article);
}
// View/Articles/edit.ctp:
// Since $article->isNew() is false, we will get an edit form
<?= $this->Form->create($article) ?>
125 https://api.cakephp.org/3.x/class-Cake.View.Form.EntityContext.html
126 https://api.cakephp.org/3.x/class-Cake.View.Form.ArrayContext.html
127 https://api.cakephp.org/3.x/class-Cake.View.Form.NullContext.html
Output:
Note: Since this is an edit form, a hidden input field is generated to override the default HTTP method.
In some cases, the entity’s ID is automatically appended to the end of the form’s action URL. If you would like to
avoid an ID being added to the URL, you can pass a string to $options['url'], such as '/my-account' or
\Cake\Routing\Router::url(['controller' => 'Users', 'action' => 'myAccount']).
The $options array is where most of the form configuration happens. This special array can contain a number of
different key-value pairs that affect the way the form tag is generated. Valid values:
• 'type' - Allows you to choose the type of form to create. If no type is provided then it will be autodetected
based on the form context. Valid values:
– 'get' - Will set the form method to HTTP GET.
– 'file' - Will set the form method to POST and the 'enctype' to “multipart/form-data”.
– 'post' - Will set the method to POST.
– 'put', 'delete', 'patch' - Will override the HTTP method with PUT, DELETE or PATCH
respectively, when the form is submitted.
• 'method' - Valid values are the same as above. Allows you to explicitly override the form’s method.
• 'url' - Specify the URL the form will submit to. Can be a string or a URL array.
• 'encoding' - Sets the accept-charset encoding for the form. Defaults to
Configure::read('App.encoding').
• 'enctype' - Allows you to set the form encoding explicitly.
• 'templates' - The templates you want to use for this form. Any templates provided will be merged on top
of the already loaded templates. Can be either a filename (without extension) from /config or an array of
templates to use.
• 'context' - Additional options for the form context class. (For example the EntityContext accepts a
'table' option that allows you to set the specific Table class the form should be based on.)
• 'idPrefix' - Prefix for generated ID attributes.
• 'templateVars' - Allows you to provide template variables for the formStart template.
• autoSetCustomValidity - Set to true to use custom required and notBlank validation messages in the
control’s HTML5 validity message. Default is false.
Tip: Besides the above options you can provide, in the $options argument, any valid HTML attributes that you
want to pass to the created form element.
// Same effect:
echo $this->Form
->setValueSources(['query', 'context'])
->create($articles);
// Same effect:
echo $this->Form->create($article, ['valueSources' => 'query']);
The supported sources are context, data and query. You can use one or more sources. Any widgets generated
by FormHelper will gather their values from the sources, in the order you setup.
The value sources will be reset to the default (['context']) when end() is called.
By using the type option you can change the HTTP method a form will use:
Output:
Specifying a 'file' value for type, changes the form submission method to ‘post’, and includes an enctype of
“multipart/form-data” on the form tag. This is to be used if there are any file elements inside the form. The absence of
the proper enctype attribute will cause the file uploads not to function.
E.g.
Output:
When using 'put', 'patch' or 'delete' as 'type' values, your form will be functionally equivalent to a
‘post’ form, but when submitted, the HTTP request method will be overridden with ‘PUT’, ‘PATCH’ or ‘DELETE’,
respectively. This allows CakePHP to emulate proper REST support in web browsers.
Using the 'url' option allows you to point the form to a specific action in your current controller or another controller
in your application.
For example, if you’d like to point the form to the publish() action of the current controller, you would supply an
$options array, like the following:
Output:
If the desired form action isn’t in the current controller, you can specify a complete URL for the form action. The
supplied URL can be relative to your CakePHP application:
echo $this->Form->create(null, [
'url' => [
'controller' => 'Articles',
'action' => 'publish'
]
]);
Output:
echo $this->Form->create(null, [
'url' => 'http://www.google.com/search',
'type' => 'get'
]);
Output:
Use 'url' => false if you don’t want to output a URL as the form action.
Often models will have multiple validation sets, and you will want FormHelper to mark fields required based on a the
specific validation rules your controller action is going to apply. For example, your Users table has specific validation
rules that only apply when an account is being registered:
echo $this->Form->create($user, [
'context' => ['validator' => 'register']
]);
The above will use the rules defined in the register validator, which are defined by
UsersTable::validationRegister(), for $user and all related associations. If you are creating a
form for associated entities, you can define validation rules for each association by using an array:
echo $this->Form->create($user, [
'context' => [
'validator' => [
'Users' => 'register',
'Comments' => 'default'
]
]
]);
The above would use register for the user, and default for the user’s comments.
While the built-in context classes are intended to cover the basic cases you’ll encounter you may need to
build a new context class if you are using a different ORM. In these situations you need to implement the
Cake\View\Form\ContextInterface128 . Once you have implemented this interface you can wire your new context
into the FormHelper. It is often best to do this in a View.beforeRender event listener, or in an application view
class:
Context factory functions are where you can add logic for checking the form options for the correct type of entity. If
matching input data is found you can return an object. If there is no match return null.
Tip: Please note that while the fields generated by the control() method are called generically “inputs” on this
page, technically speaking, the control() method can generate not only all of the HTML input type elements,
but also other HTML form elements (e.g. select, button, textarea).
By default the control() method will employ the following widget templates:
128 https://api.cakephp.org/3.x/class-Cake.View.Form.ContextInterface.html
The type of control created (when we provide no additional options to specify the generated element type) is inferred
via model introspection and depends on the column datatype:
Column Type Resulting Form Field
string, uuid (char, varchar, etc.) text
boolean, tinyint(1) checkbox
decimal number
float number
integer number
text textarea
text, with name of password, passwd password
text, with name of email email
text, with name of tel, telephone, or phone tel
date day, month, and year selects
datetime, timestamp day, month, year, hour, minute, and meridian selects
time hour, minute, and meridian selects
binary file
The $options parameter allows you to choose a specific control type if you need to:
Tip: As a small subtlety, generating specific elements via the control() form method will always also generate
the wrapping div, by default. Generating the same type of element via one of the specific form methods (e.g.
$this->Form->checkbox('published');) in most cases won’t generate the wrapping div. Depending on
your needs you can use one or another.
The wrapping div will have a required class name appended if the validation rules for the model’s field in-
dicate that it is required and not allowed to be empty. You can disable automatic required flagging using the
'required' option:
To skip browser validation triggering for the whole form you can set option 'formnovalidate' => true for the
input button you generate using View\Helper\FormHelper::submit() or set 'novalidate' => true
in options for View\Helper\FormHelper::create().
For example, let’s assume that your Users model includes fields for a username (varchar), password (varchar), ap-
proved (datetime) and quote (text). You can use the control() method of the FormHelper to create appropriate
controls for all of these form fields:
echo $this->Form->create($user);
// The following generates a Text input
echo $this->Form->control('username');
// The following generates a Password input
echo $this->Form->control('password');
// Assuming 'approved' is a datetime or timestamp field the following
//generates: Day, Month, Year, Hour, Minute
echo $this->Form->control('approved');
// The following generates a Textarea element
echo $this->Form->control('quote');
echo $this->Form->button('Add');
echo $this->Form->end();
echo $this->Form->control('birth_dt', [
'label' => 'Date of birth',
'minYear' => date('Y') - 70,
'maxYear' => date('Y') - 18,
]);
Besides the specific Options for Control, you also can specify any option accepted by corresponding specific method
for the chosen (or inferred by CakePHP) control type and any HTML attribute (for instance onfocus).
If you want to create a select form field while using a belongsTo (or hasOne) relation, you can add the following to
your UsersController (assuming your User belongsTo Group):
$this->set('groups', $this->Users->Groups->find('list'));
To make a select box for a belongsToMany Groups association you can add the following to your UsersController:
$this->set('groups', $this->Users->Groups->find('list'));
If your model name consists of two or more words (e.g. “UserGroups”), when passing the data using set() you
should name your data in a pluralised and lower camelCased129 format as follows:
$this->set('userGroups', $this->UserGroups->find('list'));
Note: You should not use FormHelper::control() to generate submit buttons. Use
View\Helper\FormHelper::submit() instead.
129 https://en.wikipedia.org/wiki/Camel_case#Variations_and_synonyms
When creating control widgets you should name your fields after the matching attributes in the form’s entity. For
example, if you created a form for an $article entity, you would create fields named after the properties. E.g.
title, body and published.
You can create controls for associated models, or arbitrary models by passing in association.fieldname as the
first parameter:
echo $this->Form->control('association.fieldname');
Any dots in your field names will be converted into nested request data. For example, if you created a field with a
name 0.comments.body you would get a name attribute that looks like 0[comments][body]. This convention
makes it easy to save data with the ORM. Details for the various association types can be found in the Creating Inputs
for Associated Data section.
When creating datetime related controls, FormHelper will append a field-suffix. You may notice additional fields
named year, month, day, hour, minute, or meridian being added. These fields will be automatically con-
verted into DateTime objects when entities are marshalled.
FormHelper::control() supports a large number of options via its $options argument. In addition
to its own options, control() accepts options for the inferred/chosen generated control types (e.g. for
checkbox or textarea), as well as HTML attributes. This subsection will cover the options specific to
FormHelper::control().
• $options['type'] - A string that specifies the widget type to be generated. In addition to the field types
found in the Creating Form Controls, you can also create 'file', 'password', and any other type supported
by HTML5. By specifying a 'type' you will force the type of the generated control, overriding model
introspection. Defaults to null.
E.g.
echo $this->Form->control('field', ['type' => 'file']);
echo $this->Form->control('email', ['type' => 'email']);
Output:
<div class="input file">
<label for="field">Field</label>
<input type="file" name="field" value="" id="field" />
</div>
<div class="input email">
<label for="email">Email</label>
<input type="email" name="email" value="" id="email" />
</div>
• $options['label'] - Either a string caption or an array of options for the label. You can set this key to
the string you would like to be displayed within the label that usually accompanies the input HTML element.
Defaults to null.
E.g.
echo $this->Form->control('name', [
'label' => 'The User Alias'
]);
Output:
<div class="input">
<label for="name">The User Alias</label>
<input name="name" type="text" value="" id="name" />
</div>
Alternatively, set this key to false to disable the generation of the label element.
E.g.
Output:
<div class="input">
<input name="name" type="text" value="" id="name" />
</div>
Set this to an array to provide additional options for the label element. If you do this, you can use a 'text'
key in the array to customize the label text.
E.g.
echo $this->Form->control('name', [
'label' => [
'class' => 'thingy',
'text' => 'The User Alias'
]
]);
Output:
<div class="input">
<label for="name" class="thingy">The User Alias</label>
<input name="name" type="text" value="" id="name" />
</div>
• $options['options'] - You can provide in here an array containing the elements to be generated for
widgets such as radio or select, which require an array of items as an argument (see Creating Radio
Buttons and Creating Select Pickers for more details). Defaults to null.
• $options['error'] - Using this key allows you to override the default model error messages and can be
used, for example, to set i18n messages. To disable the error message output & field classes set the 'error'
key to false. Defaults to null.
E.g.
To override the model error messages use an array with the keys matching the original validation error messages.
E.g.
$this->Form->control('name', [
'error' => ['Not long enough' => __('This is not long enough')]
]);
As seen above you can set the error message for each validation rule you have in your models. In addition you
can provide i18n messages for your forms.
• $options['nestedInput'] - Used with checkboxes and radio buttons. Controls whether the input ele-
ment is generated inside or outside the label element. When control() generates a checkbox or a radio
button, you can set this to false to force the generation of the HTML input element outside of the label
element.
On the other hand you can set this to true for any control type to force the generated input element inside the
label. If you change this for radio buttons then you need to also modify the default radioWrapper template.
Depending on the generated control type it defaults to true or false.
• $options['templates'] - The templates you want to use for this input. Any specified templates will
be merged on top of the already loaded templates. This option can be either a filename (without extension) in
/config that contains the templates you want to load, or an array of templates to use.
• $options['labelOptions'] - Set this to false to disable labels around nestedWidgets or set it to an
array of attributes to be provided to the label tag.
In addition to the generic control() method, FormHelper has specific methods for generating a number of
different types of controls. These can be used to generate just the control widget itself, and combined with other meth-
ods like View\Helper\FormHelper::label() and View\Helper\FormHelper::error() to generate
fully custom form layouts.
Many of the various control element methods support a common set of options which, depending on the form method
used, must be provided inside the $options or in the $attributes array argument. All of these options are also
supported by the control() method. To reduce repetition, the common options shared by all control methods are
as follows:
• 'id' - Set this key to force the value of the DOM id for the control. This will override the 'idPrefix' that
may be set.
• 'default' - Used to set a default value for the control field. The value is used if the data passed to the form
does not contain a value for the field (or if no data is passed at all). An explicit default value will override any
default values defined in the schema.
Example usage:
$sizes = ['s' => 'Small', 'm' => 'Medium', 'l' => 'Large'];
echo $this->Form->select('size', $sizes, ['default' => 'm']);
Note: You cannot use default to check a checkbox - instead you might set the value in
$this->request->getData() in your controller, or set the control option 'checked' to true.
Beware of using false to assign a default value. A false value is used to disable/exclude options of a control
field, so 'default' => false would not set any value at all. Instead use 'default' => 0.
• 'value' - Used to set a specific value for the control field. This will override any value that may else be
injected from the context, such as Form, Entity or request->getData() etc.
Note: If you want to set a field to not render its value fetched from context or valuesSource you will need to
set 'value' to '' (instead of setting it to null).
In addition to the above options, you can mixin any HTML attribute you wish to use. Any non-special option name
will be treated as an HTML attribute, and applied to the generated HTML control element.
Changed in version 3.3.0: As of 3.3.0, FormHelper will automatically use any default values defined in your database
schema. You can disable this behavior by setting the schemaDefault option to false.
The rest of the methods available in the FormHelper are for creating specific form elements. Many of these methods
also make use of a special $options or $attributes parameter. In this case, however, this parameter is used
primarily to specify HTML tag attributes (such as the value or DOM id of an element in the form).
Will output:
echo $this->Form->password('password');
Will output:
echo $this->Form->hidden('id');
Will output:
Creating Textareas
For example:
echo $this->Form->textarea('notes');
Will output:
<textarea name="notes"></textarea>
If the form is being edited (i.e. the array $this->request->getData() contains the information previously
saved for the User entity), the value corresponding to notes field will automatically be added to the HTML gener-
ated.
Example:
• 'rows', 'cols' - You can use these two keys to set the HTML attributes which specify the number of rows
and columns for the textarea field.
E.g.
echo $this->Form->textarea('comment', ['rows' => '5', 'cols' => '5']);
Output:
<textarea name="comment" cols="5" rows="5">
</textarea>
These controls share some commonalities and a few options and thus, they are all grouped in this subsection for easier
reference.
You can find below the options which are shared by select(), checkbox() and radio() (the options particular
only to one of the methods are described in each method’s own section.)
• 'value' - Sets or selects the value of the affected element(s):
– For checkboxes, it sets the HTML 'value' attribute assigned to the input element to whatever you
provide as value.
– For radio buttons or select pickers it defines which element will be selected when the form is rendered (in
this case 'value' must be assigned a valid, existent element value). May also be used in combination
with any select-type control, such as date(), time(), dateTime():
echo $this->Form->time('close_time', [
'value' => '13:30:00'
]);
Note: The 'value' key for date() and dateTime() controls may also have as value a UNIX timestamp,
or a DateTime object.
For a select control where you set the 'multiple' attribute to true, you can provide an array with the
values you want to select by default:
// HTML <option> elements with values 1 and 3 will be rendered preselected
echo $this->Form->select(
'rooms',
[1, 2, 3, 4, 5],
[
'multiple' => true,
'value' => [1, 3]
]
);
echo $this->Form->select(
'field',
[1, 2, 3, 4, 5],
['empty' => '(choose one)']
);
Output:
<select name="field">
<option value="">(choose one)</option>
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
</select>
• 'hiddenField' - For checkboxes and radio buttons, by default, a hidden input element is also created,
along with the main element, so that the key in $this->request->getData() will exist even without a
value specified. For checkboxes its value defaults to 0 and for radio buttons to ''.
Example of default output:
Which outputs:
If you want to create multiple blocks of controls on a form, that are all grouped together, you should set this
parameter to false on all controls except the first. If the hidden input is on the page in multiple places, only
the last group of inputs’ values will be saved.
In this example, only the tertiary colors would be passed, and the primary colors would be overridden:
<h2>Primary Colors</h2>
<input type="hidden" name="color" value="0" />
<label for="color-red">
<input type="checkbox" name="color[]" value="5" id="color-red" />
Red
</label>
<label for="color-blue">
<input type="checkbox" name="color[]" value="5" id="color-blue" />
Blue
</label>
<label for="color-yellow">
<input type="checkbox" name="color[]" value="5" id="color-yellow" />
Yellow
</label>
<h2>Tertiary Colors</h2>
<input type="hidden" name="color" value="0" />
<label for="color-green">
<input type="checkbox" name="color[]" value="5" id="color-green" />
Green
</label>
<label for="color-purple">
<input type="checkbox" name="color[]" value="5" id="color-purple" />
Purple
</label>
<label for="color-orange">
<input type="checkbox" name="color[]" value="5" id="color-orange" />
Orange
</label>
Disabling 'hiddenField' on the second control group would prevent this behavior.
You can set a hidden field to a value other than 0, such as ‘N’:
echo $this->Form->checkbox('published', [
'value' => 'Y',
'hiddenField' => 'N',
]);
It’s possible to use the Collection class to build your options array. This approach is ideal if you already have a
collection of entities and would like to build a select element from them.
You can use the combine method to build a basic options array.:
It’s also possible to add extra attributes by expanding the array. The following will create a data attribute on the option
element, using the map collection method.:
Creating Checkboxes
echo $this->Form->checkbox('done');
Will output:
It is possible to specify the value of the checkbox by using the $options array.
E.g.
Will output:
If you don’t want the FormHelper to create a hidden input use 'hiddenField'.
E.g.
Will output:
• $options - An optional array containing at minimum the labels for the radio buttons. Can also contain values
and HTML attributes. When this array is missing, the method will either generate only the hidden input (if
'hiddenField' is true) or no element at all (if 'hiddenField' is false).
• $attributes - An optional array including any of the Common Options For Specific Controls, or of the
Options for Select, Checkbox and Radio Controls, of the radio button specific attributes (see below), as well as
any valid HTML attributes.
Creates a set of radio button inputs. The default widget templates used are:
Will output:
Generally $options contains simple key => value pairs. However, if you need to put custom attributes on your
radio buttons you can use an expanded format.
E.g.
echo $this->Form->radio(
'favorite_color',
[
['value' => 'r', 'text' => 'Red', 'style' => 'color:red;'],
['value' => 'u', 'text' => 'Blue', 'style' => 'color:blue;'],
['value' => 'g', 'text' => 'Green', 'style' => 'color:green;'],
]
);
Will output:
Red
</label>
<label for="favorite-color-u">
<input type="radio" name="favorite_color" value="u" style="color:blue;" id=
˓→"favorite-color-u">
Blue
</label>
<label for="favorite-color-g">
<input type="radio" name="favorite_color" value="g" style="color:green;" id=
˓→"favorite-color-g">
Green
</label>
You can define additional attributes for an individual option’s label as well:
echo $this->Form->radio(
'favorite_color',
[
['value' => 'r', 'text' => 'Red', 'label' => ['class' => 'red']],
['value' => 'u', 'text' => 'Blue', 'label' => ['class' => 'blue']],
]
);
Will output:
Red
</label>
<label for="favorite-color-u" class="blue">
<input type="radio" name="favorite_color" value="u" style="color:blue;" id=
˓→"favorite-color-u">
Blue
</label>
If the label key is used on an option, the attributes in $attributes['label'] will be ignored.
Changed in version 3.8.0: The label key in complex options was added.
• $attributes - An optional array including any of the Common Options For Specific Controls, or of the
Options for Select, Checkbox and Radio Controls, or of the select-specific attributes (see below), as well as any
valid HTML attributes.
Creates a select element, populated with the items from the $options array. If $attributes['value']
is provided, then the HTML option element(s) which have the specified value(s) will be shown as selected when
rendering the select picker.
By default select uses the following widget templates:
Output:
<select name="field">
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
</select>
echo $this->Form->select('field', [
'Value 1' => 'Label 1',
'Value 2' => 'Label 2',
'Value 3' => 'Label 3'
]);
Output:
<select name="field">
<option value="Value 1">Label 1</option>
<option value="Value 2">Label 2</option>
<option value="Value 3">Label 3</option>
</select>
If you would like to generate a select with optgroups, just pass data in hierarchical format (nested array). This
works on multiple checkboxes and radio buttons too, but instead of optgroup it wraps the elements in fieldset
elements.
For example:
$options = [
'Group 1' => [
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
],
'Group 2' => [
'Value 3' => 'Label 3'
]
];
echo $this->Form->select('field', $options);
Output:
<select name="field">
<optgroup label="Group 1">
<option value="Value 1">Label 1</option>
<option value="Value 2">Label 2</option>
</optgroup>
<optgroup label="Group 2">
<option value="Value 3">Label 3</option>
</optgroup>
</select>
$options = [
['text' => 'Description 1', 'value' => 'value 1', 'attr_name' => 'attr_value 1'],
['text' => 'Description 2', 'value' => 'value 2', 'attr_name' => 'attr_value 2'],
['text' => 'Description 3', 'value' => 'value 3', 'other_attr_name' => 'other_
˓→attr_value'],
];
echo $this->Form->select('field', $options);
Output:
<select name="field">
<option value="value 1" attr_name="attr_value 1">Description 1</option>
<option value="value 2" attr_name="attr_value 2">Description 2</option>
<option value="value 3" other_attr_name="other_attr_value">Description 3</option>
</select>
• 'empty' - Set the 'empty' key in the $attributes argument to true (the default value is false) to
add a blank option with an empty value at the top of your dropdown list.
For example:
$options = ['M' => 'Male', 'F' => 'Female'];
echo $this->Form->select('gender', $options, ['empty' => true]);
Will output:
<select name="gender">
<option value=""></option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
• 'escape' - The select() method allows for an attribute called 'escape' which accepts a boolean value
and determines whether to HTML entity encode the contents of the select’s option elements.
E.g.
// This will prevent HTML-encoding the contents of each option element
$options = ['M' => 'Male', 'F' => 'Female'];
echo $this->Form->select('gender', $options, ['escape' => false]);
• 'multiple' - If set to true, the select picker will allow multiple selections.
E.g.
echo $this->Form->select('field', $options, ['multiple' => true]);
Output:
<input name="field" value="" type="hidden">
<div class="checkbox">
<label for="field-1">
<input name="field[]" value="Value 1" id="field-1" type="checkbox">
Label 1
</label>
</div>
<div class="checkbox">
<label for="field-2">
<input name="field[]" value="Value 2" id="field-2" type="checkbox">
Label 2
</label>
</div>
• 'disabled' - This option can be set in order to disable all or some of the select’s option items. To
disable all items set 'disabled' to true. To disable only certain items, assign to 'disabled' an array
containing the keys of the items to be disabled.
E.g.
$options = [
'M' => 'Masculine',
'F' => 'Feminine',
'N' => 'Neuter'
];
echo $this->Form->select('gender', $options, [
'disabled' => ['M', 'N']
]);
Will output:
<select name="gender">
<option value="M" disabled="disabled">Masculine</option>
<option value="F">Feminine</option>
<option value="N" disabled="disabled">Neuter</option>
</select>
$options = [
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
];
echo $this->Form->select('field', $options, [
'multiple' => 'checkbox',
'disabled' => ['Value 1']
]);
Output:
To add a file upload field to a form, you must first make sure that the form enctype is set to 'multipart/
form-data'.
So start off with a create() method such as the following:
Next add a line that looks like either of the following two lines to your form’s view template file:
echo $this->Form->control('submittedfile', [
'type' => 'file'
]);
// OR
echo $this->Form->file('submittedfile');
Note: Due to the limitations of HTML itself, it is not possible to put default values into input fields of type ‘file’.
Each time the form is displayed, the value inside will be empty.
Upon submission, file fields provide an expanded data array to the script receiving the form data.
For the example above, the values in the submitted data array would be organized as follows, if CakePHP was installed
on a Windows server (the key 'tmp_name' will contain a different path in a Unix environment):
$this->request->data['submittedfile']
This array is generated by PHP itself, so for more detail on the way PHP handles data passed via file fields read the
PHP manual section on file uploads130 .
Note: When using $this->Form->file(), remember to set the form encoding-type, by setting the 'type'
option to 'file' in $this->Form->create().
The date and time related methods share a number of common traits and options and hence are grouped together into
this subsection.
130 http://php.net/features.file-upload
These options are common for the date and time related controls:
• 'empty' - If true an extra, empty, option HTML element is added inside select at the top of the list. If
a string, that string is displayed as the empty element. Defaults to true.
• 'default' | value - Use either of the two to set the default value to be shown by the field. A value in
$this->request->getData() matching the field name will override this value. If no default is provided
time() will be used.
• 'year', 'month', 'day', 'hour', 'minute', 'second', 'meridian' - These options
allow you to control which control elements are generated or not. By setting any of these options to false
you can disable the generation of that specific that select picker (if by default it would be rendered in the used
method). In addition each option allows you to pass HTML attributes to that specific select element.
These options are concerning the date-related methods - i.e. year(), month(), day(), dateTime() and
date():
• 'monthNames' - If false, 2 digit numbers will be used instead of text for displaying months in the select
picker. If set to an array (e.g. ['01' => 'Jan', '02' => 'Feb', ...]), the given array will be
used.
• 'minYear' - The lowest value to use in the year select picker.
• 'maxYear' - The maximum value to use in the year select picker.
• 'orderYear' - The order of year values in the year select picker. Possible values are 'asc' and 'desc'.
Defaults to 'desc'.
• 'year', 'month', 'day' - These options allow you to control which control elements are generated or
not. By setting any of these options to false you can disable the generation of that specific that select picker
(if by default it would be rendered in the used method). In addition each option allows you to pass HTML
attributes to that specific select element.
These options are concerning the time-related methods - hour(), minute(), second(), dateTime() and
time():
• 'interval' - The interval in minutes between the values which are displayed in the option elements of the
minutes select picker. Defaults to 1.
• 'round' - Set to up or down if you want to force rounding minutes in either direction when the value doesn’t
fit neatly into an interval. Defaults to null.
• timeFormat - Applies to dateTime() and time(). The time format to use in the select picker; either
12 or 24. When this option is set to anything else than 24 the format will be automatically set to 12 and the
meridian select picker will be displayed automatically to the right of the seconds select picker. Defaults to
24.
• format - Applies to hour(). The time format to use; either 12 or 24. In case it’s set to 12 the meridian
select picker won’t be automatically displayed. It’s up to you to either add it or provide means to infer from the
form context the right period of the day. Defaults to 24.
• second - Applies to dateTime() and time(). Set to true to enable the seconds drop down. Defaults to
false.
• 'hour', 'minute', 'second', 'meridian' - These options allow you to control which control
elements are generated or not. By setting any of these options to false you can disable the generation of that
specific that select picker (if by default it would be rendered in the used method). In addition each option allows
you to pass HTML attributes to that specific select element.
Calling the method without additional options will generate, by default, 5 select pickers, for: year (4 digits), month
(full English name), day (num), hour (num), minutes (num).
For example
<?= $this->form->dateTime('registered') ?>
Output:
<select name="registered[year]">
<option value="" selected="selected"></option>
<option value="2022">2022</option>
...
<option value="2012">2012</option>
</select>
<select name="registered[month]">
<option value="" selected="selected"></option>
<option value="01">January</option>
...
<option value="12">December</option>
</select>
<select name="registered[day]">
<option value="" selected="selected"></option>
<option value="01">1</option>
...
<option value="31">31</option>
</select>
<select name="registered[hour]">
<option value="" selected="selected"></option>
<option value="00">0</option>
...
<option value="23">23</option>
</select>
<select name="registered[minute]">
<option value="" selected="selected"></option>
<option value="00">00</option>
...
<option value="59">59</option>
</select>
To create datetime controls with custom classes/attributes on a specific select box, you can provide them as arrays of
options for each component, within the $options argument.
For example:
echo $this->Form->dateTime('released', [
'year' => [
'class' => 'year-classname',
],
'month' => [
'class' => 'month-class',
'data-type' => 'month',
],
]);
Output:
echo $this->Form->time('released', [
'interval' => 15,
'hour' => [
'class' => 'foo-class',
],
'minute' => [
'class' => 'bar-class',
],
]);
echo $this->Form->year('purchased', [
'minYear' => 2000,
'maxYear' => date('Y')
]);
<select name="purchased[year]">
<option value=""></option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
</select>
echo $this->Form->month('mob');
Will output:
<select name="mob[month]">
<option value=""></option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
You can pass in, your own array of months to be used by setting the 'monthNames' attribute, or have months
displayed as numbers by passing false.
E.g.
Note: The default months can be localized with CakePHP Internationalization & Localization features.
For example:
Will output:
<select name="created[day]">
<option value="" selected="selected">Day</option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
...
<option value="31">31</option>
</select>
echo $this->Form->hour('created', [
'format' => 12
]);
echo $this->Form->hour('created', [
'format' => 24
]);
<select name="arrival[minute]">
<option value="" selected="selected"></option>
<option value="00">00</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
<option value="50">50</option>
</select>