You are previewing Mastering Yii.
O'Reilly logo
Mastering Yii

Book Description

Advance your modern web application development skills with Yii Framework 2

About This Book

  • Learn to work with the key aspects of Yii Framework 2

  • Explore how to create RESTful APIs with Yii

  • Incorporate codeception with Yii2 to test your code thoroughly

  • Who This Book Is For

    This book is for Yii Framework developers who want to quickly master Yii2. This book assumes some familiarity with Yii2, PHP 5, and HTML5.

    What You Will Learn

  • Explore Yii2’s conventions and learn how to properly configure Yii2

  • Create both web and console applications

  • Reduce development time by learning to create classes automatically with Gii, Yii2’s automatic code generation tool

  • Use Yii2’s database migration tool

  • Manage and access databases with Active Record, DAO, and Query Builder

  • Handle user authentication and authorization within Yii2

  • Create RESTful APIs with Yii Framework 2

  • Test applications automatically with codeception

  • In Detail

    The successor of Yii Framework 1.1, Yii2 is a complete rewrite of Yii Framework, one of the most popular PHP 5 frameworks for making modern web applications. The update embraces the best practices and protocols established with newer versions of PHP, while still maintaining the simple, fast, and extendable behavior found in its predecessor.

    This book has been written to enhance your skills and knowledge with Yii Framework 2. Starting with configuration and how to initialize new projects, you’ll learn how to configure, manage, and use every aspect of Yii2 from Gii, DAO, Query Builder, Active Record, and migrations, to asset manager. You'll also discover how to automatically test your code using codeception.

    With this book by your side, you’ll have all the skills you need to quickly create rich modern web and console applications with Yii2.

    Style and approach

    This book is a step-by-step guide to mastering every aspect of Yii Framework 2. Each chapter outlines a new set of tools along with code that can be used to efficiently create modern web and console applications

    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 http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Mastering Yii
      1. Table of Contents
      2. Mastering Yii
      3. Credits
      4. About the Author
      5. About the Reviewer
      6. www.PacktPub.com
        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. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Composer, Configuration, Classes, and Path Aliases
        1. Composer
        2. Configuration
          1. Requirements checker
          2. Entry scripts
            1. Web entry script
          3. Configuration files
            1. Web and console configuration files
            2. Database configuration
            3. Parameter configuration
            4. Environment configuration
          4. Setting up our application environment
          5. Setting the web environment for NGINX
          6. Setting the web environment for Apache
        3. Components and objects
          1. Components
          2. Objects
        4. Path aliases
        5. Summary
      9. 2. Console Commands and Applications
        1. Configuration and usage
          1. Entry script
          2. Configuration
          3. Setting the console environment
          4. Running console commands
        2. Built-in console commands
          1. The help command
          2. The asset command
          3. The cache command
          4. The fixture command
          5. The Gii command
          6. The message command
          7. The migration command
        3. Creating console commands
          1. Generating help information
          2. Passing command-line arguments
          3. Exit codes
          4. Formatting
        4. Summary
      10. 3. Migrations, DAO, and Query Building
        1. Connecting to databases
          1. Additional configuration options
        2. Writing database migrations
          1. An overview of schema
          2. Writing migrations
          3. Running migrations
          4. Altering a database schema
        3. Database access objects
          1. Querying for data
            1. Quoting table and column names
          2. Executing queries
          3. Parameter binding
          4. Transactions
        4. Query Builder
          1. Query construction methods
          2. The select method
            1. The from method
            2. The where method
              1. The string format
              2. The hash format
              3. The operator format
            3. Ordering results
            4. Limiting and offsetting data
            5. Grouping and having
            6. Joins and unions
            7. Executing queries
            8. Examining queries
            9. Iterating over query results
        5. Data providers and data widgets
        6. Data replication and load balancing
        7. Summary
      11. 4. Active Record, Models, and Forms
        1. Configuring Gii
          1. Gii for web applications
          2. Gii for console applications
        2. Active Record
          1. The Active Record pattern
          2. Creating Active Record classes
            1. Creating active record classes with Gii
              1. Using Gii's web interface
              2. Using Gii's console interface
          3. Working with Active Record
            1. Model validation rules
            2. Adding custom validators
            3. Customizing validator error messages
            4. Working with validation errors
            5. Manually executing validation rules
            6. Model attribute labels
            7. Active Record relationships
            8. Using multiple database connections with Active Record
            9. Behaviors in Active Record
          4. Working with Active Record
            1. Querying data
              1. Data access
            2. Saving data
              1. Creating new records
            3. Deleting data
            4. Active Record events
        3. Models
          1. Model attributes
            1. Scenarios
        4. Forms
          1. Generating forms with Gii
            1. Generating forms with Gii's web interface
            2. Generating forms with Gii's console interface
          2. Using forms
            1. ActiveForm and input types
        5. Summary
      12. 5. Modules, Widgets, and Helpers
        1. Modules
          1. Module components
            1. The module class structure
            2. Controllers
            3. Views and layouts
            4. Registering modules
              1. Dynamically registering modules
              2. Bootstrapping modules
          2. Accessing modules
          3. Managing modules with Composer
          4. Modules in summary
        2. Widgets
          1. Using widgets
          2. Commonly used built-in widgets
            1. Bootstrap widgets
            2. jQuery UI widgets
            3. Yii-specific widgets
          3. Creating custom widgets
          4. A summary of widgets
        3. Helpers
          1. The URL helper
          2. The HTML helper
          3. The JSON helper
          4. The Markdown helper
          5. Variable dumping
          6. Inflector
          7. FileHelper
        4. Summary
      13. 6. Asset Management
        1. Asset bundles
          1. Using asset bundles
          2. Configuration
            1. Asset mapping
          3. Asset types and locations
          4. Asset options
          5. Asset publication
          6. Client cache management with asset bundles
          7. Using preprocessor with asset bundles
          8. The asset command line tool
        2. Third-party asset tools
          1. NodeJS
          2. Bower
          3. Grunt
        3. Summary
      14. 7. Authenticating and Authorizing Users
        1. Authentication of users
          1. Implementing the user identity interface
            1. Cookie-based authentication
            2. Working with user identities
          2. Authenticating users with forms
        2. Authorization
          1. Access control filters
          2. Role-based access control
          3. Configuring RBAC
          4. Creating permissions and permission relationships
          5. Custom authorization rules
          6. Checking if a user has access to a role
        3. Flash messages
        4. Hashing and encryption
          1. Hashing and verifying passwords
          2. Data encryption and decryption
          3. Data hashing
        5. Summary
      15. 8. Routing, Responses, and Events
        1. Routing
          1. Default and catch all routes
          2. Custom routes and URL rules
            1. Parameterizing routes
            2. URL suffixes
            3. HTTP method-specific URL rules
          3. Custom URL rule classes
          4. Dynamic rule generation
        2. Requests
          1. Retrieving request parameters and data
          2. Request headers and cookies
          3. Retrieving client and URL information
        3. Responses
          1. Setting status codes
            1. Web exceptions
          2. Setting response headers
          3. The response body
          4. Redirection
          5. The file output
        4. Events
          1. Event handlers
          2. Triggering events
          3. Class-level events
          4. Global events
        5. Summary
      16. 9. RESTful APIs
        1. ActiveController
          1. Configuring ActiveController display fields
          2. Data serialization within responses
          3. Disabling ActiveController actions
          4. Customizing ActiveController actions
        2. Authentication filters
          1. HTTP basic authentication
          2. Query parameter authentication
          3. OAuth2 authentication
          4. Composite authentication
          5. Custom authentication filters
          6. Action-specific authentication
          7. Checking access
        3. Verb filters
        4. Cross-origin resource headers
        5. Rate Limiting
        6. Error handling
        7. Custom API controllers
          1. Returning data
          2. Response Formatting
        8. Summary
      17. 10. Testing with Codeception
        1. Reasons for testing
          1. How to approach testing
            1. Testing manually
          2. Testing a few core components
            1. Test-driven development
        2. Configuring Codeception with Yii2
        3. Unit testing
          1. Generating unit tests
          2. Unit test examples
            1. Testing User model methods
        4. Functional testing
          1. Setting up functional tests
          2. Generating functional tests
          3. Examples of functional tests
        5. Acceptance testing
          1. Setting up acceptance testing
          2. Examples of acceptance tests
        6. Fixtures
          1. Creating fixtures
          2. Defining fixtures
          3. Using fixtures in unit tests
        7. Automatic change testing
        8. Summary
      18. 11. Internationalization and Localization
        1. Configuring Yii2 and PHP
          1. The intl extension
        2. The application language
          1. Programmatically setting the application language
          2. Dynamically setting the application language
        3. Message translations
          1. Message sources
          2. Default translations
          3. Framework messages
          4. Handling missing translations
          5. Generating message files
          6. Message formatting
        4. Viewing file translations
        5. Module translations
        6. Widget translations
        7. Summary
      19. 12. Performance and Security
        1. Caching
          1. Caching data
            1. Caching dependencies
            2. Database query caching
          2. Fragment caching
          3. Page caching
          4. HTTP caching
          5. Caching database schema
        2. General performance enhancements
          1. Enabling OPCache
          2. Optimizing Composer dependencies
          3. Upgrading to PHP 7
          4. Switch to Facebook's HHVM
        3. Security considerations
          1. Certificates
          2. Cookies
          3. Protecting against cross-site scripting
          4. Enabling cross-site request forgery protection
        4. Summary
      20. 13. Debugging and Deploying
        1. Debugging
          1. Logging
          2. Benchmarking
          3. Error handling
            1. Handling errors within non HTML responses
          4. Debugging with the Yii2 debug extension
        2. Deploying
        3. Summary
      21. Index