Book Image

Yii 1.1 Application Development Cookbook

Book Image

Yii 1.1 Application Development Cookbook

Overview of this book

When Alex told me he was about to write a Yii cookbook about a year ago, I was wondering how original it would be, considering the fact that there was already an online user-contributed cookbook (aka. Yii wiki). It turned out Alex produced a book that is not only full of wisdom about how to use Yii effectively, but also presented in such a systematic way that it can be taken as an essential companion book to the definitive guide to Yii. In fact, Alex has successfully intrigued the interest of every member in the Yii developer team when he asked for review and comments on his newly finished book chapters.As the founder and the lead developer of the Yii framework, I feel this book is a must-read for every Yii programmer. While this book does not describe directly the rules set by Yii, it shows how to program with Yii from a practical perspective. People who are driven by tight project schedules will find this book very handy as it gives ready-to-use solutions to many problems they may face in their projects; people who are already familiar with Yii will also find this book very informative as most problem solutions given in the book can be considered as officially recommended because they have undergone thorough review of every Yii developer team member. Alex, through this book and his active participation in the Yii project, proved himself to be a great programmer as well as a good writer. Qiang XueLead developer of the Yii framework Yii framework is a rapidly growing PHP5 MVC framework often referred to as Rails for PHP. It has become a solid base for many exciting web applications such as Stay.com and Russia Today's meetfriends.rt.com and can be a good base for your developments. Yii is an object-oriented, high-performance, component-based PHP web application framework. Yii is pronounced as Yee and is an acronym for "Yes It Is!". Familiar with Yii and want to exploit it to its full potential, but do not know how to go about it? Yii 1.1 Application Development Cookbook will show you how to use Yii efficiently. You will learn about implementing shortcuts using core features, creating your own reusable code base, using test-driven development, and many more topics that will escalate your knowledge in no time at all! Yii 1.1 Application Development Cookbook will help you learn more about Yii framework and application development practices in general with demonstrations of shortcuts and information about dangerous things you should not do. Grouped in 13 chapters, the recipes will assist you to write your applications exploiting Yii core functionality to its full potential. The chapters are generally independent of each other and you can start reading from the chapter you need most, whether it is "AJAX and jQuery", "Database, Active Record and Model Tricks" or "Extending Yii". The most interesting topics include Yii application deployment, a guide to writing your own extensions, advanced error handling, debugging and logging, application security, and performance tuning. Yii 1.1 Application Development Cookbook will help you utilize Yii functionalities completely and efficiently.
Table of Contents (21 chapters)
Yii 1.1 Application Development Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Yii is a very flexible and high-performance application development framework written in PHP. It helps building web applications from small ones to large-scale enterprise applications. The framework name stands for Yes It Is. This is often the accurate and most concise response to inquires from those new to Yii:

Is it fast? ... Is it secure? ... Is it professional? ... Is it right for my next project? ... The answer is Yes, it is!

This cookbook contains 13 independent chapters full of recipes that will show you how to use Yii efficiently. You will learn about the hidden framework gems, using core features, creating your own reusable code base, using test-driven development, and many more topics that will bring your knowledge to a new level!

What this book covers

Chapter 1, Under the Hood provides information about the most interesting Yii features hidden under the hood: events, import, autoloading, exceptions, component, and widget configuration, and more.

Chapter 2, Router , Controller, and Views is about handy things concerning the Yii URL router, controllers, and views: URL rules, external actions and controllers, view clips, decorators, and more.

Chapter 3, AJAX and jQuery focuses on the Yii's client side that is built with jQuery—the most widely used JavaScript library out there. It is very powerful and easy to learn and use. This chapter focuses on Yii-specific tricks rather than jQuery itself.

Chapter 4, Working with Forms. Yii makes working with forms a breeze and documentation on it is almost complete. Still, there are some areas that need clarification and examples. Some of the topics covered in this chapter are creating own validators and input widgets, uploading files, using, and customizing CAPTCHA.

Chapter 5, Testing Your Application covers both unit testing, functional testing, and generating code coverage reports. Recipes follow a test driven development approach. You will write tests for several small applications and then will implement functionality.

Chapter 6, Database, Active Record, and Model Tricks is about working with databases efficiently, when to use models and when not to, how to work with multiple databases, how to automatically pre-process Active Record fields, and how to use powerful database criteria.

Chapter 7, Using Zii Components covers data providers, grids, and lists: How to configure sorting and search, how to use grids with multiple related models, how to create your own column types, and more.

Chapter 8, Extending Yii shows not only how to implement your own Yii extension but also how to make your extension reusable and useful for the community. In addition, we will focus on many things you should do to make your extension as efficient as possible.

Chapter 9, Error Handling , Debugging, and Logging reviews logging, analyzing the exception stack trace, and own error handler implementation.

Chapter 10, Security provides information about keeping your application secure according to the general web application security principle "filter input escape output". We will cover topics such as creating your own controller filters, preventing XSS, CSRF, and SQL injections, escaping output, and using role-based access control.

Chapter 11, Performance Tuning shows how to configure Yii to gain extra performance. You will learn a few best practices of developing an application that will run smoothly until you have very high loads.

Chapter 12, Using External Code focuses on using the third party code with Yii. We will use Zend Framework, Kohana, and PEAR but you will be able to use any code after learning how it works.

Chapter 13, Deployment covers various tips that are especially useful on application deployment, when developing an application in a team, or when you just want to make your development environment more comfortable.

What you need for this book

In order to run the examples in this book, the following software will be required:

  • Web server:

    • 2.x version of Apache web server is preferred

    • Other versions and web servers will work too, but configuration details are not provided

  • Database server: MySQL is recommended

    • MySQL 4+ with InnoDB support, MySQL 5 or higher recommended

  • PHP: PHP 5.3 is recommended

    • PHP 5.2 or PHP 5.3, PHP 5.3 recommended

  • Yii:

    • latest 1.1.x

Additionally, the following tools are not strictly required but are used for specific recipes:

  • PHPUnit

  • XDebug

  • Selenium RC

  • PEAR

  • Smarty

  • memcached

Who this book is for

If you are a developer with a good knowledge of PHP5, are familiar with the basics of Yii, have checked its definitive guide, and have tried to develop applications using Yii, then this book is for you. Knowledge of the object-oriented approach and MVC pattern will be a great advantage as Yii uses these extensively.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We can include view partials through the use of the include directive."

A block of code is set as follows:

defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 0);

$yii=dirname(__FILE__).'/../framework/yii.php';
$config=dirname(__FILE__).'/../app/config/production.php';

require($yii);

Yii::createWebApplication($config)->run();

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 0);

$yii=dirname(__FILE__).'/../framework/yii.php';
$config=dirname(__FILE__).'/../app/config/production.php';

require($yii);

Yii::createWebApplication($config)->run();

Any command-line input or output is written as follows:

cd path/to/protected/tests
phpunit unit/BBCodeTest.php

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send book author feedback about the book, simply fill a form at http://yiicookbook.org/feedback.

If there is a book that you need and would like to see PACKT publish, please send a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

To get the example code files for this book visit http://yiicookbook.org/code.

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in the book—maybe a mistake in the text or the code—we would be grateful if you would report this. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://yiicookbook.org/feedback and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on the book website at http://yiicookbook.org/errata.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact the book's author using http://yiicookbook.org/feedback if you are having a problem with any aspect of the book, and he will do his best to address it.