"The Definitive Guide to Yii 2.0"
Total Page:16
File Type:pdf, Size:1020Kb
The Definitive Guide to Yii 2.0 http://www.yiiframework.com/doc/guide Qiang Xue, Alexander Makarov, Carsten Brandt, Klimov Paul, and many contributors from the Yii community This tutorial is released under the Terms of Yii Documentation. Copyright 2014 Yii Software LLC. All Rights Reserved. Contents 1 Introduction1 1.1 What is Yii............................1 1.2 Upgrading from Version 1.1...................2 2 Getting Started 13 2.1 What do you need to know................... 13 2.2 Installing Yii........................... 14 2.3 Running Applications...................... 23 2.4 Saying Hello............................ 27 2.5 Working with Forms....................... 30 2.6 Working with Databases..................... 36 2.7 Generating Code with Gii.................... 42 2.8 Looking Ahead.......................... 48 3 Application Structure 51 3.1 Overview............................. 51 3.2 Entry Scripts........................... 52 3.3 Applications............................ 54 3.4 Application Components..................... 66 3.5 Controllers............................ 69 3.6 Models............................... 78 3.7 Views............................... 89 3.8 Modules.............................. 103 3.9 Filters............................... 110 3.10 Widgets.............................. 118 3.11 Assets............................... 122 3.12 Extensions............................. 142 4 Handling Requests 155 4.1 Overview............................. 155 4.2 Bootstrapping........................... 156 4.3 Routing and URL Creation................... 157 4.4 Requests.............................. 172 iii iv CONTENTS 4.5 Responses............................. 176 4.6 Sessions and Cookies....................... 182 4.7 Handling Errors.......................... 190 4.8 Logging.............................. 194 5 Key Concepts 203 5.1 Components............................ 203 5.2 Properties............................. 205 5.3 Events............................... 207 5.4 Behaviors............................. 215 5.5 Configurations.......................... 222 5.6 Aliases............................... 227 5.7 Class Autoloading........................ 230 5.8 Service Locator.......................... 232 5.9 Dependency Injection Container................. 235 6 Working with Databases 247 6.1 Database Access Objects..................... 247 6.2 Query Builder........................... 261 6.3 Active Record........................... 280 6.4 Database Migration........................ 315 7 Getting Data from Users 339 7.1 Creating Forms.......................... 339 7.2 Validating Input......................... 344 7.3 Uploading Files.......................... 361 7.4 Collecting tabular input..................... 366 7.5 Getting Data for Multiple Models................ 368 7.6 Extending ActiveForm on the Client Side........... 369 8 Displaying Data 375 8.1 Data Formatting......................... 375 8.2 Pagination............................. 380 8.3 Sorting............................... 382 8.4 Data Providers.......................... 384 8.5 Data widgets........................... 391 8.6 Working with Client Scripts................... 405 8.7 Theming.............................. 410 9 Security 413 9.1 Security.............................. 413 9.2 Authentication.......................... 413 9.3 Authorization........................... 417 9.4 Working with Passwords..................... 435 CONTENTS v 9.5 Cryptography........................... 436 9.6 Security best practices...................... 437 10 Caching 447 10.1 Caching.............................. 447 10.2 Data Caching........................... 447 10.3 Fragment Caching........................ 456 10.4 Page Caching........................... 460 10.5 HTTP Caching.......................... 461 11 RESTful Web Services 465 11.1 Quick Start............................ 465 11.2 Resources............................. 469 11.3 Controllers............................ 474 11.4 Routing.............................. 478 11.5 Response Formatting....................... 480 11.6 Authentication.......................... 484 11.7 Rate Limiting........................... 487 11.8 Versioning............................. 488 11.9 Error Handling.......................... 490 12 Development Tools 493 13 Testing 495 13.1 Testing............................... 495 13.2 Testing environment setup.................... 496 13.3 Unit Tests............................. 497 13.4 Functional Tests......................... 498 13.5 Acceptance Tests......................... 498 13.6 Fixtures.............................. 499 14 Special Topics 507 14.1 Creating your own Application structure............ 507 14.2 Console applications....................... 508 14.3 Core Validators.......................... 515 14.4 Yii and Docker.......................... 532 14.5 Internationalization........................ 534 14.6 Mailing.............................. 549 14.7 Performance Tuning....................... 554 14.8 Shared Hosting Environment.................. 559 14.9 Using template engines...................... 560 14.10Working with Third-Party Code................. 561 14.11Using Yii as a Micro-framework................. 565 15 Widgets 571 vi CONTENTS 16 Helpers 573 16.1 Helpers.............................. 573 16.2 ArrayHelper............................ 574 16.3 Html helper............................ 583 16.4 Url Helper............................. 590 Chapter 1 Introduction 1.1 What is Yii Yii is a high performance, component-based PHP framework for rapidly developing modern Web applications. The name Yii (pronounced Yee or [ji :]) means “simple and evolutionary” in Chinese. It can also be thought of as an acronym for Yes It Is! 1.1.1 What is Yii Best for? Yii is a generic Web programming framework, meaning that it can be used for developing all kinds of Web applications using PHP. Because of its component-based architecture and sophisticated caching support, it is es- pecially suitable for developing large-scale applications such as portals, for- ums, content management systems (CMS), e-commerce projects, RESTful Web services, and so on. 1.1.2 How does Yii Compare with Other Frameworks? If you’re already familiar with another framework, you may appreciate know- ing how Yii compares: • Like most PHP frameworks, Yii implements the MVC (Model-View- Controller) architectural pattern and promotes code organization based on that pattern. • Yii takes the philosophy that code should be written in a simple yet elegant way. Yii will never try to over-design things mainly for the purpose of strictly following some design pattern. • Yii is a full-stack framework providing many proven and ready-to- use features: query builders and ActiveRecord for both relational and NoSQL databases; RESTful API development support; multi-tier cach- ing support; and more. 1 2 CHAPTER 1. INTRODUCTION • Yii is extremely extensible. You can customize or replace nearly every piece of the core’s code. You can also take advantage of Yii’s solid extension architecture to use or develop redistributable extensions. • High performance is always a primary goal of Yii. Yii is not a one-man show, it is backed up by a strong core developer team1, as well as a large community of professionals constantly contributing to Yii’s development. The Yii developer team keeps a close eye on the latest Web development trends and on the best practices and features found in other frameworks and projects. The most relevant best practices and features found elsewhere are regularly incorporated into the core framework and ex- posed via simple and elegant interfaces. 1.1.3 Yii Versions Yii currently has two major versions available: 1.1 and 2.0. Version 1.1 is the old generation and is now in maintenance mode. Version 2.0 is a com- plete rewrite of Yii, adopting the latest technologies and protocols, including Composer, PSR, namespaces, traits, and so forth. Version 2.0 represents the current generation of the framework and will receive the main development efforts over the next few years. This guide is mainly about version 2.0. 1.1.4 Requirements and Prerequisites Yii 2.0 requires PHP 5.4.0 or above and runs best with the latest version of PHP 7. You can find more detailed requirements for individual features by running the requirement checker included in every Yii release. Using Yii requires basic knowledge of object-oriented programming (OOP), as Yii is a pure OOP-based framework. Yii 2.0 also makes use of the latest features of PHP, such as namespaces2 and traits3. Understanding these con- cepts will help you more easily pick up Yii 2.0. 1.2 Upgrading from Version 1.1 There are many differences between versions 1.1 and 2.0 of Yii as the frame- work was completely rewritten for 2.0. As a result, upgrading from version 1.1 is not as trivial as upgrading between minor versions. In this guide you’ll find the major differences between the two versions. If you have not used Yii 1.1 before, you can safely skip this section and turn directly to “Getting started“. Please note that Yii 2.0 introduces more new features than are covered in this summary. It is highly recommended that you read through the whole 1http://www.yiiframework.com/team/ 2https://secure.php.net/manual/en/language.namespaces.php 3https://secure.php.net/manual/en/language.oop5.traits.php 1.2. UPGRADING FROM VERSION 1.1 3 definitive guide to learn about them all. Chances are that some features you previously had to develop for yourself are now part of the core code. 1.2.1 Installation