The Definitive Guide to Yii
Total Page:16
File Type:pdf, Size:1020Kb
The Definitive Guide to Yii 1.0 Qiang Xue and Xiang Wei Zhuo Copyright 2008-2009. All Rights Reserved. Contents Contents i License ix 1 Getting Started1 1.1 The Definitive Guide to Yii...........................1 1.2 New Features...................................1 1.2.1 Version 1.0.7...............................1 1.2.2 Version 1.0.6...............................1 1.2.3 Version 1.0.5...............................2 1.3 What is Yii....................................2 1.3.1 Requirements...............................3 1.3.2 What is Yii Best for?..........................3 1.3.3 How is Yii Compared with Other Frameworks?............3 1.4 Installation....................................3 1.4.1 Requirements...............................4 1.5 Creating First Yii Application..........................4 1.5.1 Connecting to Database.........................8 1.5.2 Implementing CRUD Operations....................9 2 Fundamentals 13 2.1 Model-View-Controller (MVC)......................... 13 ii Contents 2.1.1 A Typical Workflow........................... 14 2.2 Entry Script.................................... 15 2.2.1 Debug Mode............................... 15 2.3 Application.................................... 16 2.3.1 Application Configuration........................ 16 2.3.2 Application Base Directory....................... 17 2.3.3 Application Component......................... 17 2.3.4 Core Application Components..................... 18 2.3.5 Application Lifecycles.......................... 19 2.4 Controller..................................... 20 2.4.1 Route................................... 20 2.4.2 Controller Instantiation......................... 21 2.4.3 Action................................... 21 2.4.4 Filter................................... 22 2.5 Model....................................... 24 2.6 View........................................ 25 2.6.1 Layout................................... 25 2.6.2 Widget.................................. 26 2.6.3 System View............................... 27 2.7 Component.................................... 27 2.7.1 Component Property........................... 27 2.7.2 Component Event............................ 28 2.7.3 Component Behavior........................... 29 2.8 Module...................................... 30 Contents iii 2.8.1 Creating Module............................. 31 2.8.2 Using Module............................... 31 2.8.3 Nested Module.............................. 33 2.9 Path Alias and Namespace........................... 33 2.10 Conventions.................................... 34 2.10.1 URL.................................... 34 2.10.2 Code.................................... 35 2.10.3 Configuration............................... 35 2.10.4 File.................................... 35 2.10.5 Directory................................. 36 2.11 Development Workflow.............................. 37 3 Working with Forms 39 3.1 Working with Form................................ 39 3.2 Creating Model.................................. 39 3.2.1 Defining Model Class........................... 40 3.2.2 Declaring Validation Rules....................... 40 3.2.3 Securing Attribute Assignments..................... 43 3.2.4 Triggering Validation........................... 45 3.2.5 Retrieving Validation Errors...................... 46 3.2.6 Attribute Labels............................. 46 3.3 Creating Action.................................. 46 3.4 Creating Form.................................. 48 3.5 Collecting Tabular Input............................. 49 iv Contents 4 Working with Databases 53 4.1 Working with Database............................. 53 4.2 Data Access Objects (DAO)........................... 53 4.2.1 Establishing Database Connection................... 54 4.2.2 Executing SQL Statements....................... 55 4.2.3 Fetching Query Results......................... 56 4.2.4 Using Transactions............................ 56 4.2.5 Binding Parameters........................... 57 4.2.6 Binding Columns............................. 58 4.3 Active Record................................... 58 4.3.1 Establishing DB Connection....................... 59 4.3.2 Defining AR Class............................ 60 4.3.3 Creating Record............................. 61 4.3.4 Reading Record.............................. 62 4.3.5 Updating Record............................. 65 4.3.6 Deleting Record............................. 65 4.3.7 Data Validation.............................. 66 4.3.8 Comparing Records........................... 67 4.3.9 Customization.............................. 67 4.3.10 Using Transaction with AR....................... 67 4.3.11 Named Scopes.............................. 68 4.4 Relational Active Record............................ 70 4.4.1 Declaring Relationship.......................... 72 4.4.2 Performing Relational Query...................... 74 Contents v 4.4.3 Relational Query Options........................ 76 4.4.4 Dynamic Relational Query Options................... 78 4.4.5 Statistical Query............................. 78 4.4.6 Relational Query with Named Scopes................. 80 5 Caching 83 5.1 Caching...................................... 83 5.2 Data Caching................................... 85 5.2.1 Cache Dependency............................ 86 5.3 Fragment Caching................................ 87 5.3.1 Caching Options............................. 87 5.3.2 Nested Caching.............................. 89 5.4 Page Caching................................... 90 5.5 Dynamic Content................................. 91 6 Extending Yii 93 6.1 Overview..................................... 93 6.2 Using Extensions................................. 94 6.2.1 Application Component......................... 94 6.2.2 Widget.................................. 95 6.2.3 Action................................... 95 6.2.4 Filter................................... 95 6.2.5 Controller................................. 96 6.2.6 Validator................................. 96 6.2.7 Console Command............................ 97 vi Contents 6.2.8 Module.................................. 98 6.2.9 Generic Component........................... 98 6.3 Creating Extensions............................... 98 6.3.1 Application Component......................... 99 6.3.2 Widget.................................. 99 6.3.3 Action................................... 100 6.3.4 Filter................................... 100 6.3.5 Controller................................. 101 6.3.6 Validator................................. 101 6.3.7 Console Command............................ 101 6.3.8 Module.................................. 102 6.3.9 Generic Component........................... 102 6.4 Using 3rd-Party Libraries............................ 102 7 Special Topics 105 7.1 URL Management................................ 105 7.1.1 Creating URLs.............................. 105 7.1.2 User-friendly URLs............................ 106 7.2 Authentication and Authorization....................... 110 7.2.1 Defining Identity Class.......................... 110 7.2.2 Login and Logout............................ 112 7.2.3 Access Control Filter........................... 112 7.2.4 Role-Based Access Control....................... 116 7.3 Theming...................................... 121 7.4 Logging...................................... 122 Contents vii 7.4.1 Message Logging............................. 122 7.4.2 Message Routing............................. 123 7.4.3 Performance Profiling.......................... 126 7.5 Error Handling.................................. 127 7.5.1 Raising Exceptions............................ 127 7.5.2 Displaying Errors............................. 128 7.5.3 Message Logging............................. 130 7.6 Web Service.................................... 130 7.6.1 Defining Service Provider........................ 130 7.6.2 Declaring Web Service Action...................... 131 7.6.3 Consuming Web Service......................... 132 7.6.4 Data Types................................ 132 7.6.5 Class Mapping.............................. 133 7.6.6 Intercepting Remote Method Invocation................ 134 7.7 Internationalization................................ 134 7.7.1 Locale and Language........................... 135 7.7.2 Translation................................ 135 7.7.3 Date and Time Formatting....................... 138 7.7.4 Number Formatting........................... 139 7.8 Using Alternative Template Syntax....................... 139 7.8.1 Using CPradoViewRenderer ........................ 140 7.9 Console Applications............................... 143 7.9.1 Using the yiic Tool........................... 144 7.10 Security...................................... 144 viii Contents 7.10.1 Cross-site Scripting Prevention..................... 144 7.10.2 Cross-site Request Forgery Prevention................. 145 7.10.3 Cookie Attack Prevention........................ 146 7.11 Performance Tuning............................... 147 7.11.1 Enabling APC Extension........................ 147 7.11.2 Disabling Debug Mode.......................... 147 7.11.3 Using yiilite.php ............................ 147 7.11.4 Using Caching Techniques........................ 148 7.11.5 Database Optimization......................... 148 7.11.6 Minimizing Script Files......................... 149 License of Yii The Yii framework is free software. It is released under the terms of the following BSD License. Copyright c 2008-2009 by Yii Software LLC. All rights reserved. Redistribution and