You are previewing Zend Framework 2 Cookbook.
O'Reilly logo
Zend Framework 2 Cookbook

Book Description

A guide to all the ins and outs of Zend Framework 2 features

  • Recipes to help you create, test, and optimize your applications

  • A useful guide for PHP developers wanting to broaden their horizons

  • Helps you understand how MVC works and how to make it work for you

In Detail

Zend Framework 2 is the latest creation of World Wide Web infrastructure company Zend Technologies Ltd. This new PHP framework comes with tons of features and an attractive way of creating applications. Not only is the overall usability of the technology much better, but it also makes your applications more testable, something that is often overlooked.

"Zend Framework 2 Cookbook" will show you how applications are set up in Zend Framework 2 and how you can develop successfully in this massive framework. You will master features like Modules, Views, Controllers, and Authentication. The book also discusses the Event Manager, unit testing, and how to optimize your application.

The book begins with a discussion about setting up Zend Framework 2, and you will also look at how the framework itself works. By the end of this book, you will be able to create entire secure applications on your own and make sure they are tested and optimized for performance as well.

You will learn about sending and receiving e-mails, translation and localization of the application, and how to set up the framework on a Linux web server. You will also learn how to display data from the application to the user by using different display strategies and renderings. The creation of modules will also be discussed. Then, you will move on to look at how to authenticate users and make sure the developer knows how to pick the best method available. Unit testing, debugging, and enhancing the performance will also be covered in this book.

"Zend Framework 2 Cookbook" is a perfect book for anyone who wants to start developing with Zend Framework 2.

Table of Contents

  1. Zend Framework 2 Cookbook
    1. Table of Contents
    2. Zend Framework 2 Cookbook
    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. Zend Framework 2 Basics
      1. Introduction
      2. Setting up a Zend Framework 2 project
        1. Getting ready
        2. How to do it…
          1. Cloning the skeleton
          2. Moving the skeleton
          3. Initializing the Composer
        3. How it works…
        4. There's more…
        5. See also
      3. Handling routines
        1. How to do it…
          1. Setting up routing
          2. Using SimpleRouteStack
          3. Using TreeRouteStack
        2. How it works…
        3. There's more…
          1. Namespace – Zend\Mvc\Router\Http
            1. The Hostname class explained
            2. The Literal class explained
          2. Methods explained
          3. The Part class explained
          4. Regex explained
          5. The Scheme class explained
          6. The Segment class explained
      4. Understanding dependency injection
        1. How to do it…
          1. Initializing the DI at call-time
          2. Initializing the DI through a Configuration object
        2. How it works…
          1. The DI only gives out one instance of an object
          2. Defining either all properties, or using a Fully Qualified (FQ) setter parameter
        3. There's more…
      5. Using configurations to your benefit
        1. How to do it…
          1. Creating a global configuration
          2. Creating configuration that only works for a local machine
          3. Editing your application.config.php file
        2. How it works…
        3. There's more…
      6. The EventManager and Bootstrap classes
        1. How to do it…
          1. Using the bootstrap
          2. Starting a session
            1. Using the EventManager class
            2. Changing the View output
        2. How it works…
          1. The Observer pattern explained
          2. Aspect-Oriented Programming (AOP) explained
            1. Event-driven architecture explained
        3. There's more…
    9. 2. Translating and Mail Handling
      1. Introduction
      2. Translating your application
        1. Getting ready
        2. How to do it…
          1. Setting up and checking the essentials
          2. Translating strings in the controller
          3. Translating strings in the View
          4. Translating strings with Poedit
        3. How it works…
          1. Basic set up of translation in your module
        4. There's more…
          1. PHP array
          2. Gettext
          3. Ini
      3. Localizing your application
        1. How to do it…
          1. So it begins
          2. Localizing currencies
          3. Localizing date/times
        2. How it works…
          1. Identifying the client language
          2. Localizing currencies and dates
      4. Sending mail
        1. How to do it…
          1. Transport\Sendmail
          2. Transport\Smtp
          3. Transport\File
        2. How it works…
          1. Sending mail through SMTP
          2. Sending mail through files
      5. Receiving mail
        1. Getting ready
        2. How to do it…
          1. Connecting to an IMAP mail server
          2. Connecting to a POP3 mail server
          3. Working with flags on IMAP or Maildir connections
          4. Maildir++ Quota system
          5. Keeping a connection alive
        3. How it works…
          1. Connecting to a POP3 server
          2. About the Maildir++ Quota system
          3. Keeping the connection alive
        4. There's more…
    10. 3. Handling and Decorating Forms
      1. Introduction
      2. Creating forms
        1. Getting ready…
        2. How to do it…
          1. Creating a basic form
          2. Defining a form that is extended from Zend\Form
          3. Defining a form that uses the Zend\Form\Annotation
          4. Adding elements to a Zend\Form extend form
          5. Adding elements to an annotated form
          6. Validating form input
        3. How it works…
          1. Setting up a basic form
          2. Setting up an annotated form
          3. Adding elements to the form
          4. Forms, filtering, and validation
        4. There's more…
      3. Using form view helpers
        1. How to do it…
          1. Form
          2. FormButton
          3. FormCaptcha
          4. FormCheckbox
          5. FormCollection
          6. FormColor
          7. FormDate, FormDateTime, and FormDateTimeLocal
          8. FormEmail
          9. FormFile
          10. FormHidden
          11. FormImage
          12. FormInput
          13. FormLabel
          14. FormElementErrors
        2. How it works…
          1. Form
          2. FormButton
          3. FormCaptcha
          4. FormCheckbox
          5. FormCollection
          6. FormColor
          7. FormDate, FormDateTime, and FormDateTimeLocal
          8. FormEmail
          9. FormFile
          10. FormHidden
          11. FormImage
          12. FormInput
          13. FormLabel
          14. FormElementErrors
      4. Creating a custom form element and form view helper
        1. How to do it…
          1. Creating the new element
            1. Creating the new view helper
          2. Adding view helper to the configuration
          3. Displaying the new element
        2. How it works…
          1. Creating the element
          2. What did we do
    11. 4. Using View
      1. Introduction
      2. Working with View
        1. Getting ready
        2. How to do it…
          1. Configure the ViewManager
          2. Set variables in the ViewModel instance
          3. Mark up the template file
        3. How it works…
          1. The configuration
          2. The ViewModel instance
          3. The ViewStrategy class
          4. The ViewRenderer helper
        4. Getting ready
        5. How to do it…
          1. The BasePath view helper
          2. The Doctype view helper
          3. The URL view helper
          4. The Partial view helper
        6. How it works…
          1. Zend\View\Helper\AbstractHelper
          2. The BasePath view helper explained
          3. The Doctype view helper explained
          4. The URL view helper explained
          5. The Partial view helper explained
        7. There's more…
      3. Creating a global layout template
        1. Getting ready
        2. How to do it…
          1. Creating the main layout file
          2. Creating the error templates
        3. How it works…
      4. Creating reusable Views
        1. Getting ready
        2. How to do it…
          1. Use the Action view helper to get the re-usable content
          2. Define a child to the ViewModel instance
        3. How it works…
          1. The Action view helper explained
          2. Defining a child to a ViewModel instance explained
          3. Pros and cons
      5. Using view strategies/renderers
        1. Getting ready
        2. How to do it…
          1. Adding a view strategy
          2. The JSON strategy
          3. The Feed strategy
        3. How it works…
          1. The view strategy class
          2. The default view strategies
          3. The JSON strategy explained
          4. The Feed strategy explained
          5. More about view strategies
      6. Using context switching for a different output
        1. Getting ready
        2. How to do it…
          1. Define multiple strategies to output
          2. Determine the view model based on the Accept header
        3. How it works…
        4. There's more…
      7. Writing a custom view strategy/renderer
        1. How to do it…
          1. Creating the XmlOutput renderer
        2. How it works…
          1. The XmlRenderer and XmlModel
          2. The XmlStrategy
        3. There's more…
    12. 5. Configuring and Using Databases
      1. Introduction
        1. Default database engines available
          1. IBM DB2 driver
          2. MySQLi driver
          3. OCI8 driver
          4. PGSQL driver
          5. SQLSRV driver
          6. PDO driver
      2. Connecting to a database
        1. Getting ready
        2. How to do it…
          1. Connecting to a MySQL database through the configuration
          2. Connecting to multiple databases through the configuration
          3. Connecting to a MySQL database through code
        3. How it works…
          1. Connecting to a MySQL database through the configuration
          2. Connecting to multiple databases through the configuration
          3. About the ServiceManager
      3. Executing simple queries
        1. Getting ready
        2. How to do it…
          1. Using raw SQL
          2. Using the prepared statements
          3. Quote identifier
          4. Quote identifier chain
          5. Quote (trusted) value
          6. Quote value list
          7. Quote identifier in fragment
        3. How it works…
          1. Using raw SQL
          2. Using prepared statements
          3. Quoting in our SQL
          4. Using createStatement
      4. Executing queries using TableGateway
        1. Getting ready
        2. How to do it…
          1. Inserting a new record
          2. Updating a record
          3. Deleting a record
          4. Advanced selects – joins conditions
        3. How it works…
      5. Optimizating with a DB profiler
        1. Getting ready
        2. How to do it…
          1. Setting up a new profiler
        3. How it works…
        4. There's more…
      6. Creating a Database Access Object
        1. Getting ready
        2. How to do it…
          1. Creating our new module and configuration
          2. Creating a connector
          3. Creating a mapper interface
          4. Creating an abstract mapper class
          5. Creating a Data Transfer Object
          6. Creating a mapper class
        3. How it works…
          1. About the DAO
          2. About the recipe
    13. 6. Modules, Models, and Services
      1. Introduction
      2. Creating a new module
        1. Getting ready
        2. How to do it…
          1. Creating the Module.php
          2. Attaching to the loadModules.postevent
          3. Implementing the getAutoloaderConfig
          4. Implementing the getControllerConfig, getControllerPluginConfig and getViewHelperConfig
        3. How it works…
          1. Creating a new module directory
          2. Creating the Module.php
          3. Optionally act on ModuleManager events
          4. Understanding the loadModules event
          5. The loadModules.resolve event
          6. The loadModule event
          7. The loadModules.post
          8. Attaching to the loadModules.post event
          9. More specific non configuration file Module configuration
          10. The getAutoloaderConfig method
          11. The getControllerConfig, getControllerPluginConfig, and getViewHelperConfig methods
      3. Using modules as a widget
        1. Getting ready
        2. How to do it…
          1. Creating the Comment/Controller/Index
          2. Using a view helper to display the comments statically
          3. Using the forward to render the comments statically
          4. Getting the comments through AJAX
        3. How it works…
          1. Using a view helper to display the comments statically
          2. Using the forward() method to render the comments statically
          3. Getting the comments through AJAX
          4. About Widgetizing
      4. A Model and a Hydrator
        1. Getting ready
        2. How to do it…
          1. Accessing the Model
          2. Creating a Hydrator
          3. Creating a Hydrator strategy
        3. How it works…
          1. Think about the model's purpose
          2. Think about the model's location
          3. Think about the model's methods
          4. Unit test the model
          5. Document your class
          6. Creating a hydrator
          7. Creating a hydrator strategy
          8. About models
        4. There's more…
      5. A basic service
        1. Getting ready
        2. How to do it…
          1. Creating a service
          2. Getting a service from within a controller
        3. How it works…
    14. 7. Handling Authentication
      1. Introduction
      2. Understanding Authentication methods
        1. Getting ready
        2. How to do it…
          1. The DbTable adapter
          2. The Http adapter
        3. How it works…
          1. The DbTable adapter (again)
          2. The Http adapter (again)
          3. The Digest adapter
          4. The LDAP adapter
          5. About Authentication
      3. Setting up a simple database Authentication
        1. Getting ready
        2. How to do it…
          1. Setting up the module initialization
          2. Creating the authentication service
          3. Setting up the controller and action
        3. How it works…
      4. Writing a custom Authentication method
        1. Getting ready
        2. How to do it…
          1. Creating our adapter
          2. The adapter outline
          3. Creating a getter and setter for any error messages
          4. Making sure we have a secure connection
          5. Checking if the certificate is an actual certificate
          6. Checking if we have all the certificate fields
          7. Checking if the certificate isn't expired yet
          8. Creating a getter and setter for the Database adapter
          9. Creating the authenticate method
        3. How it works…
          1. What are we trying to achieve
          2. About certificates
        4. There's more…
    15. 8. Optimizing Performance
      1. Introduction
      2. Caching and when to Cache
        1. Getting ready
        2. How to do it…
          1. Caching configuration
          2. Caching output
          3. Caching the class map
        3. How it works…
      3. Understanding and using storage plugins
        1. How to do it…
          1. Using the ClearExpiredByFactor plugin
          2. Using the ExceptionHandler plugin
          3. Using the IgnoreUserAbort plugin
          4. Using the OptimizeByFactor plugin
          5. Using the Serializer plugin
          6. Using any plugin
        2. How it works…
      4. Setting up a caching system
        1. Getting ready
        2. How to do it…
          1. Benchmarking our application before cache
          2. Implementing configuration/class map cache
          3. Implementing the class caching
        3. How it works…
    16. 9. Catching Bugs
      1. Introduction
      2. Handling Exceptions – your partner in crime
        1. Getting ready
        2. How to do it...
          1. Exception classes in Zend Framework 2
          2. Handling Exceptions on dispatch or rendering
        3. How it works...
          1. Exception classes in Zend Framework 2
          2. Handling exceptions on dispatch or rendering
          3. About try-catch
        4. See also
      3. Logging and how it makes your life easier
        1. Getting ready
        2. How to do it...
          1. Implementing a really simple file logger
          2. Implementing a FirePHP logger
        3. How it works...
          1. Implementing a really simple file logger
          2. Implementing a FirePHP logger
          3. About the Logger
      4. Unit testing – why would you do it
        1. Getting ready
        2. How to do it...
          1. Pseudo-code examples
        3. How it works...
          1. What is unit testing
          2. When should we test? – before, or after code is written
          3. It is a matter of discipline
      5. Setting up and using unit testing
        1. Getting ready
        2. How to do it...
          1. Setting up the test framework
        3. How it works...
        4. There's more...
        5. See also
    17. A. Setting up the Essentials
      1. Making sure you have all that you need
        1. Installing Zend Server Community Edition on a Linux environment
        2. Installing Zend Server Community Edition on a Windows environment
        3. First-time run of Zend Server
        4. See also
      2. Downloading Zend Framework 2 and finding its documentation
        1. Finding Zend Framework 2
        2. Coding in the phpcloud
        3. The documentation and getting started guide
        4. See also
      3. Composer and its uses within Zend Framework 2
        1. The composer.json file
        2. Upgrading packages
        3. See also
      4. Basic Zend Framework 2 structures
        1. Folder – config
        2. Folder – module
          1. Folder – public
          2. Folder – vendor
          3. File – init_autoloader.php
          4. What's next?
      5. About storage adapters and patterns
        1. Storage adapter's implementations
        2. Storage adapters
          1. Apc caching
          2. Dba caching
          3. File system caching
          4. Memcached caching
          5. Memory caching
          6. Redis caching
          7. Session caching
          8. WinCache caching
          9. XCache caching
          10. ZendServerDisk caching
          11. ZendServerShm caching
        3. Cache patterns
          1. The CallbackCache pattern
          2. The CaptureCache pattern
          3. The ClassCache pattern
          4. The ObjectCache pattern
          5. The OutputCache pattern
          6. The PatternOptions pattern
        4. Explaining the difference
    18. Index