Table of Contents

This book is a collection of Yii recipes grouped in 13 chapters generally independent of each other.

Looking for the first edition?

Second edition

In the second edition many recipes were updated and rewritten to reflect latest Yii and PHP world changes and improve overall experience. Additionally all errata from the first edition was fixed.

There are some totally new recipes. These are underlined in table of contents.

Chapter 1, Under the Hood
Will tell you about most interesting Yii features hidden under the hood: events, import, autoloading, exceptions, component and widget configuration and more.
  • Using getters and setters
  • Using Yii events
  • Using import and autoloading
  • Using exceptions
  • Configuring components
  • Configuring widget defaults
  • Using Yii core collections
  • Working with requests
Chapter 2, Router, Controller, and Views
This chapter is about handy things about Yii URL router, controllers, and views: URL rules, external actions and controllers, view clips, decorators and more.
  • Configuring URL rules
  • Generating URLs by path
  • Using regular expressions in URL rules
  • Creating URL rules for static pages
  • Providing your own URL rules at runtime
  • Using base controller
  • Using external actions
  • Displaying static pages with CViewAction
  • Using flash messages
  • Using controller context in a view
  • Reusing views with partials
  • Using clips
  • Using decorators
  • Defining multiple layouts
  • Paginating and sorting data
Chapter 3, AJAX and jQuery
Yii’s client side is built with jQuery — the widely used, powerful, and simple to learn JavaScript library. This chapter focuses on Yii-specific tricks rather than jQuery itself.
  • Loading a block through AJAX
  • Managing assets
  • Including resources into the page
  • Working with JSON
  • Passing configuration from PHP to JavaScript
  • Handling variable number of inputs
  • Rendering content at the client side
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.
  • Writing your own validators
  • Uploading files
  • Adding CAPTCHA
  • Customizing CAPTCHA
  • Creating a custom input widget with CInputWidget
Chapter 5, Testing Your Application
This chapter covers both unit testing, functional testing and generating code coverage reports. Recipes are following test driven development approach: you'll write tests for several small applications and then will implement functionality.
  • Setting up the testing environment
  • Writing and running unit tests
  • Using fixtures
  • Testing the application with functional tests
  • Generating code coverage reports
Chapter 6, Database, Active Record, and Model Tricks
In this chapter you’ll learn how to work with database 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.
  • Getting data from a database
  • Defining and using multiple DB connections
  • Using scopes to get models for different languages
  • Processing model fields with AR event-like methods
  • Applying markdown and HTML
  • Highlighting code with Yii
  • Automating timestamps
  • Setting an author automatically
  • Implementing single table inheritance
  • Using CDbCriteria
Chapter 7, Using Zii Components
This chapter 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.
  • Using data providers
  • Using grids
  • Using lists
  • Creating custom grid columns
Chapter 8, Extending Yii
Shows not only how to implement your own Yii extension but how to make your extension reusable and useful for community. Also we’ll focus on many things you should do to make your extension as efficient as possible.
  • Creating model behaviors
  • Creating components
  • Creating reusable controller actions
  • Creating reusable controllers
  • Creating a widget
  • Creating CLI commands
  • Creating filters
  • Creating modules
  • Creating a custom view renderer
  • Making extensions distribution-ready
Chapter 9, Error Handling, Debugging, and Logging
In this chapter we will review logging, analysing exception stack trace and will implement our own error handler.
  • Using different log routes
  • Analyzing the Yii error stack trace
  • Logging and using the context information
  • Implementing your own smart 404 handler
Chapter 10, Security
You’ll learn how to keep your application secure according to general web application security principle “filter input escape output”. We’ll cover such topics as creating your own controller filters, preventing XSS, CSRF and SQL injections, escaping output and using role based access control.
  • Using controller filters
  • Using CHtml and CHtmlPurifier to prevent XSS
  • Preventing SQL injections
  • Preventing CSRF
  • Using RBAC
Chapter 11, Performance Tuning
In this chapter we’ll show how to configure Yii to gain extra performance. Also you’ll learn some best practices of developing an application that will run smooth until you have very high loads.
  • Following best practices
  • Speeding up sessions handling
  • Using cache dependencies and chains
  • Profiling an application with Yii
  • Leveraging HTTP caching
Chapter 12, Using External Code
Focuses on using third party code with Yii. We'll use Zend Framework, Kohana and PEAR but you'll be able to use any code after learning how it works.
  • Using Zend Framework from Yii
  • Customizing the Yii autoloader
  • Using Kohana inside Yii
  • Using PEAR inside Yii
  • Using Composer with Yii
Chapter 13, Deployment
Covers various tips that are especially useful on application deployment, when developing application in team or when you just want to make your development environment more comfortable.
  • Changing the Yii directories layout
  • Moving an application out of webroot
  • Sharing the framework directory
  • Moving configuration parts into separate files
  • Using multiple configurations to simplify the deployment
  • Implementing and executing cron jobs
  • Maintenance mode

First edition

Chapter 1, Under the Hood
Will tell you about most interesting Yii features hidden under the hood: events, import, autoloading, exceptions, component and widget configuration and more.
  • Using getters and setters
  • Using Yii events
  • Using import and autoloading
  • Using exceptions
  • Configuring components
  • Configuring widget defaults
  • Using Yii core collections
  • Working with request
Chapter 2, Router, Controller, and Views
This chapter is about handy things about Yii URL router, controllers, and views: URL rules, external actions and controllers, view clips, decorators and more.
  • Configuring URL rules
  • Generating URLs by path
  • Using regular expressions in URL rules
  • Creating URL rules for static pages
  • Providing your own URL rules at runtime
  • Using base controller
  • Using external actions
  • Displaying static pages with CViewAction
  • Using flash messages
  • Using controller context in a view
  • Reusing views with partials
  • Using clips
  • Using decorators
  • Defining multiple layouts
  • Paginating and sorting data
Chapter 3, AJAX and jQuery
Yii’s client side is built with jQuery — the widely used, powerful, and simple to learn JavaScript library. This chapter focuses on Yii-specific tricks rather than jQuery itself.
  • Loading a block through AJAX
  • Managing assets
  • Including resources into the page
  • Working with JSON
  • Passing configuration from PHP to JavaScript
  • Handling variable number of inputs
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.
  • Writing your own validators
  • Uploading files
  • Adding CAPTCHA
  • Customizing CAPTCHA
  • Creating a custom input widget with CInputWidget
Chapter 5, Testing Your Application
This chapter covers both unit testing, functional testing and generating code coverage reports. Recipes are following test driven development approach: you'll write tests for several small applications and then will implement functionality.
  • Setting up the testing environment
  • Writing and running unit tests
  • Using fixtures
  • Testing the application with functional tests
  • Generating code coverage reports
Chapter 6, Database, Active Record, and Model Tricks
In this chapter you’ll learn how to work with database 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.
  • Getting data from a database
  • Defining and using multiple DB connections
  • Using scopes to get models for different languages
  • Processing model fields with AR event-like methods
  • Applying markdown and HTML
  • Highlighting code with Yii
  • Automating timestamps
  • Setting an author automatically
  • Implementing single table inheritance
  • Using CDbCriteria
Chapter 7, Using Zii Components
This chapter 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.
  • Using data providers
  • Using grids
  • Using lists
  • Creating custom grid columns
Chapter 8, Extending Yii
Shows not only how to implement your own Yii extension but how to make your extension reusable and useful for community. Also we’ll focus on many things you should do to make your extension as efficient as possible. This chapter is available for free.
  • Creating model behaviors
  • Creating components
  • Creating reusable controller actions
  • Creating reusable controllers
  • Creating a widget
  • Creating CLI commands
  • Creating filters
  • Creating modules
  • Creating a custom view renderer
  • Making extensions distribution-ready
Chapter 9, Error Handling, Debugging, and Logging
In this chapter we will review logging, analysing exception stack trace and will implement our own error handler.
  • Using different log routes
  • Analyzing the Yii error stack trace
  • Logging and using the context information
  • Implementing your own smart 404 handler
Chapter 10, Security
You’ll learn how to keep your application secure according to general web application security principle “filter input escape output”. We’ll cover such topics as creating your own controller filters, preventing XSS, CSRF and SQL injections, escaping output and using role based access control.
  • Using controller filters
  • Using CHtml and CHtmlPurifier to prevent XSS
  • Preventing SQL injections
  • Preventing CSRF
  • Using RBAC
Chapter 11, Performance Tuning
In this chapter we’ll show how to configure Yii to gain extra performance. Also you’ll learn some best practices of developing an application that will run smooth until you have very high loads.
  • Following best practices
  • Speeding up sessions handling
  • Using cache dependencies and chains
  • Profiling an application with Yii
Chapter 12, Using External Code
Focuses on using third party code with Yii. We'll use Zend Framework, Kohana and PEAR but you'll be able to use any code after learning how it works.
  • Using Zend Framework from Yii
  • Customizing the Yii autoloader
  • Using Kohana inside Yii
  • Using PEAR inside Yii
Chapter 13, Deployment
Covers various tips that are especially useful on application deployment, when developing application in team or when you just want to make your development environment more comfortable.
  • Changing the Yii directories layout
  • Moving an application out of webroot
  • Sharing the framework directory
  • Moving configuration parts into separate files
  • Using multiple configurations to simplify the deployment
  • Implementing and executing cron jobs
  • Maintenance mode