The Yii Book (2nd ed.) by Larry Ullman Self-published Find this book on the web at larry.pub. Revision: 1.7 Copyright © 2017 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 shall not have any liability to any person or entity with respect to any loss or damage 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 used in this book may be trademarks of their own respective owners. Images of 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 anyof 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, and the author was aware of a trademark claim, the designations appear as requested 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 ................................ 9 Getting Help .................................. 11 I Getting Started 13 1 FUNDAMENTAL CONCEPTS 14 Object-Oriented Programming ........................ 14 The MVC Approach .............................. 21 Using a Web Server .............................. 26 Command Line Tools ............................. 28 2 STARTING A NEW APPLICATION 32 Working with Composer ............................ 32 Creating a Yii Application ........................... 35 Testing the Site Shell .............................. 36 Testing the Requirements ........................... 39 3 A MANUAL FOR YOUR YII SITE 43 The Site’s Folders ............................... 43 Referencing Files and Directories ....................... 45 Yii Conventions ................................. 46 i CONTENTS How Yii Handles a Page Request ....................... 47 Using the Debugger .............................. 52 4 INITIAL CUSTOMIZATIONS AND CODE GENERATIONS 55 Configuring Your Web Server ......................... 55 Enabling Debug Mode ............................. 56 Basic Configurations .............................. 58 Developing Your Site .............................. 68 Generating Code with Gii ........................... 77 II Core Concepts 85 5 WORKING WITH MODELS 86 The Model Classes ............................... 86 Establishing Rules ............................... 88 Changing Labels ................................ 108 Watching for Model Events .......................... 110 Relating Models ................................ 113 The Object Class ................................ 116 6 WORKING WITH VIEWS 118 The View Structure .............................. 118 Where Views are Referenced ......................... 119 Layouts and Views ............................... 120 Editing View Files ............................... 122 Working with Layouts ............................. 132 Alternative Content Presentation ....................... 138 7 WORKING WITH CONTROLLERS 142 Controller Basics ................................ 142 Revisiting Views ................................ 147 Making Use of Models ............................. 148 Handling Forms ................................. 151 ii CONTENTS Understanding Routes ............................. 153 Tapping Into Filters .............................. 159 Showing Static Pages .............................. 160 Handling Exceptions .............................. 162 8 WORKING WITH DATABASES 165 Debugging Database Operations ....................... 165 Database Options ................................ 166 Using Active Record .............................. 168 Using Database Access Objects ........................ 177 Using Query Builder .............................. 180 Choosing an Interface Option ......................... 186 Common Challenges .............................. 188 9 WORKING WITH FORMS 191 Understanding Forms and MVC ....................... 191 Creating Forms without Models ........................ 192 Creating Forms with Models ......................... 195 Common Form Needs ............................. 199 10 MAINTAINING STATE 220 Cookies ..................................... 220 Sessions ..................................... 224 Working with Flash Messages ......................... 227 11 USER AUTHENTICATION AND AUTHORIZATION 230 Fundamentals of Authentication ....................... 230 Authentication Options ............................ 241 Referencing the User .............................. 249 Basic Access Control .............................. 250 Role-Based Access Control ........................... 255 iii CONTENTS 12 WORKING WITH WIDGETS 267 Using Widgets ................................. 267 Basic Yii Widgets ............................... 270 Presenting Data ................................ 274 The Bootstrap Widgets ............................ 295 The jQuery UI Widgets ............................ 296 13 USING EXTENSIONS 302 The Basics of Extensions ........................... 302 The Krajee Extensions ............................. 305 The 2amigos Extensions ............................ 310 A User Extension ................................ 314 Using Swift Mailer ............................... 315 Using Elasticsearch ............................... 318 14 JAVASCRIPT AND JQUERY 338 What You Must Know ............................. 338 Adding JavaScript to a Page ......................... 339 Using JavaScript with ActiveForm ...................... 343 Implementing Ajax ............................... 346 Common Needs ................................. 354 III Advanced Topics 359 15 INTERNATIONALIZATION 360 What is i18n? .................................. 360 Setting the Locale ............................... 362 Detecting the User’s Locale .......................... 363 Providing Language-Appropriate Text .................... 365 Using Placeholders and Parameters ...................... 368 Using Formatters ................................ 374 Other Concepts ................................. 379 i18n and Your Models ............................. 380 iv CONTENTS 16 LEAVING THE BROWSER 382 Writing a Proxy Script ............................. 382 RESTful Services ................................ 385 Creating a Console Application ........................ 391 v Introduction This is the 26th book that I’ve written, and of the many things I’ve learned in that time, a reliable fact is readers rarely read the introduction. Still, I put some thought into the introduction and recommend you spend the five minutes required to read it. In this particular introduction, I provide the arguments for (and against) frameworks, and the Yii framework specifically. I also explain what knowledge and technical requirements are expected of you, dear reader. And if that was not enough, the introduction concludes by providing you with resources to seek help when you need it. So: five minutes of your time for all that! Okay, maybe 8 minutes. How aboutyou give it a go? Why Frameworks? Simply put, a framework is an established library of code meant to expedite software 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–or more commonly, team–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 framework is an expectation of any professional programmer today. Why You Should Use a Framework The most obvious argument for using a framework is being able to develop projects much, much faster than if you don’t use a framework. But there are other, more critical, arguments, too. As already stated, framework-based projects should also be both more reliable and secure than those coded by hand. Both qualities
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages405 Page
-
File Size-