Web Application Development with Yii and PHP

Web Application Development with Yii and PHP
eBook: $26.99
Formats: PDF, PacktLib, ePub and Mobi formats
save 15%!
Print + free eBook + free PacktLib access to the book: $71.98    Print cover: $44.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Table of Contents
Sample Chapters
  • A step-by-step guide to creating a modern Web application using PHP, MySQL, and Yii
  • Build a real-world, user-based, database-driven project task management application using the Yii development framework
  • Start with a general idea, and finish with deploying to production, learning everything about Yii inbetween, from "A"ctive record to "Z"ii component library

Book Details

Language : English
Paperback : 332 pages [ 235mm x 191mm ]
Release Date : November 2012
ISBN : 1849518726
ISBN 13 : 9781849518727
Author(s) : Jeffrey Winesett
Topics and Technologies : All Books, Web Development, Open Source, PHP

Table of Contents

Chapter 1: Meet Yii
Chapter 2: Getting Started
Chapter 3: The TrackStar Application
Chapter 4: Project CRUD
Chapter 5: Managing Issues
Chapter 6: User Management and Authentication
Chapter 7: User Access Control
Chapter 8: Adding User Comments
Chapter 9: Adding an RSS Web Feed
Chapter 10: Making It Look Good
Chapter 11: Using Yii Modules
Chapter 12: Production Readiness
  • Chapter 1: Meet Yii
    • Easy
    • Efficient
    • Extensible
    • MVC architecture
      • Model
      • View
      • Controller
    • Stitching these together: Yii request routing
      • Blog posting example
    • Object-relational mapping and Active Record
      • Active Record
    • The view and controller
    • Summary
    • Chapter 2: Getting Started
      • Installing Yii
        • Installing a database
      • Creating a new application
      • "Hello, World!"
        • Creating the controller
          • Configuring Gii
        • One final step
        • Reviewing our request routing
      • Adding dynamic content
        • Adding the date and time
        • A different approach for adding the date and time
          • Moving the data creation to the controller
        • Have you been paying attention?
      • Linking pages together
        • Linking to a new page
        • Getting a little help from Yii CHtml
      • Summary
      • Chapter 3: The TrackStar Application
        • Introducing TrackStar
        • Creating user stories
          • Users
          • Projects
          • Issues
        • Navigation and page flow
        • Data relationships
        • Creating the new application
        • Connecting to the database
          • Yii and databases
        • Testing the database connection
          • Using the interactive shell
          • Automated testing – unit and functional tests
            • Unit and functional testing
          • Benefits of testing
          • Testing in Yii
          • Unit tests
            • Installing PHPUnit
          • Testing the connection
        • Summary
        • Chapter 4: Project CRUD
          • Feature planning
          • Creating the project table
            • Yii database migrations
            • Naming conventions
          • Creating the AR model class
            • Configuring Gii
            • Using Gii to create our Project AR class
          • Enabling CRUD operations for projects
            • Creating CRUD scaffolding for projects
            • Creating a new project
              • Form field validation
            • Reading the project
            • Updating and deleting projects
            • Managing projects in Admin mode
          • Summary
          • Chapter 5: Managing Issues
            • Feature planning
            • Designing the schema
              • Defining some relationships
            • Building objects and their relationships
            • Creating active record model classes
              • Creating the Issue model class
              • Creating the User model class
            • Creating the issue CRUD operations
            • Using the issue CRUD operations
              • Creating a new issue
                • Adding drop-down fields
                • Adding the issue type drop-down
                • Adding the status dropdown: do it yourself
            • Fixing the owner and requester fields
              • Enforcing a project context
                • Defining filters
                • Adding a filter
                • Specifying the filtered actions
                • Adding filter logic
                • Adding the project ID
                • Altering the project details page
              • Returning back to the owner and requester dropdowns
                • Generating the data to populate the dropdowns
                • Making one last change
            • The rest of the CRUD
              • Listing the issues
                • Altering the project controller
                • Altering the project view file
            • Final tweaks
              • Getting the status and type text to display
                • Adding the text display to the form
              • Changing the issue detail view
              • Displaying owner and requester names
                • Using relational AR
              • Making some final navigation tweaks
            • Summary
            • Chapter 6: User Management and Authentication
              • Feature planning
              • User CRUD
                • Updating our common audit history columns
                  • Component behavior
                • Adding a password confirmation field
                • Hash the password
              • Authenticating users using the database
                • Introducing the Yii authentication model
                  • Changing the authenticate implementation
                  • Extending user attributes
                • Displaying the last login time on the home page
              • Summary
              • Chapter 7: User Access Control
                • Feature planning
                • Access control filter
                • Role based access control
                  • Configuring the authorization manager
                  • Creating the RBAC database tables
                  • Creating the RBAC authorization hierarchy
                    • Writing a console application command
                  • Assigning users to roles
                  • Adding RBAC roles to users on a per-project basis
                    • Adding RBAC business rules
                    • Implementing the new project AR methods
                  • Adding users to projects
                    • Altering the project model class
                    • Adding the new form model class
                    • Adding the new action method to the project controller
                    • Adding the new view file to display the form
                    • Putting it all together
                • Checking authorization level
                • Summary
                • Chapter 8: Adding User Comments
                  • Feature planning
                  • Creating the model
                  • Creating the comment CRUD
                  • Altering the scaffolding to meet our requirements
                    • Adding a comment
                    • Displaying the form
                  • Creating a recent comments widget
                    • Introducing CWidget
                      • Named scopes
                      • More on relational AR queries in Yii
                      • Creating the widget
                      • Introducing CPortlet
                  • Summary
                  • Chapter 9: Adding an RSS Web Feed
                    • Feature planning
                    • A little background–content syndication, RSS, and Zend Framework
                    • Installing Zend Framework
                    • Using Zend_Feed
                    • Creating user-friendly URLs
                      • Using the URL manager
                        • Configuring routing rules
                    • Adding the feed links
                    • Summary
                    • Chapter 10: Making It Look Good
                      • Feature planning
                      • Designing with layouts
                        • Specifying a layout
                        • Applying and using a layout
                      • Deconstructing the main.php layout file
                        • Introducing the Blueprint CSS framework
                          • Understanding the Blueprint installation
                        • Setting the page title
                        • Defining a page header
                        • Displaying menu navigation items
                        • Creating a breadcrumb navigation
                        • Specifying the content being decorated by the layout
                        • Defining the footer
                      • Nesting the layouts
                      • Creating themes
                        • Building themes in Yii
                        • Creating a theme
                        • Configuring the application to use a theme
                      • Translating the site to other languages
                        • Defining locale and language
                        • Performing language translation
                          • Performing message translation
                          • Performing file translation
                      • Summary
                      • Chapter 11: Using Yii Modules
                        • Feature planning
                        • Working with modules
                          • Creating a module
                          • Using a module
                          • Module layout
                            • Applying a layout
                        • Restricting admin access
                        • Adding a system-wide message
                          • Creating the database table
                          • Creating our model and CRUD scaffolding
                          • Adding a link to our new functionality
                          • Displaying the message to users
                            • Importing the new model class for application-wide access
                            • Selecting the most recently updated message
                            • Adding a little design tweak
                        • Summary
                        • Chapter 12: Production Readiness
                          • Feature planning
                          • Logging
                            • Message logging
                              • Categories and levels
                              • Adding a login message log
                            • Message routing
                          • Handling errors
                            • Displaying errors
                          • Caching
                            • Cache configuration
                            • Using a file-based cache
                            • Cache dependencies
                            • Query caching
                            • Fragment caching
                              • Declaring fragment caching options
                              • Using fragment cache
                            • Page caching
                          • General performance tuning tips
                            • Using APC
                            • Disabling the debug mode
                            • Using yiilite.php
                            • Using caching techniques
                            • Enabling schema caching
                          • Summary

                          Jeffrey Winesett

                          Jeffrey Winesett has over ten years of experience building large-scale, web-based applications. He has been a strong proponent of using open source development frameworks when developing applications, and a champion of the Yii framework in particular since its initial alpha release. He frequently presents on, writes about, and develops with Yii as often as possible.

                          Sorry, we don't have any reviews for this title yet.

                          Code Downloads

                          Download the code and support files for this book.

                          Submit Errata

                          Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


                          - 9 submitted: last submission 14 Nov 2013

                          Errata type: Code | Page number: 30

                          Towards the bottom of the page, in the section One final step it suggests code for the hello.php view file as:

                          <?php <h1>Hello World!</h1>

                          However, the beginning PHP tag is not needed, just the html snippet:

                          <h1>Hello World!</h1>


                          Errata type: Code | Page number: 37

                          Towards the top of the page, in the section Getting a little help from Yii CHtml it suggests code for the hello.php view file as:


                          However, a comma is missing between the 'Goodbye' and the array(). It should read:

                          CHtml::link('Goodbye', array('message/goodbye'));

                          Errata type: Typo | Page number: 53

                          Small typo close to the bottom of the page. It says:

                          Change the assertEquals(true) statement...

                          But should read

                          Change the assertTrue(true) statement...

                          Errata type: Typo | Page number: 188

                          At the bottom of the page (in the e-book), in the code declaration for adding a foreign key on the comment table, there is a missing ending quote character on the first of the two RESTRICT arguments. It is printed as "RESTRICT, but should be "RESTRICT".

                          The entire line should be: $this->addForeignKey("fk_comment_owner", "tbl_comment", "create_user_id", "tbl_user", "id", "RESTRICT", "RESTRICT");

                          Errata type: Others | Page number: 178-179

                          This is not exactly a book error, but in order for the application to allow a user to create multiple projects as well as assign the same users to multiple projects within the same role, we need to make a small change to the code when associating a user to a role upon project creation. We need wrap the role assignment in an if statement to determine whether or not the user has already been added to the RBAC hierarchy for that role.

                          if(!$auth->isAssigned($this->role, $this->_user->id))
                          $bizRule='return isset($params["project"]) && $params["project"]->allowCurrentUser("'.$this->role.'");';
                          $auth->assign($this->role,$this->_user->id, $bizRule);

                          Errata type: Others | Chapter number: 7

                          After RBAC implementation
                          This is not exactly a book error, but once we change to using our RBAC structure, we need to change our simple access rules, defined in protected/components/Controller.php to use the role rather than name when checking if the user is an admin. So in our allow rule, we need to change:

                          This is correct in the code but was not explicit in the printed code.

                          Errata type: others

                          You can also find the errata page and the associated code bundle at



                          Errata type: Code | Page number: 139

                          The CActiveRecord::beforeValidate() method in the second line of the information box on the page should be:


                          Errata type: Code | Page number: 264

                          change this:
                          yiic migrate create_system_messages_table
                          to this:
                          yiic migrate create create_system_messages_table

                          Sample chapters

                          You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

                          Frequently bought together

                          Web Application Development with Yii and PHP +    Practical Data Analysis =
                          50% Off
                          the second eBook
                          Price for both: £24.65

                          Buy both these recommended eBooks together and get 50% off the cheapest eBook.

                          What you will learn from this book

                          • Auto-generate skeleton web applications as well as CRUD scaffolding for the domain object model using the built-in command-line interface
                          • Use Yii database migrations to version and manage your database changes
                          • Create user-based applications with Yii's authentication model
                          • Enhance your user-based application to control a user's access to feature functionality
                          • Write an administrative console application to extend the features provided by Yii's powerful command-line tools
                          • Integrate other frameworks and libraries into a Yii application to further extend its feature set
                          • Spend less time managing application configuration by taking advantage of Yii's sensible defaults and conventions.
                          • Learn how easy it is to implement a multi-user application by utilizing Yii's authentication and role-based authorization models
                          • Update your code to properly manage the user's last login time and store it in the database

                          In Detail

                          This book is a step by step tutorial in developing a real-world application using the incremental and iterative approach to software development. You learn about agile software development by leaning on the agile nature of the Yii application development framework. You touch on each aspect of the software development lifecycle by building a project task management application from concept through production deployment.

                          After a brief, general introduction to the Yii framework and outlining the software development approach taken throughout the book, the chapters break down in the same way as software development iterations do in real-world projects. After the 1st iteration, you will have a working and tested application with a valid, tested connection to a database.

                          In the 2nd and 3rd iterations, we flesh out our main database entities and domain object model and become familiar with Yii's object-relational-mapping (ORM) layer, Active Record. We also learn how to lean on Yii's auto-generation tools to automatically build our create/read/update/delete (CRUD) functionality against our newly created model. These iterations also focus on how Yii's form validation and submission model works. By the end of the third iteration you will have a working application that allows you to mange projects and issues (tasks) within those projects.

                          The 4th and 5th iterations are dedicated to user management. We learn about the built-in authentication model within Yii to assist in application login and logout functionality. We then dive into the authorization model, first taking advantage of a Yii's simple access control model, then implementing the more sophisticated role-based access control (RBAC) framework that Yii provides.

                          By the end of the 5th iteration, all of the basics of a task management application are in place. The next several iterations are focused on the nice-to-haves. We add user comment functionality, introducing a reusable content portlet architecture approach in the process. We add in an RSS Web feed and demonstrate how easy it is to integrate other third-party tools within a Yii application. We take advantage of Yii's theming structure to help streamline and design the application, and then introduce Yii's internationalization (I18N) features so the application can be adapted to various languages and regions without engineering changes.

                          Finally, we turn our focus to production deployment. We introduce ways to optimize performance and security to prepare the application for a real-world production environment.


                          This is a step-by-step tutorial for developing web applications using Yii. This book follows the test-first, incremental, and iterative approach to software development while developing a project task management application.

                          Who this book is for

                          If you are a PHP programmer with knowledge of object oriented programming and want to rapidly develop modern, sophisticated web applications, then this book is for you. No prior knowledge of Yii is required to read this book.

                          Code Download and Errata
                          Packt Anytime, Anywhere
                          Register Books
                          Print Upgrades
                          eBook Downloads
                          Video Support
                          Contact Us
                          Awards Voting Nominations Previous Winners
                          Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
                          Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software