Cakephp Cookbook Documentation Release 4.X
Total Page:16
File Type:pdf, Size:1020Kb
CakePHP Cookbook Documentation Release 4.x Cake Software Foundation Sep 25, 2021 Contents 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 2 Quick Start Guide 13 Content Management Tutorial......................................... 13 CMS Tutorial - Creating the Database..................................... 15 CMS Tutorial - Creating the Articles Controller................................ 19 3 4.0 Migration Guide 29 Deprecated Features Removed......................................... 29 Deprecations.................................................. 29 Breaking Changes................................................ 31 New Features.................................................. 37 4 Tutorials & Examples 41 Content Management Tutorial......................................... 41 CMS Tutorial - Creating the Database..................................... 43 CMS Tutorial - Creating the Articles Controller................................ 47 CMS Tutorial - Tags and Users......................................... 56 CMS Tutorial - Authentication......................................... 64 CMS Tutorial - Authorization.......................................... 69 Bookmarker Tutorial.............................................. 73 Bookmarker Tutorial Part 2........................................... 81 Blog Tutorial.................................................. 88 Blog Tutorial - Part 2.............................................. 91 Blog Tutorial - Part 3.............................................. 102 Blog Tutorial - Authentication......................................... 109 i 5 Contributing 117 Documentation................................................. 117 Tickets...................................................... 125 Code....................................................... 126 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 155 Configuring your Application.......................................... 155 Environment Variables............................................. 156 Additional Class Paths............................................. 159 Inflection Configuration............................................. 160 Configure Class................................................. 160 Reading and writing configuration files..................................... 162 Disabling Generic Tables............................................ 164 8 Routing 167 Quick Tour................................................... 167 Connecting Routes............................................... 169 Connecting Scoped Middleware........................................ 182 RESTful Routing................................................ 183 Passed Arguments................................................ 187 Generating URLs................................................ 188 Generating Asset URLs............................................. 190 Redirect Routing................................................ 191 Entity Routing.................................................. 192 Custom Route Classes............................................. 192 Creating Persistent URL Parameters...................................... 194 9 Request & Response Objects 197 Request..................................................... 197 Response.................................................... 208 Setting Cookies................................................. 214 Setting Cross Origin Request Headers (CORS)................................. 215 Common Mistakes with Immutable Responses................................. 215 Cookie Collections............................................... 215 10 Controllers 219 The App Controller............................................... 220 Request Flow.................................................. 220 Controller Actions................................................ 220 Interacting with Views............................................. 221 Redirecting to Other Pages........................................... 223 Loading Additional Models........................................... 224 Paginating a Model............................................... 225 Configuring Components to Load........................................ 225 Request Life-cycle Callbacks.......................................... 226 ii More on Controllers............................................... 227 11 Views 267 The App View.................................................. 267 View Templates................................................. 268 Using View Blocks............................................... 271 Layouts..................................................... 273 Elements..................................................... 275 View Events................................................... 278 Creating Your Own View Classes........................................ 278 More About Views............................................... 279 12 Database Access & ORM 375 Quick Example................................................. 375 More Information................................................ 377 13 Caching 543 Configuring Cache Engines........................................... 544 Writing to a Cache............................................... 547 Reading From a Cache............................................. 548 Deleting From a Cache............................................. 549 Clearing Cached Data.............................................. 550 Using Cache to Store Counters......................................... 550 Using Cache to Store Common Query Results................................. 551 Using Groups.................................................. 551 Globally Enable or Disable Cache....................................... 552 Creating a Cache Engine............................................ 552 14 Bake Console 555 15 Console Commands 557 The CakePHP Console............................................. 557 Console Applications.............................................. 558 Renaming Commands.............................................. 559 Commands................................................... 559 CakePHP Provided Commands......................................... 581 Routing in the Console Environment...................................... 595 16 Debugging 597 Basic Debugging................................................ 597 Using the Debugger Class............................................ 598 Outputting Values................................................ 598 Logging With Stack Traces........................................... 599 Generating Stack Traces............................................ 599 Getting an Excerpt From a File......................................... 599 Editor Integration................................................ 600 Using Logging to Debug............................................ 600 Debug Kit.................................................... 601 17 Deployment 603 Moving files................................................... 603 Adjust config/app.php.............................................. 603 Check Your Security.............................................. 604 Set Document Root............................................... 604 Improve Your Application’s Performance................................... 604 iii Deploying an update.............................................. 605 18 Mailer 607 Basic Usage................................................... 607 Configuration.................................................. 608 Setting Headers................................................. 609 Sending Templated Emails........................................... 609 Sending Attachments.............................................. 611 Sending Messages Quickly........................................... 612 Sending Emails from CLI............................................ 612 Creating Reusable Emails............................................ 613 Configuring Transports............................................. 614 Sending emails without using Mailer...................................... 616 Testing Mailers................................................. 617 19 Error & Exception Handling 619 Error & Exception Configuration........................................ 619 Changing Exception Handling......................................... 620 Custom Error Templates............................................ 621 Custom ErrorController............................................. 621 Custom ExceptionRenderer........................................... 622 Creating your Own Error Handler......................................