ebook img

The Yii Book PDF

575 Pages·5.675 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The Yii Book

The Yii Book by Larry Ullman Self-published Find this book on the Web at yii.larryullman.com. Revision: 0.9 Copyright © 2014 by Larry Ullman Technical Reviewer: Qiang Xue Technical Reviewer: Alexander Makarov Cover design very kindly provided by Paul Wilcox. Notice of Rights All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the author. Notice of Liability The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of the book, the author shallnothaveanyliabilitytoanypersonorentitywithrespecttoanylossordamage caused or alleged to be caused directly or indirectly by the instructions contained in this book or by the computer software and hardware products described in it. Trademarks MySQL is a registered trademark of Oracle in the United States and in other coun- tries. Macintosh and Mac OS X are registered trademarks of Apple, Inc. Microsoft and Windows are registered trademarks of Microsoft Corp. Other product names usedinthisbookmaybetrademarksoftheirownrespectiveowners. Imagesof Web sites in this book are copyrighted by the original holders and are used with their kind permission. This book is not officially endorsed by nor affiliated with any of the above companies. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, andtheauthorwasawareofatrademarkclaim,thedesignationsappearasrequested by the owner of the trademark. All other product names and services identified throughout this book are used in editorial fashion only and for the benefit of such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation with this book. ISBN-13: ISBN-10: ii This book is dedicated to: Qiang Xue, creator of the Yii framework; Alexander Makarov, and the whole Yii development team; and to the entire Yii community. Thanks to you all for making, embracing, and supporting such an excellent Web development tool. iii Contents Introduction 1 Why Frameworks? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Why Yii? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 What You’ll Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1 FUNDAMENTAL CONCEPTS 13 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . 13 The MVC Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Using a Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Command Line Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2 STARTING A NEW APPLICATION 31 Downloading Yii . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Testing the Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Installing the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Building the Site Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Testing the Site Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 3 A MANUAL FOR YOUR YII SITE 37 The Site’s Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Referencing Files and Directories . . . . . . . . . . . . . . . . . . . . . . . 39 Yii Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 How Yii Handles a Page Request . . . . . . . . . . . . . . . . . . . . . . . 41 i CONTENTS 4 INITIAL CUSTOMIZATIONS AND CODE GENERATIONS 46 Enabling Debug Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Moving the Protected Folder . . . . . . . . . . . . . . . . . . . . . . . . . 47 Basic Configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Developing Your Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Generating Code with Gii . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5 WORKING WITH MODELS 77 The Model Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Establishing Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Changing Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Watching for Model Events . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Relating Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 6 WORKING WITH VIEWS 107 The View Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Where Views are Referenced . . . . . . . . . . . . . . . . . . . . . . . . . 108 Layouts and Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Editing View Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Working with Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Alternative Content Presentation . . . . . . . . . . . . . . . . . . . . . . . 126 7 WORKING WITH CONTROLLERS 131 Controller Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Revisiting Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Making Use of Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Handling Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Basic Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Understanding Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Tapping Into Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Showing Static Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 ii CONTENTS 8 WORKING WITH DATABASES 162 Debugging Database Operations . . . . . . . . . . . . . . . . . . . . . . . 162 Database Options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Using Active Record . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Using Query Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Using Database Access Objects . . . . . . . . . . . . . . . . . . . . . . . . 186 Choosing an Interface Option . . . . . . . . . . . . . . . . . . . . . . . . . 189 Common Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 9 WORKING WITH FORMS 193 Understanding Forms and MVC . . . . . . . . . . . . . . . . . . . . . . . 193 Creating Forms without Models . . . . . . . . . . . . . . . . . . . . . . . . 195 Using CHtml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Using “Active” Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Using CActiveForm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Using Form Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Common Form Needs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 10 MAINTAINING STATE 226 Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 11 USER AUTHENTICATION AND AUTHORIZATION 235 Fundamentals of Authentication . . . . . . . . . . . . . . . . . . . . . . . 235 Authentication Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 The UserIdentity State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Working with Flash Messages . . . . . . . . . . . . . . . . . . . . . . . . . 270 12 WORKING WITH WIDGETS 273 Using Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Basic Yii Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Presenting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 The jQuery UI Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 iii CONTENTS 13 USING EXTENSIONS 311 The Basics of Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 The bootstrap Extension. . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 The giix Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Validator Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Auto-Setting Timestamps . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 Using a WYSIWYG Editor . . . . . . . . . . . . . . . . . . . . . . . . . . 324 14 JAVASCRIPT AND JQUERY 329 What You Must Know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 Adding JavaScript to a Page . . . . . . . . . . . . . . . . . . . . . . . . . 330 Using JavaScript with CActiveForm . . . . . . . . . . . . . . . . . . . . . 335 Implementing Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Common Needs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 15 INTERNATIONALIZATION 357 What is i18n? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 Setting the Locale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Detecting the User’s Locale . . . . . . . . . . . . . . . . . . . . . . . . . . 360 Providing Language-Appropriate Text . . . . . . . . . . . . . . . . . . . . 362 Formatting Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 Representing Time Zones . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 i18n and Your Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 16 LEAVING THE BROWSER 376 Writing a Proxy Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 Creating Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 Creating a Console Application . . . . . . . . . . . . . . . . . . . . . . . . 393 iv CONTENTS 17 IMPROVING PERFORMANCE 400 Testing Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 Changing the Bootstrap . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408 Changing the Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . 409 Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 Improving Database Performance . . . . . . . . . . . . . . . . . . . . . . . 420 Using Multiple Caching Mechanisms . . . . . . . . . . . . . . . . . . . . . 426 Browser Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 Creating a Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432 18 ADVANCED DATABASE ISSUES 434 Database Migration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434 Calling Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 Using Complex Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442 Using Complex Relationships . . . . . . . . . . . . . . . . . . . . . . . . . 446 19 EXTENDING YII 450 Fundamental Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 More Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470 Working with Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479 Deploying Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506 20 WORKING WITH THIRD-PARTY LIBRARIES 507 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507 Accessing Library Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 Working with Composer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514 Using Symfony . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518 Using Swift Mailer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 Using Elasticsearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524 v CONTENTS 21 TESTING YOUR APPLICATIONS 547 Test Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548 Using PHPUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548 Using Selenium . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559 Checking Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565 vi Introduction This is the 24th book that I’ve written, and of the many things I’ve learned in that time, a reliable fact is this: readers rarely read the introduction. Still, I put a fair amount of time into the introduction and would ask you to spend the five minutes required to read it. Inthisparticularintroduction,Iprovidetheargumentsfor(andagainst)frameworks, and the Yii framework specifically. I also explain what knowledge and technical requirements are expected of you, the dear reader. And if that was not enough, the introduction concludes by providing you with resources you can use to seek help when you need it. So: five minutes of your time for all that. Okay, maybe 8 minutes. How about you give it a go? Why Frameworks? Simplyput,aframeworkisanestablishedlibraryofcodemeanttoexpeditesoftware development. Writing everything from scratch on every project is impractical; code reuse is faster, more reliable, and possibly more secure. Many developers eventually create a lightweight framework of their own, even if that’s just a handful of commonly used functions. True frameworks such as Yii are just the release of a complete set of tools that a smart and hardworking person has been kind enough to make public. Even if you don’t buy the arguments for using a framework in its own right, it’s safe to say that the ability to use a frame- work, whether that means a few pieces of your own reusable code or a full-fledged framework such as Yii, is to be expected for any regular programmer today. Why You Should Use a Framework The most obvious argument for using a framework is that you’ll be able to develop projects much, much faster than if you didn’t use a framework. But there are other arguments, and those are more critical. 1

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.