You are previewing Web Application Development with Yii 2 and PHP.
O'Reilly logo
Web Application Development with Yii 2 and PHP

Book Description

Fast-track your web application development using the new generation Yii PHP framework

In Detail

Yii is a high performance PHP framework used for rapid web application development. It is well designed, well supported, easy to learn, and easy to maintain. This book embraces the learn-by-example methodology to show you the most important features of the Yii 2 framework. Throughout the course of this book, you will build a simple real-world application; each chapter will introduce you to a new functionality and show you how to tweak your application. Instead of trying to be an all-encompassing reference about the framework, this is a walkthrough of the really important pieces of information that you have to understand in detail.

You will learn how to use Yii's active record and CRUD scaffolding to manage the data in your database. Authentication, extensions, events and behaviors, and route management are just some of the many other features of Yii that you will learn from this book. By the end of this book, you will have a basic CRM application that is all set for service!

What You Will Learn

  • Build a complete application boilerplate with a single shell command and transform it into a running website with another
  • Generate code for routine CRUD interface pages
  • Make a vile extension just for fun and learn how to attach it to a target application
  • Control and customize the output that will be sent to the client browser
  • Authenticate users using a classic password-based technique and authorize them using role-based access control
  • Prepare your application for multiple users and deployment to a production environment
  • Answer mind-boggling SEO feature requests by modifying the route handling mechanism
  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

    Table of Contents

    1. Web Application Development with Yii 2 and PHP
      1. Table of Contents
      2. Web Application Development with Yii 2 and PHP
      3. Credits
      4. About the Authors
      5. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. Who this book is for
        3. What you need for this book
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started
        1. A basic application
          1. Installation of a basic application template
          2. Specifics of the basic application template
        2. An advanced application
          1. Installation of an advanced application template
          2. Specifics of the advanced application template
        3. Summary
      9. 2. Making a Custom Application with Yii 2
        1. The design stage
          1. Task at hand
          2. Domain model design
          3. Target feature
        2. Initial preparations
          1. Setting up project management
          2. Setting up the testing harness
          3. Setting up the deployment pipeline
            1. Making a web application entry point visible
        3. Introducing the Yii framework into our application
          1. First end-to-end test
          2. Yii 2 installation to the bare code base
            1. Checking the requirements
          3. An introduction to Yii conventions
          4. Building the wireframe code
          5. Adding a controller
          6. Handling possible errors
        4. Making the data and application layers
          1. Defining the customer model at the data layer
          2. Setting up the database
          3. Object-relation mapping in Yii
          4. Decoupling from ORM
        5. Creating the user interface
          1. The Add New Customer UI
          2. Routing 101
          3. Layouts
          4. Finishing the Add New Customer UI
          5. Widgets
          6. The List Customers UI
          7. Customer Query UI
        6. Using the application
        7. Summary
      10. 3. Automatically Generating the CRUD Code
        1. Definition of the model to work with
        2. Using Gii
          1. Installing Gii into the application
          2. Generating the code for the Model class
          3. Generating the CRUD code
        3. Finishing touches
          1. Creating a new layout to support pages generated by Gii
        4. An overview of the generated CRUD UI
        5. Pros and cons of generated classes over manually created ones
        6. Summary
      11. 4. The Renderer
        1. Anatomy of Yii rendering
        2. The Yii application components
        3. The View component
          1. Algorithm to find the view files
          2. Algorithm to search the layout file to be used
          3. The internal workings of rendering the view file
        4. Custom renderers
        5. A custom response formatter
        6. The asset bundles
          1. An asset bundle with files from an arbitrary folder
          2. Asset publishing
          3. An asset bundle with files from a web-accessible folder
          4. Registering CSS and JavaScript files manually
          5. Placing JavaScript in different positions in the asset bundles
          6. Making a custom asset bundle for our application
        7. Themes
          1. Making a custom snowy theme
        8. Widgets
        9. Summary
      12. 5. User Authentication
        1. Anatomy of the user login in Yii
        2. Password-based login mechanics in general
        3. Making the user management interface
          1. Acceptance tests for the user management interface
          2. Database table to store user records
          3. Generating the model and CRUD code by Gii
          4. Removing the password field from the autogenerated code
        4. Hashing a password upon saving a user record
          1. Functional tests for password hashing
          2. Password hashing implementation inside the active record
        5. Making a user record into an identity
        6. Making the login interface
          1. Specifications of user authentication
          2. Making the authentication indicator
          3. The login form functionality
          4. The logout functionality and wrapping things up
        7. Summary
      13. 6. User Authorization and Access Control
        1. Access control using the state of user authentication
          1. FEATURE – hook methods of the controller
          2. FEATURE – exception handling in Yii
          3. FEATURE – controller action filters
        2. Role-based access control
        3. Protecting the CRM management from CRM users
          1. Installing predefined users
          2. RBAC managers in Yii
          3. The failing test for our role hierarchy
          4. Setting up the role hierarchy
          5. The failing test for access control in controllers
          6. FEATURE – access control filter
          7. Applying access control to the site
        4. Summary
      14. 7. Modules
        1. FEATURE – Yii modules
          1. The informal concept of reachability
          2. Exploring the intricacies of module configuration through simple examples
          3. The Debug module
        2. Building the API module
          1. Building a test suite to support testing the API module
          2. Defining the requirements for automatic tests of API modules
          3. Moving the controller actions to a separate module
        3. Retrospective on the modules mentioned in previous chapters
        4. Summary
      15. 8. Overall Behavior
        1. FEATURE – message log
          1. Actually storing the log messages
            1. Setting up the e-mailing component so that log messages can be mailed
          2. Reading the stored log messages
        2. FEATURE – profiling
        3. Error handling details
          1. FEATURE – error handling controller action
          2. List of built-in exceptions
        4. Caching
          1. FEATURE – cache component
          2. FEATURE – database queries caching
          3. FEATURE – page fragment caching
          4. FEATURE – whole page caching
          5. FEATURE – caching the request by HTTP headers
        5. Minimizing the assets
        6. Summary
      16. 9. Making an Extension
        1. Extension idea
        2. Creating the extension contents
          1. Preparing the boilerplate code for the extension
          2. FEATURE – bootstrapping
          3. FEATURE – extension registering
          4. Making the bootstrap for our extension – hideous attachment of a controller
        3. Making the extension installable as... erm, extension
          1. Preparing the correct composer.json manifest
          2. Configuring the repositories
        4. Summary
      17. 10. Events and Behaviors
        1. Automatically marking database records with the timestamp and user ID
          1. Test case for customer creation
          2. Test case for updating customer updates
            1. Preparing the database fields
          3. Using the timestamp and blameable behaviors
        2. FEATURE – behaviors
        3. FEATURE – events
        4. Built-in events
          1. Events of \yii\base\Application
          2. Events of \yii\base\Controller
          3. Events of \yii\base\Module
          4. Events of \yii\base\View
          5. Events of \yii\web\View
          6. Events of \yii\base\Model
          7. Events of \yii\db\BaseActiveRecord
          8. Events of \yii\db\Connection
          9. Events of \yii\web\Response
          10. Events of \yii\web\User
          11. Events of \yii\mail\BaseMailer
        5. Summary
      18. 11. The Grid
        1. Dismissing of the domain layer
        2. Designing for the customers' index
          1. Making address, e-mail, and phone active records
          2. Making the common base controller for submodels
          3. Making relations from customer to address, e-mail address, and phone
        3. FEATURE – widgets
        4. Creating the index page for customers
          1. Making a base GridView for customers
          2. Changing the format of the column content
          3. FEATURE – formatter
          4. Making the custom GridView column for the customer audit info
          5. Compressing submodels related to customers into single columns
          6. FEATURE – GridView columns
          7. Implementing filtering inside GridView of customers
          8. Implementing sorting inside GridView of customers
        5. Summary
      19. 12. Route Management
        1. Yii 2 routing 102
        2. FEATURE – routing using names of modules, controllers, and actions
          1. Fundamental rules of URL management in Yii 2
          2. FEATURE – creating URLs in Yii
        3. Custom routes using a configuration
          1. FEATURE – URL rules
        4. Custom routes using custom URL rule classes
        5. Summary
      20. 13. Collaborative Work
        1. Configuration construction
          1. Adding local overrides to the configuration
        2. Console application
          1. Custom console commands
        3. Database migrations
          1. Making custom templates for database migrations
        4. Summary
      21. A. Deployment Setup with Vagrant
        1. Planning
        2. Initial setup
        3. Fine-tuning the virtual machine
          1. Preparing the guest OS
          2. Preparing the database and web server
          3. Preparing the application
        4. Using the virtual machine as a local deploy target
      22. B. The Active Form Primer
        1. Making the Edit form for customer
          1. Active query
          2. Customizing the autogenerated form
          3. Passing the customer ID to submodels
          4. Returning to the Update Customer form after updating the submodel
          5. Custom column value for the addresses table
      23. Index