You are previewing Yii Application Development Cookbook Second Edition.
O'Reilly logo
Yii Application Development Cookbook Second Edition

Book Description

A Cookbook covering both practical Yii application development tips and the most important Yii internal features

  • Learn how to use Yii even more efficiently

  • Full of practically useful solutions and concepts you can use in your application

  • Both important Yii concept descriptions and practical recipes are inside

In Detail

The Yii framework is a rapidly growing PHP5 MVC framework often referred to as Rails for PHP. It has already become a solid base for many exciting web applications such as Stay.com and can be a good base for your developments, too. This book will help you to learn Yii quickly and in more depth for use in for your developments.

"Yii Application Development Cookbook" will show you how to use Yii efficiently. You will learn about taking shortcuts using core features, creating your own reusable code base, using test driven development, and many more topics that will give you a lot of experience in a moderate amount of time.

The second edition fixes all errata found in the first edition and also features new recipes on the client side, HTTP caching, and using Composer with Yii.

The chapters of the book are generally independent and since this book’s goal is to enhance a practical approach to Yii development, you can start reading from the chapter you need most, be it Ajax and jQuery, Database, Active Record, and Model Tricks, or Extending Yii.

"Yii Application Development Cookbook" will help you to learn more about the Yii framework and application development practices in general, showing shortcuts and dangerous things you shouldn’t do.

With all the recipes grouped in 13 chapters, you will write your applications more efficiently using shortcuts and using Yii core functionality in a good way. The most interesting topics are; Yii application deployment, a guide to writing your own extensions, advanced error handling, debugging and logging, application security, performance tuning, and much more.

"Yii Application Development Cookbook" will help you to learn more about the Yii framework and application development practices in general. You will write your applications more efficiently using shortcuts and using Yii core functionality in a good way.

Table of Contents

  1. Yii Application Development Cookbook Second Edition
    1. Table of Contents
    2. Yii Application Development Cookbook Second Edition
    3. Credits
    4. About the Author
    5. About the Reviewers
    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. Under the Hood
      1. Introduction
      2. Using getters and setters
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
      3. Using Yii events
        1. How to do it...
        2. There's more...
          1. Further reading
        3. See also
      4. Using import and autoloading
        1. How to do it...
        2. How it works...
        3. There's more...
      5. Using exceptions
        1. How to do it…
        2. How it works…
        3. There's more...
      6. Configuring components
        1. How to do it…
        2. How it works…
        3. There's more...
      7. Configuring widget defaults
        1. How to do it…
        2. See also
      8. Using Yii core collections
        1. How to do it…
      9. Working with requests
        1. How to do it…
        2. There's more...
    9. 2. Router, Controller, and Views
      1. Introduction
      2. Configuring URL rules
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Further reading
        5. See also
      3. Generating URLs by path
        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. Creating URL rules for static pages
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      6. Providing your own URL rules at runtime
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Using a base controller
        1. Getting ready
        2. How to do it...
        3. How it works...
      8. Using external actions
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more…
      9. Displaying static pages with CViewAction
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Further reading
        5. See also
      10. Using flash messages
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more…
      11. Using the controller context in a view
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more…
      12. Reusing views with partials
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more…
        5. See also
      13. Using clips
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      14. Using decorators
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more…
        5. See also
      15. Defining multiple layouts
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      16. Paginating and sorting data
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more…
    10. 3. AJAX and jQuery
      1. Introduction
      2. Loading a block through AJAX
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Prevent including a bundled jQuery
          2. Further reading
        5. See also
      3. Managing assets
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. What is inside the assets directory
          2. Publishing an entire directory
          3. Further reading
        5. See also
      4. Including resources in the page
        1. How to do it...
        2. How it works...
        3. There's more...
          1. Using custom script packages
          2. Registering linked resources
          3. Registering meta tags
          4. Further reading
        4. See also
      5. Working with JSON
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Passing configuration from PHP to JavaScript
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      7. Handling variable number of inputs
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more…
        5. See also
      8. Rendering content at the client side
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more…
        5. See also
    11. 4. Working with Forms
      1. Introduction
      2. Writing your own validators
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Further reading
      3. Uploading files
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. File validation
          2. Further reading
        5. See also
      4. Adding CAPTCHA
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Further reading:
        5. See also
      5. Customizing CAPTCHA
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Creating a custom input widget with CInputWidget
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    12. 5. Testing Your Application
      1. Introduction
      2. Setting up the testing environment
        1. Getting ready
        2. How to do it...
        3. There's more...
        4. See also
      3. Writing and running unit tests
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Using fixtures
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Testing the application with functional tests
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Generating code coverage reports
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    13. 6. Database, Active Record, and Model Tricks
      1. Introduction
      2. Getting data from a database
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      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
          2. Further reading
          3. See also
      4. Using scopes to get models for different languages
        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. There's more...
          1. Further reading
        5. See also
      6. Applying markdown and HTML
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Markdown syntax
          2. Yii markdown wrapper and usage
        5. See also
      7. Highlighting code with Yii
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Yii code highlighter
          2. More code highlighters
        5. See also
      8. Automating timestamps
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Setting up an author automatically
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      10. Implementing single table inheritance
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      11. Using CDbCriteria
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
    14. 7. Using Zii Components
      1. Introduction
      2. Using data providers
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Further reading
        5. See also
      3. Using grids
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using data from related Active Record models
          2. Further reading
        5. See also
      4. Using lists
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Adding sorting
          2. Customizing templates
          3. Customizing markup and data displayed
          4. Further reading
        5. See also
      5. Creating custom grid columns
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    15. 8. Extending Yii
      1. Introduction
      2. Creating model behaviors
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. CActiveRecordBehavior and CModelBehavior
          2. More behavior types
          3. Further reading
        5. See also
      3. Creating components
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Overriding existing application components
          2. Further reading
        5. See also
      4. Creating reusable controller actions
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Further reading
        5. See also
      5. Creating reusable controllers
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Creating a widget
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Creating CLI commands
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Further reading
        5. See also
      8. Creating filters
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Creating modules
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      10. Creating a custom view renderer
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Further reading
        5. See also
      11. Making extensions distribution-ready
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
    16. 9. Error Handling, Debugging, and Logging
      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::log
          2. Yii::beginProfile and Yii::endProfile
          3. Log messages immediately
          4. Further reading
        5. See also
      3. Analyzing the Yii error stack trace
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Logging and using the context information
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Implementing your own smart 404 handler
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Further reading
        5. See also
    17. 10. Security
      1. Introduction
      2. Using controller filters
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Using CHtml and CHtmlPurifier to prevent XSS
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. XSS types
          2. Configuring the HTML Purifier
          3. HTML Purifier performance
          4. Further reading
        5. See also
      4. Preventing SQL injections
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Preventing CSRF
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Extra measures
          2. Using GET and POST properly
          3. Further reading
        5. See also
      6. Using RBAC
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Naming RBAC nodes
          2. A way to keep the hierarchy simple and efficient
          3. Avoiding RBAC
          4. Further reading
        5. See also
    18. 11. Performance Tuning
      1. Introduction
      2. Following best practices
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Server-side performance is just a part of the big picture
          2. Things to be done without using Yii
          3. Active Record versus query builder and SQL
          4. Always check for slow queries first
          5. Cache or save results of heavy processes
          6. Handling too much processing
          7. Further reading
        5. See also
      3. Speeding up session handling
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Using cache dependencies and chains
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Profiling an application with Yii
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Leveraging HTTP caching
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Optimizing filters
          2. Further reading
        5. See also
    19. 12. Using External Code
      1. Introduction
      2. Using Zend Framework from Yii
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Customizing the Yii autoloader
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Further reading
        5. See also
      4. Using Kohana inside Yii
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Further reading
        5. See also
      5. Using PEAR inside Yii
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Using Composer with Yii
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    20. 13. Deployment
      1. Introduction
      2. Changing the Yii directory layout
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Moving an application out of webroot
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Sharing the framework directory
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Moving configuration parts into separate files
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Using multiple configurations to simplify the deployment
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Implementing and executing cron jobs
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Further reading
        5. See also
      8. Maintenance mode
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    21. Index