Cakephp Cookbook Documentation Release 2.X
Total Page:16
File Type:pdf, Size:1020Kb
CakePHP Cookbook Documentation Release 2.x Cake Software Foundation Sep 18, 2021 Contents 1 Getting Started 1 Blog Tutorial..................................................1 Blog Tutorial - Adding a layer.........................................9 2 Installation 27 Requirements.................................................. 27 License..................................................... 28 Downloading CakePHP............................................. 28 Permissions................................................... 28 Setup...................................................... 29 Development.................................................. 29 Production.................................................... 30 Advanced Installation and URL Rewriting................................... 30 Fire It Up.................................................... 34 3 CakePHP Overview 35 What is CakePHP? Why use it?......................................... 35 Understanding Model-View-Controller..................................... 36 Where to Get Help............................................... 38 4 Controllers 41 The App Controller............................................... 41 Request parameters............................................... 42 Controller actions................................................ 42 Request Life-cycle callbacks.......................................... 43 Controller Methods............................................... 44 Controller Attributes.............................................. 51 More on controllers............................................... 53 5 Views 73 View Templates................................................. 73 Using view blocks................................................ 75 Layouts..................................................... 77 Elements..................................................... 79 i Creating your own view classes......................................... 82 View API.................................................... 83 More about Views................................................ 85 6 Models 187 Understanding Models............................................. 187 More on models................................................. 189 7 Core Libraries 317 General Purpose................................................. 317 Behaviors.................................................... 340 Components................................................... 340 Helpers..................................................... 391 Utilities..................................................... 391 8 Plugins 501 How To Install Plugins............................................. 501 How To Use Plugins.............................................. 503 How To Create Plugins............................................. 505 9 Shells, Tasks & Console Tools 511 The CakePHP console.............................................. 511 Creating a shell................................................. 513 Shell tasks.................................................... 514 Invoking other shells from your shell...................................... 515 Console output levels.............................................. 516 Styling output.................................................. 516 Configuring options and generating help.................................... 517 Routing in shells / CLI............................................. 524 Shell API.................................................... 524 More topics................................................... 527 10 Development 545 Configuration.................................................. 545 Routing..................................................... 558 Sessions..................................................... 574 Exceptions.................................................... 580 Error Handling................................................. 586 Debugging.................................................... 588 Testing...................................................... 592 REST...................................................... 616 Dispatcher Filters................................................ 619 11 Deployment 625 Check your security............................................... 625 Set document root................................................ 625 Update core.php................................................. 626 Improve your application’s performance.................................... 626 12 Simple Authentication and Authorization Application 627 Creating all users’ related code......................................... 627 Authentication (login and logout)........................................ 630 Authorization (who’s allowed to access what)................................. 632 13 Simple Acl controlled Application 635 ii Preparing our Application............................................ 636 Preparing to Add Auth............................................. 637 Initialize the Db Acl tables........................................... 639 Acts As a Requester............................................... 639 Creating ACOs (Access Control Objects)................................... 641 14 Simple Acl controlled Application - part 2 643 An Automated tool for creating ACOs..................................... 643 Setting up permissions............................................. 644 Logging in.................................................... 645 Logout...................................................... 646 All done..................................................... 646 15 Contributing 647 Documentation................................................. 647 Tickets...................................................... 655 Code....................................................... 656 Coding Standards................................................ 658 Backwards Compatibility Guide........................................ 669 CakePHP Development Process........................................ 671 16 Appendices 673 2.10 Migration Guide.............................................. 673 2.9 Migration Guide............................................... 674 2.8 Migration Guide............................................... 675 2.7 Migration Guide............................................... 677 2.6 Migration Guide............................................... 679 2.5 Migration Guide............................................... 682 2.4 Migration Guide............................................... 687 2.3 Migration Guide............................................... 693 2.2 Migration Guide............................................... 699 2.1 Migration Guide............................................... 705 2.0 Migration Guide............................................... 716 Migration from 1.2 to 1.3............................................ 747 General Information............................................... 765 17 Indices and tables 767 Index 769 iii iv CHAPTER1 Getting Started The CakePHP framework provides a robust base for your application. It can handle every aspect, from the user’s initial request all the way to the final rendering of a web page. And since the framework follows the principles of MVC, it allows you to easily customize and extend most aspects of your application. The framework also provides a basic organizational structure, from filenames to database table names, keeping your entire application consistent and logical. This concept is simple but powerful. Follow the conventions and you’ll always know exactly where things are and how they’re organized. The best way to experience and learn CakePHP is to sit down and build something. To start off we’ll build a simple blog application. Blog Tutorial Welcome to CakePHP. You’re probably checking out this tutorial because you want to learn more about how CakePHP works. It’s our aim to increase productivity and make coding more enjoyable: we hope you’ll see this as you dive into the code. This tutorial will walk you through the creation of a simple blog application. We’ll be getting and installing CakePHP, creating and configuring a database, and creating enough application logic to list, add, edit, and delete blog posts. 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.2.8 or greater. 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. The more object-oriented programming you’ve done, the better: but fear not if you’re a procedural fan. 1 CakePHP Cookbook Documentation, Release 2.x 4. Finally, you’ll need a basic knowledge of the MVC programming pattern. A quick overview can be found in Understanding Model-View-Controller. Don’t worry, it’s only half a page or so. Let’s get started! Getting CakePHP First, let’s get a copy of fresh CakePHP code. To get a fresh download, visit the CakePHP project on GitHub: https://github.com/cakephp/cakephp/tags and down- load the latest release of 2.0 You can also clone the repository using git4: git clone -b 2.x git://github.com/cakephp/cakephp.git Regardless of how