Yii Book - Second Edition

Web Application Development With Yii And PHP


[I'd prefer to watch a video overview...]

The first edition, which at the time was also the only Yii book available, was published back in 2010. The framework has undergone many changes since then, and at this point, this first edition is in need of updating. So, together with PackT publishing, and a great team of technical editors, we set out to provide this update.

This book takes a strong learn-by-doing philosophy to introducing you to the Yii framework. After a brief introduction to the framework in chapters 1 and 2, the remaining 10 chapters are dedicated to building an entire Web application from conception though getting it ready for production. This is a pretty big departure from the common tech book approach, which often provides many disparate, independent examples as different aspects of the technology are covered. In this book, each example builds upon previous ones and is consistently couched in the same application context as we create a unified application. It is intended to mirror a real project development environment that would you normally find yourself in when working on your projects.

The application that we build is an online project-management tool called TrackStar. Trackstar is a user-based application that allows you to create projects and track the progress of issues related to the project as you and your team work to complete the project. We chose this type of application because it is already very familiar to a lot of application developers, and because the features are both simple enough to provide good examples to learn Yii, but rich and sophisticated enough to showcase the true power of the framework. The basic features we develop for our TrackStar application are also very common to many other Web-based applications, so the examples provided translate very easily to many of the requirements you will find yourself having to meet in your real-world projects. Since we build the application together, step-by-step, the examples are related to each other, in the same way features of your real-world projects are. So this approach really helps the reader to immediately understand the benefits of using the Yii framework in a real-world application development scenario.

Chapter-By-Chapter Breakdown

Here is a brief chapter-by-chapter breakdown to better understand what to expect from this book.

Chapter 1 - Meet Yii

Chapter 1 begins by providing a brief history of the framework. We then provide an overview of the Model-View-Controller (MVC) architecture in Yii. Finally we cover the basic user request lifecycle as a request makes it way through a Yii application and back out as a response from the application.

  • History of Yii
  • Model-View-Controller (MVC) in Yii
  • Yii request routing

Chapter 2 - Getting Started

In Chapter 2 the reader dives right into using Yii. We provide the iconic Hello World application example and in the process cover the following topics:

  • Installing the Yii framework
  • Creating a new application shell
  • Using Gii - Yii's code generation tool
  • Linking pages together
  • Adding dynamic content

Chapter 3 - The TrackStar Application

In Chapter 3 we get started on building the TrackStar application, which sets the context for the rest of the book. We outline what this application is and some of the requirements we intend to meet by identifying a number of user stories for the application. In this chapter, you learn the following about Yii:

  • Connecting to a database
  • Using the interactive shell from the command line
  • Unit and functional testing in Yii

Chapter 4 - Project CRUD

In Chapter 4 we begin working on the foundation of our TrackStar application, managing projects. The project is an important entity within this project management application, and we add the ability to "C"reate, "R"ead, "U"pdate and "D"elete (CRUD) these entities within the application. In doing so, you learn the following about Yii:

  • Using database migrations
  • Creating a new model with Gii
  • Active Record in Yii
  • Creating CRUD scaffolding with Gii
  • Working with forms
  • Form field validation

Chapter 5 - Managing Issues

In chapter 5, we being building out the functionality around the issue (or task) entity. TrackStar is an issue tracking application, so we need to add the ability to create, read, update and delete issues as well as assign them to users and track their progress. As we build out these features, you learn the following about Yii:

  • Using filters in Yii
  • Relational Active Record in Yii
  • Using Zii extensions

Chapter 6 - User Management and Authentication

In chapter 6, we flush out the user entity, which is an important part of this user-based application. We change the application to use the database in order to authenticate users and we add the ability to assign users to projects and issues. As we build out these features, you learn the following about Yii:

  • Understanding Yii Authentication
  • Using the Unique, Compare, and Email form field validators
  • Using Behaviors in Yii

Chapter 7 - User Access Control

In chapter 7, we begin to add in authorization controls. We need to be able to control the access to certain functionality on a user-by-user basis. As we build out these features within the application, you learn the following about Yii:

  • Yii's Simple Access Control feature
  • Role-based access control (RBAC) in Yii
  • Creating console commands
  • Using the Query Builder for database access
  • Using direct Data Access Objects (DAO) for database access
  • Creating and using flash messages
  • Using an auto-complete drop-down widget

Chapter 8 - Adding User Comments

With the main application entities in-place, Chapter 8 turns focus to some nice-to-have features. In this chapter we add in the ability for users to leave comments on the project issues. This allows users to be able to have conversations about the projects and issues within the application. As we add the comment feature, you learn the following about Yii:

  • Statistical query relationship
  • Widgets and Yii's portlet model
  • Using named scopes in Yii

Chapter 9 - Adding An RSS Web Feed

In Chapter 9, we outfit our application with the ability to deliver some of its content as a Web feed for other applications to consume. As we add in an RSS data Web feed, you learn the following about Yii:

  • 3rd-party library and framework integration
  • Url management

Chapter 10 - Making It Look Good

In Chapter 10, we give some attention to the front-end of the TrackStar application. We show how to change the layout and design of the application without changing any of the back-end engineering. We also provide some language translation to allow our application to expand to a broader audience. As we make these changes, you learn the following about Yii:

  • Understanding layouts
  • Creating themes
  • Internationalization (i18n) in Yii
  • Localization (l10n) in Yii

Chapter 11 - Using Yii Modules

In Chapter 11, we add in some features that are intended to only be accessed by administrators of the application. We add in the ability for a site administrator to provide application-wide system messages to users. In doing so, we use what is called a module in Yii, which provides a nice way to keep the admin functionality separate from the rest of the application features. As we make these changes, you learn the following about Yii:

  • Understanding and using modules in Yii
  • Using themes within modules

Chapter 12 - Production Readiness

In the final chapter, Chapter 12, we prepare our application for a production environment. We add in a message logging strategy and demonstrate how to handle errors in the application. We also add in some caching of our content in order to improve the performance and user experience. As we prepare the application for production, you learn the following about Yii:

  • Using Yii's logging features
  • Using Yii's caching features
  • Error handling
  • General performance tuning

Additional Resources

In addition to the book, all of the code developed throughout the book is available on a public github repository. You can also view the trackstar application online, and contact Jeff, the author, directly with any issues or questions you have about the book: