www.it-ebooks.info Yii Application Development Cookbook Second Edition A Cookbook covering both practical Yii application development tips and the most important Yii features Alexander Makarov BIRMINGHAM - MUMBAI www.it-ebooks.info Yii Application Development Cookbook Second Edition Copyright © 2013 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: August 2011 Second edition: April 2013 Production Reference: 1150413 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78216-310-7 www.packtpub.com Cover Image by Alexander Makarov ([email protected]) www.it-ebooks.info Credits Author Project Coordinator Alexander Makarov Kranti Berde Reviewers Proofreaders Maurizio Domba Maria Gould Thomas Jantz Paul Hindle Lawrence A. Herman Acquisition Editor Usha Iyer Indexer Hemangini Bari Lead Technical Editor Joel Noronha Graphics Ronak Dhruv Technical Editors Hardik B. Soni Production Coordinator Ankita Meshram Nilesh R. Mohite Cover Work Nilesh R. Mohite www.it-ebooks.info About the Author Alexander Makarav is an experienced engineer from Russia and has been a Yii framework core team member since 2010. Before joining the Yii core team, he participated in the CodeIgniter community growth in Russia. In 2009, he finished the Russian translation of the framework documentation and created the Russian community website. In 2012, he released the Russian version of the book along with Russian community members. In the same year, he was the technical reviewer for three more books: f The Yii Book: Developing Web Applications Using the Yii PHP Framework, Larry Ullman f Web Application Development with Yii and PHP, Jeff Winesett f Yii Rapid Application Development Hotshot, Lauren O'Meara and James Hamilton In his free time, Alexander writes technical blog at http://rmcreative.ru/, speaks at conferences, and enjoys movies, music, traveling, photography, and languages. He currently resides in Voronezh, Russia with his beloved wife and daughter. www.it-ebooks.info About the Reviewers Maurizio Domba is a frontend and backend web developer with over 20 years of professional experience in computer programming and 10 years in web development. He is part of the Yii core development team since August 2010 and is an active member of the Yii community. At the moment he is developing intranet web applications for an export-import enterprise and working on other international projects, always trying to help others to improve their code and project usability. When not programming the Web, he is programming his wife and kids, always with a smile on his face, open-hearted and open-minded. He loves climbing, martial arts, meditation, and salsa. Thomas Jantz brings his background in language and art to his career as a web application development consultant at Plum Flower Software. His projects include an emphasis on rapid application development and user experience. www.it-ebooks.info www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why Subscribe? f Fully searchable across every book published by Packt f Copy and paste, print and bookmark content f On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. www.it-ebooks.info Table of Contents Preface 1 Chapter 1: Under the Hood 7 Introduction 7 Using getters and setters 7 Using Yii events 10 Using import and autoloading 17 Using exceptions 20 Configuring components 24 Configuring widget defaults 27 Using Yii core collections 28 Working with requests 32 Chapter 2: Router, Controller, and Views 37 Introduction 38 Configuring URL rules 38 Generating URLs by path 41 Using regular expressions in URL rules 45 Creating URL rules for static pages 48 Providing your own URL rules at runtime 51 Using a base controller 55 Using external actions 57 Displaying static pages with CViewAction 61 Using flash messages 63 Using the controller context in a view 64 Reusing views with partials 66 Using clips 68 Using decorators 70 Defining multiple layouts 71 Paginating and sorting data 73 www.it-ebooks.info Table of Contents Chapter 3: AJAX and jQuery 77 Introduction 77 Loading a block through AJAX 77 Managing assets 83 Including resources in the page 88 Working with JSON 92 Passing configuration from PHP to JavaScript 95 Handling variable number of inputs 98 Rendering content at the client side 105 Chapter 4: Working with Forms 119 Introduction 119 Writing your own validators 119 Uploading files 122 Adding CAPTCHA 126 Customizing CAPTCHA 131 Creating a custom input widget with CInputWidget 134 Chapter 5: Testing Your Application 139 Introduction 139 Setting up the testing environment 139 Writing and running unit tests 143 Using fixtures 149 Testing the application with functional tests 155 Generating code coverage reports 160 Chapter 6: Database, Active Record, and Model Tricks 165 Introduction 166 Getting data from a database 166 Defining and using multiple DB connections 172 Using scopes to get models for different languages 175 Processing model fields with AR event-like methods 178 Applying markdown and HTML 181 Highlighting code with Yii 183 Automating timestamps 189 Setting up an author automatically 191 Implementing single table inheritance 193 Using CDbCriteria 197 Chapter 7: Using Zii Components 199 Introduction 199 Using data providers 199 Using grids 206 ii www.it-ebooks.info Table of Contents Using lists 214 Creating custom grid columns 219 Chapter 8: Extending Yii 225 Introduction 225 Creating model behaviors 226 Creating components 232 Creating reusable controller actions 236 Creating reusable controllers 239 Creating a widget 243 Creating CLI commands 245 Creating filters 249 Creating modules 251 Creating a custom view renderer 258 Making extensions distribution-ready 263 Chapter 9: Error Handling, Debugging, and Logging 267 Introduction 267 Using different log routes 267 Analyzing the Yii error stack trace 274 Logging and using the context information 277 Implementing your own smart 404 handler 281 Chapter 10: Security 287 Introduction 287 Using controller filters 287 Using CHtml and CHtmlPurifier to prevent XSS 292 Preventing SQL injections 296 Preventing CSRF 301 Using RBAC 305 Chapter 11: Performance Tuning 313 Introduction 313 Following best practices 313 Speeding up session handling 317 Using cache dependencies and chains 321 Profiling an application with Yii 327 Leveraging HTTP caching 337 Chapter 12: Using External Code 343 Introduction 343 Using Zend Framework from Yii 343 Customizing the Yii autoloader 348 Using Kohana inside Yii 353 iii www.it-ebooks.info
Description: