0%

Book Description

Discover 100 useful recipes that will bring the best out of the Yii2 framework and be on the bleeding edge of web development today

About This Book

  • Learn how to use Yii2 efficiently through clear examples and core features, and see how to use tests, create reusable code snippets, core widgets, deployment, and more
  • This book provides you with a wide space for practice approaches and helps you to learn about the new Yii2 framework.
  • Understand the difference between the Yii 1.x.x versions using useful examples from real web applications

Who This Book Is For

This book is for developers with good PHP5 knowledge and MVC-frameworks who have tried to develop applications using the Yii 1.x.x version. This book will very useful for all those who would like to try Yii2, or those who are afraid to move from Yii 1.x.x. to Yii2. If you have still not tried Yii2, this book is definitely for you!

What You Will Learn

  • See the new version of the Yii2 framework and application development practices
  • Write your applications more efficiently using shortcuts, Yii's core functionality, and the best practices in Web 2.0
  • Get data from a database, and deal with Active Record, migrations, widgets, and core features
  • Easily update your skills from the previous version of the framework
  • Explore how to use Yii with different JavaScript frameworks and libraries such as WebSockets, Angular, Ember, Backbone and React
  • Learn how to keep your application secure according to the general web application security principle "filter input, escape output.
  • Write RESTfull Web Services using Yii2 and built-in features
  • Correctly install and use official extensions in your projects
  • Effectively create and implement your own Yii extension, and also ensure your extension is reusable and useful for the community

In Detail

Yii is a free, open source web application development framework written in PHP5 that promotes clean DRY design and encourages rapid development. It works to streamline your application development time and helps to ensure an extremely efficient, extensible, and maintainable end product. Being extremely performance optimized, Yii is a perfect choice for any size project. However, it has been built with sophisticated, enterprise applications in mind. You have full control over the configuration from head-to-toe (presentation-to-persistence) to conform to your enterprise development guidelines. It comes packaged with tools to help test and debug your application, and has clear and comprehensive documentation.

This book is a collection of Yii2 recipes. Each recipe is represented as a full and independent item, which showcases solutions from real web-applications. So you can easily reproduce them in your environment and learn Yii2 fast and without tears. All recipes are explained with step-by-step code examples and clear screenshots.

Yii2 is like a suit that looks great off the rack, but is also very easy to tailor to fit your needs. Virtually every component of the framework is extensible. This book will show how to use official extensions, extend any component, or write a new one.

This book will help you create modern web applications quickly, and make sure they perform well using examples and business logic from real life. You will deal with the Yii command line, migrations, and assets. You will learn about role-based access, security, and deployment. We'll show you how to easily get started, configure your environment, and be ready to write web applications efficiently and quickly.

Style and approach

The chapters of this book are independent and offer a practical approach to Yii development. You can start reading from the chapter you need most, be it Ajax and jQuery, RESTful Web Services, Database, Active Record, or Official Extensions

Table of Contents

  1. Yii2 Application Development Cookbook Third Edition
    1. Table of Contents
    2. Yii2 Application Development Cookbook Third Edition
    3. Credits
    4. About the Authors
    5. About the Reviewer
    6. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why Subscribe?
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Sections
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      5. Conventions
      6. Reader feedback
      7. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Fundamentals
      1. Introduction
      2. Installing the framework
        1. Getting ready
        2. How to do it…
          1. Installing a basic project template
          2. Installing advanced project template
        3. How it works…
        4. See also
      3. Application templates
        1. How to do it…
        2. How it works…
      4. Dependency injection container
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      5. Service locator
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      6. Code generation
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. Configuring components
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. Built-in components
        4. See also
      8. Working with events
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      9. Using external code
        1. Getting ready
        2. How to do it…

          1. Installing a library via Composer
          2. Installing libraries manually
          3. Using Yii2 code in other frameworks
        3. How it works…
        4. See also
    9. 2. Routing, Controllers, and Views
      1. Introduction
      2. Configuring URL rules
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      3. Generating URLs
        1. Getting ready
        2. How to do it…
        3. How it works...
        4. There's more...
        5. See also
      4. Using regular expressions in URL rules
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      5. Using a base controller
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      6. Using standalone actions
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      7. Creating a custom filter
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      8. Displaying static pages
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. About ViewAction
          2. Configuring URL rules
        5. See also
      9. Using flash messages
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. The getAllFlashes() method
          2. The removeAllFlashes() method
          3. The removeFlash() method
        5. See also
      10. Using the controller context in a view
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      11. Reusing views with partials
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      12. Using blocks
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      13. Using decorators
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      14. Defining multiple layouts
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      15. Pagination and sorting data
        1. Getting ready
        2. How to do it…
        3. How it works...
        4. See also
    10. 3. ActiveRecord, Model, and Database
      1. Introduction
      2. Getting data from a database
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more...
      3. Defining and using multiple DB connections
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Cross-database relations
        5. See also
      4. Customizing the ActiveQuery class
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more…
        5. See also
      5. Processing model fields with AR event-like methods
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      6. Automating timestamps
        1. Getting ready
        2. How to do it...
        3. How it works…
        4. There's more…
          1. In addition...
        5. See also
      7. Setting up an author automatically
        1. Getting ready
        2. How to do it…
        3. How it works...
        4. There's more…
        5. See also
      8. Setting up a slug automatically
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      9. Transactions
        1. Getting ready...
        2. How to do it…
        3. See also
      10. Replication and read-write splitting
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      11. Implementing single table inheritance
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
    11. 4. Forms
      1. Introduction
      2. Writing your own validators
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      3. Uploading files
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Adding and customizing CaptchaWidget
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      5. Customizing Captcha
        1. Getting ready
        2. How to do it...
        3. See also
      6. Creating a custom input widget
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Tabular input
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      8. Conditional validation
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      9. Complex forms with multiple models
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      10. AJAX-dependent drop-down list
        1. Getting ready
        2. How to do it...
        3. How it works...
      11. AJAX validation
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      12. Creating a custom client-side validation
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    12. 5. Security
      1. Introduction
      2. Authentication
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      3. Using controller filters
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      4. Preventing XSS
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more…
          1. XSS types
        5. See also
      5. Preventing SQL injections
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      6. Preventing CSRF
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Disabling CSRF-tokens for a specific action
          2. CSRF validation for Ajax-calls
          3. Additionally [rename]
          4. Using GET and POST properly
        5. See also
      7. Using RBAC
        1. Getting ready
        2. How to do it...
        3. How it works…
        4. There's more…
          1. Keeping hierarchy simple and efficient
          2. Naming RBAC nodes
        5. See also
      8. Encrypting/Decrypting data
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more…
          1. Working with passwords
        5. See also
    13. 6. RESTful Web Services
      1. Introduction
      2. Creating a REST server
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Content negotiation
          2. Customizing the Rest URL rule
        5. See also
      3. Authentication
        1. Getting ready
        2. How to do it...
        3. How it works…
        4. There's more…
        5. See also
      4. Rate limiting
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      5. Versioning
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      6. Error handling
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
    14. 7. Official Extensions
      1. Introduction
      2. Authentication client
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      3. SwiftMailer e-mail library
        1. Getting ready
        2. How to do it…
          1. Sending plain text e-mails
          2. Sending HTML content
          3. Working with SMTP transport
          4. Attaching file and embedding images
        3. How it works…
        4. See also
      4. Faker fixture data generator
        1. Getting ready
        2. How to do it…
          1. Working with your own data types
        3. How it works…
        4. See also
      5. Imagine library
        1. Getting ready
        2. How to do it…
          1. Using it as a factory
          2. Using inner methods
        3. How it works…
        4. See also
      6. MongoDB driver
        1. Getting ready
        2. How to do it…
          1. Basic usage
        3. How it works…
        4. See also
      7. ElasticSearch engine adapter
        1. Getting ready
        2. How to do it…
          1. Using the Query class
          2. Using ActiveRecord
          3. Using the ElasticSearch DebugPanel
        3. How it works…
        4. See also
      8. Gii code generator
        1. Getting ready
        2. How to do it…
          1. Working with GUI
          2. Working with CLI
        3. How it works…
        4. See also
      9. Pjax jQuery plugin
        1. Getting ready
        2. How to do it…
          1. Specifying a custom ID
          2. Using ActiveForm
          3. Working with the client-side script
        3. How it works…
        4. See also
      10. Redis database driver
        1. Getting ready
        2. How to do it…
          1. Direct usage
          2. Using ActiveRecord
        3. How it works…
        4. See also
    15. 8. Extending Yii
      1. Introduction
      2. Creating helpers
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      3. Creating model behaviors
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      4. Creating components
        1. Getting ready
        2. How to do it…
          1. Overriding existing application components
        3. How it works…
        4. See also
      5. Creating reusable controller actions
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      6. Creating reusable controllers
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      7. Creating a widget
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      8. Creating CLI commands
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      9. Creating filters
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      10. Creating modules
        1. Getting ready
        2. How to do it…
        3. How it works...
        4. See also
      11. Creating a custom view renderer
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      12. Creating a multilanguage application
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      13. Making extensions distribution-ready
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
    16. 9. Performance Tuning
      1. Following best practices
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      2. Speeding up session handling
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      3. Using cache dependencies and chains
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      4. Profiling an application with Yii
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      5. Leveraging HTTP caching
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. Last-Modified
          2. Entity Tag
        4. See also
      6. Combining and minimizing assets
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      7. Running Yii2 on HHVM
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
    17. 10. Deployment
      1. Introduction
      2. Changing the Yii directory layout
        1. Getting ready
        2. How to do it...
          1. Changing the location of the runtime directory
          2. Changing the location of the vendor directory
          3. Changing the location of the controllers
          4. Changing the locations of the views directory
        3. How it works...
        4. See also
      3. Moving an application webroot
        1. Getting ready
        2. How to do it...
          1. Placing files in the root
          2. Placing files in a subdirectory
        3. How it works...
        4. See also
      4. Changing an advanced application template
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      5. Moving configuration parts into separate files
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      6. Using multiple configurations to simplify the deployment
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      7. Implementing and executing cron jobs
        1. Getting ready
        2. How to do it...
          1. Running the Hello command
          2. Creating your own command
          3. Setting the cron schedule
        3. How it works...
        4. See also
      8. Maintenance mode
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      9. Deployment tools
        1. Getting ready
        2. How to do it...
          1. Step 1 - Preparing the remote host
          2. Step 2 - Preparing the localhost
          3. Step 3 - Adding remote configuration
          4. Step 4 - Trying to deploy
        3. How it works...
        4. See also
    18. 11. Testing
      1. Introduction
      2. Testing application with Codeception
        1. Getting ready
        2. How to do it…
          1. Preparing for the tests
          2. Running unit and functional tests
          3. Getting coverage reports
          4. Running acceptance tests
          5. Creating database fixtures
          6. Writing unit or integration test
          7. Writing functional test
          8. Writing acceptance test
          9. Creating API test suite
        3. How it works…
        4. See also
      3. Unit testing with PHPUnit
        1. Getting ready
        2. How to do it…
          1. Preparing extension structure
          2. Writing extension code
          3. Writing extension tests
          4. Running tests
          5. Analyzing code coverage
          6. Usage of component
        3. How it works…
        4. See also
      4. Unit testing with Atoum
        1. Getting ready
        2. How to do it…
          1. Preparing the extension structure
          2. Writing the extension code
          3. Writing the extension tests
          4. Running tests
          5. Analyzing code coverage
        3. How it works…
        4. See also
      5. Unit testing with Behat
        1. Getting ready
        2. How to do it…
          1. Preparing extension structure
          2. Writing extension code
          3. Writing extension tests
          4. Running tests
        3. How it works…
        4. See also
    19. 12. Debugging, Logging, and Error Handling
      1. Introduction
      2. Using different log routes
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more…
          1. Yii::trace versus Yii::getLogger()->log
          2. Yii::beginProfile and Yii::endProfile
          3. Log messages immediately
        5. See also
      3. Analyzing the Yii error stack trace
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      4. Logging and using the context information
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      5. Displaying custom errors
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      6. Custom panel for debug extension
        1. Getting ready
        2. How to do it...
        3. How it works...
          1. Handling events
        4. See also
    20. Index
35.153.134.169