Using Zend Framework 3
A free and open-source book on ZF3 for beginners
Home
Samples
Class Reference
Contribute
This book is available in:
English
Русский
Español
Français
Português
Table of Contents
Preface
About this Book
Intended Audience
Examples and Source Code
Your Feedback
About the Author
Acknowledgments
1. Introduction to Zend Framework 3
1.1. What is Zend Framework 3?
1.2. An Example PHP Website
1.2.1. Home Page
1.2.2. Login Page
1.2.3. Logout Page
1.2.4. Reviewing the Code
1.3. License
1.4. User Support
1.5. Framework Source Code
1.5.1. Coding Standards
1.6. Supported Operating Systems
1.7. Server Requirements
1.8. Security
1.9. Performance
1.10. Design Patterns
1.11. Main ZF3 Components
1.12. Differences with Zend Framework 2
1.12.1. Backward Compatibility
1.12.2. Components
1.12.3. Component Installer
1.12.4. ServiceManager and EventManager Performance
1.12.5. PSR-4
1.12.6. Middleware
1.12.7. Focus on Documentation
1.13. Summary
2. Zend Skeleton Application
2.1. Getting Zend Skeleton Application
2.2. Typical Directory Structure
2.3. Application Dependencies
2.4. Apache Virtual Host
2.5. Opening the Website in Your Browser
2.6. Creating NetBeans Project
2.7. Hypertext Access File (.htaccess)
2.8. Blocking Access to the Website by IP Address
2.9. HTTP Authentication
2.10. Having Multiple Virtual Hosts
2.11. Hosts File
2.12. Advanced Composer Usage
2.12.1. Package Names and Versions
2.12.2. Installing and Updating Packages
2.12.3. Adding a New Dependency
2.12.4. Virtual Packages
2.12.5. Composer and Version Control Systems
2.13. Summary
3. Website Operation
3.1. PHP Classes
3.2. PHP Namespaces
3.3. PHP Interfaces
3.4. PHP Class Autoloading
3.4.1. Class Map Autoloader
3.4.2. PSR-4 Standard
3.4.3. Composer-provided Autoloader
3.4.4. PSR-4 and Module's Source Directory Structure
3.5. HTTP Request and Response
3.6. Site Entry Script
3.7. Events & Application's Life Cycle
3.8. Application Configuration
3.8.1. Application-Level Config Files
3.8.2. Application-Level Extra Config Files
3.8.3. Application-Level Development Config File
3.8.4. Application-Level Extra Development Config Files
3.8.5. Module-Level Config Files
3.8.6. Combining the Configuration Files
3.9. Module Entry Point
3.10. Service Manager
3.10.1. Registering a Service
3.10.2. Service Names
3.10.3. Overriding an Existing Service
3.10.4. Registering Invokable Classes
3.10.5. Registering a Factory
3.10.6. Registering an Abstract Factory
3.10.7. Registering Service Aliases
3.10.8. Shared and Non-Shared Services
3.10.9. Service Manager Configuration
3.11. Plugin Managers
3.12. About Event Manager
3.12.1. Event & MvcEvent
3.12.2. EventManager & SharedEventManager
3.13. Summary
4. Model-View-Controller
4.1. Get the Hello World Example from GitHub
4.2. Separating Business Logic from Presentation
4.3. Controllers
4.3.1. Base Controller Class
4.4. Retrieving Data from HTTP Request
4.5. Retrieving GET and POST Variables
4.6. Putting Data to HTTP Response
4.7. Variable Containers
4.8. Expressing Error Conditions
4.9. Controller Registration
4.9.1. Registering a Controller Factory
4.9.2. LazyControllerAbstractFactory
4.10. When to Create a New Controller?
4.11. Controller Plugins
4.11.1. Writing Own Controller Plugin
4.12. Views
4.13. View Helpers
4.14. View Template Names
4.14.1. Overriding Default View Template Name
4.15. View Resolver
4.16. Disabling the View Rendering
4.17. View Rendering Strategies
4.17.1. Returning JSON Response
4.18. Error Pages
4.19. Models
4.20. Model Types
4.20.1. Entities
4.20.2. Repositories
4.20.3. Value Objects
4.20.4. Services
4.20.5. Factories
4.21. Determining the Correct Model Type
4.22. Other Model Types
4.23. Skinny Controllers, Fat Models, Simple Views
4.23.1. Skinny Controllers
4.23.2. Fat Models
4.23.3. Simple View Templates
4.24. Summary
5. Routing
5.1. URL Structure
5.2. Route Types
5.3. Combining Route Types
5.3.1. Simple Route Stack
5.3.2. Tree Route Stack
5.4. Routing Configuration
5.4.1. Configuration for Simple Routes
5.4.2. Configuration for Nested Routes
5.4.3. Default Routing Configuration in Zend Skeleton Application
5.5. Literal Route Type
5.6. Segment Route Type
5.7. Regex Route Type
5.8. Other Route Types
5.8.1. Hostname
5.8.2. Scheme
5.8.3. Method
5.9. Extracting Parameters from Route
5.9.1. Retrieving the RouteMatch and the Router Object
5.10. Generating URLs from Route
5.10.1. Generating URLs in View Templates
5.10.1.1. Passing Parameters
5.10.1.2. Generating Absolute URLs
5.10.1.3. Specifying Query Part
5.10.2. Generating URLs in Controllers
5.10.3. URL Encoding
5.11. Writing Own Route Type
5.11.1. RouteInterface
5.11.2. Custom Route Class
5.12. Summary
6. Page Appearance and Layout
6.1. About CSS Stylesheets and Twitter Bootstrap
6.2. Page Layout in Zend Framework 3
6.3. Modifying the Default Page Layout
6.4. Switching between Layouts
6.4.1. Setting Layout for All Actions of a Controller
6.5. Partial Views
6.6. Placeholder View Helper
6.7. Adding Scripts to a Web Page
6.7.1. Example
6.8. Adding CSS Stylesheets to a Web Page
6.8.1. Example
6.9. Writing Own View Helpers
6.9.1. Menu
6.9.2. Breadcrumbs
6.10. View Models and Page Composition
6.11. Summary
7. Collecting User Input with Forms
7.1. Get the Form Demo Sample from GitHub
7.2. About HTML Forms
7.2.1. Fieldsets
7.2.2. Example: "Contact Us" Form
7.2.3. GET and POST Methods
7.3. Styling HTML Forms with Twitter Bootstrap
7.4. Installing ZF3 form components
7.5. Retrieving Form Data in a Controller's Action
7.6. Forms and Model-View-Controller
7.6.1. A Typical Form Usage Workflow
7.7. A Form Model
7.8. Form Elements
7.8.1. Adding Elements to a Form Model
7.8.2. Method 1: Passing an Instance of an Element
7.8.3. Method 2: Using Array Specification
7.9. Example: Creating the Contact Form Model
7.10. Adding Form Filtering and Validation Rules
7.10.1. Input Filter
7.10.2. Adding Inputs to Input Filter
7.10.2.1. Filter Configuration
7.10.2.2. Validator Configuration
7.10.3. Creating Input Filter for the Contact Form
7.11. Using the Form in a Controller's Action
7.11.1. Passing Form Data to a Model
7.12. Form Presentation
7.12.1. Preparing the Form Model for Rendering
7.13. Standard Form View Helpers
7.13.1. Rendering a Form Element
7.13.2. Rendering an Element's Validation Errors
7.13.3. Rendering an Element's Label
7.13.4. Rendering a Form Row
7.13.5. Rendering the Entire Form
7.14. Example: Creating the View Template for the Contact Form
7.14.1. Applying the Bootstrap CSS Styles to Form
7.14.2. Styling the Validation Errors List
7.14.3. Adding the "Thank You" & "Error Sending Email" Pages
7.14.4. Results
7.15. Summary
8. Transforming Input Data with Filters
8.1. About Filters
8.1.1. FilterInterface
8.2. Standard Filters Overview
8.3. Instantiating a Filter
8.3.1. Method 1: Instantiating a Filter Manually
8.3.2. Method 2: Constructing a Filter with StaticFilter
8.3.3. Method 3: Constructing a Filter From Array
8.4. About Filter Plugin Manager
8.5. Filter's Behavior in Case of Incorrect Input Data
8.6. Filter Usage Examples
8.6.1. Filters Casting Input Data to a Specified Type
8.6.1.1. ToInt Filter
8.6.1.2. Boolean Filter
8.6.1.3. ToNull Filter
8.6.1.4. DateTimeFormatter Filter
8.6.2. Filters Performing Manipulations on a File Path
8.6.2.1. BaseName Filter
8.6.2.2. Dir Filter
8.6.2.3. RealPath Filter
8.6.3. Filters Performing Compression and Encryption of Input Data
8.6.3.1. Compress Filter
8.6.3.2. Encrypt Filter
8.6.4. Filters Manipulating String Data
8.6.4.1. StringToLower Filter
8.6.4.2. PregReplace Filter
8.6.4.3. StripTags Filter
8.6.4.4. StripNewlines Filter
8.6.4.5. UriNormalize Filter
8.6.5. Organizing Filters in a Chain
8.6.6. Custom Filtering with the Callback Filter
8.6.6.1. Example
8.7. Writing Your Own Filter
8.7.1. Using the PhoneFilter Class
8.8. Summary
9. Checking Input Data with Validators
9.1. About Validators
9.1.1. ValidatorInterface
9.2. Standard Validators Overview
9.3. Validator Behaviour in Case of Invalid or Unacceptable Data
9.4. Instantiating a Validator
9.4.1. Method 1. Manual Instantiation of a Validator
9.4.2. Method 2. Using StaticValidator Wrapper
9.4.3. Method 3. Using an Array Configuration
9.5. About Validator Plugin Manager
9.6. Validator Usage Examples
9.6.1. Validators for Checking Value Conformance to Certain Format
9.6.1.1. Ip Validator
9.6.1.2. Hostname Validator
9.6.1.3. Uri Validator
9.6.1.4. Date Validator
9.6.1.5. Regex Validator
9.6.2. Validators for Checking if a Numerical Value Lies in a Given Range
9.6.2.1. NotEmpty Validator
9.6.2.2. Between Validator
9.6.2.3. InArray Validator
9.6.2.4. StringLength Validator
9.6.3. Organizing Validators in a Chain
9.6.4. Custom Validation with the Callback Validator
9.6.4.1. Example
9.7. Writing Own Validator
9.7.1. Using the PhoneValidator Class
9.8. Using Filters & Validators Outside a Form
9.9. Summary
10. Uploading Files with Forms
10.1. About HTTP File Uploads
10.1.1. HTTP Binary Transfer Encoding
10.1.2. $_FILES Super-Global Array in PHP
10.2. Accessing Uploaded Files in ZF3
10.3. File Uploads & ZF3 Form Model
10.4. Validating Uploaded Files
10.5. Filtering Uploaded Files
10.6. InputFilter Container & File Uploads
10.6.1. FileInput
10.6.2. Executing Validators before Filters
10.7. Controller Action & File Uploads
10.8. Example: Image Gallery
10.8.1. Adding ImageForm Model
10.8.2. Adding Validation Rules to ImageForm Model
10.8.3. Writing ImageManager Service
10.8.4. Adding ImageController
10.8.4.1. Adding Upload Action & Corresponding View Template
10.8.4.2. Adding Index Action & Corresponding View Template
10.8.4.3. Adding File Action
10.8.4.4. Creating Factory for the Controller
10.8.4.5. Registering the ImageController
10.8.4.6. Creating Route
10.8.5. Results
10.9. Summary
11. Advanced Usage of Forms
11.1. Form Security Elements
11.1.1. CAPTCHA
11.1.1.1. CAPTCHA Types
11.1.1.2. CAPTCHA Form Element & View Helper
11.1.1.3. Example 1: Adding Image CAPTCHA to the ContactForm
11.1.1.4. Example 2: Adding a FIGlet CAPTCHA to the ContactForm
11.1.2. CSRF Prevention
11.1.2.1. Example: Adding a CSRF Element to Form
11.2. Using Validation Groups
11.3. Implementing Multi-Step Forms
11.3.1. Enabling Sessions
11.3.2. Adding RegistrationForm
11.3.3. Adding RegistrationController
11.3.3.1. Adding RegistrationControllerFactory
11.3.4. Adding View Templates
11.3.5. Adding Route
11.3.6. Results
11.4. Summary
12. Database Management with Doctrine ORM
12.1. Get Blog Example from GitHub
12.2. Creating a Simple MySQL Database
12.2.1. Creating New Database
12.2.2. Creating Tables
12.2.3. Importing Ready Database Schema
12.3. Integrating Doctrine ORM with Zend Framework 3
12.3.1. Installing Doctrine Components with Composer
12.3.2. Loading Doctrine Integration Modules on Application Start Up
12.3.3. Doctrine Configuration Overview
12.3.4. Overriding the Default Doctrine Configuration
12.4. Specifying Database Connection Parameters
12.5. About Doctrine Entities
12.5.1. Annotations
12.6. Creating Entities
12.6.1. Adding Post Entity
12.6.2. Adding the Comment and Tag Entities
12.6.3. Specifying Relationships between Entities
12.6.3.1. OneToMany/ManyToOne
12.6.3.2. ManyToMany
12.6.4. Specifying Entity Locations
12.7. About Entity Manager
12.7.1. Entity Repositories
12.8. Adding Blog Home Page
12.9. Adding New Post
12.9.1. Adding PostForm
12.9.2. Adding PostManager Service
12.9.3. Creating Controller Action and View Template
12.10. Editing Existing Post
12.10.1. Modifying PostManager
12.10.2. Adding Controller Action and View Template
12.11. Deleting a Post
12.11.1. Modifying PostManager
12.11.2. Adding Controller Action
12.12. Implementing Post Preview
12.12.1. Adding CommentForm
12.12.2. Modifying PostManager
12.12.3. Adding Controller Action and View Template
12.13. Implementing Admin Page
12.14. Implementing Tag Cloud
12.14.1. Adding Custom Post Repository
12.14.2. Calculating Tag Cloud
12.14.3. Modifying Controller Action
12.14.4. Rendering Tag Cloud
12.15. Implementing Pagination
12.15.1. Doctrine ORM Paginator
12.15.2. ZF3 Paginator
12.15.2.1. Modifying PostRepository
12.15.2.2. Modifying IndexController
12.15.2.3. Visualizing the Pagination Widget
12.15.2.4. Results
12.16. Summary
13. Database Migrations
13.1. Installing Doctrine\Migrations Component
13.2. Configuring Migrations
13.3. Creating Migrations
13.3.1. Creating the Initial Migration
13.3.2. Adding Another Migration
13.4. Executing Migrations
13.5. Summary
14. Creating a New Module
14.1. When to Create a New Module?
14.2. How to Create a New Module?
14.2.1. Renaming the Skeleton Module
14.2.2. Enabling Class Autoloading
14.2.3. Enabling the Module
14.3. Module.php File & Event Listening
14.3.1. Example 1. Switching Layout Template
14.3.2. Example 2. Forcing the Use of HTTPS
14.3.3. Example 3. Reporting All Exceptions in Your Website
14.4. Registering the Module as a Composer Package
14.5. Summary
15. Working with Sessions
15.1. PHP Sessions
15.2. Installing Zend\Session Component
15.3. Session Manager
15.3.1. Providing Session Configuration
15.3.2. Making the Session Manager the Default One
15.4. Session Containers
15.4.1. Method 1. Manual Instantiation of a Session Container
15.4.2. Method 2. Creating a Session Container Using Factory
15.4.3. Saving Data to Session with Session Container
15.5. Summary
16. User Management, Authentication and Access Filtering
16.1. Get User Demo Sample from GitHub
16.2. Creating the User Module
16.3. Setting Up the Database
16.4. Implementing User Entity
16.5. Adding UserController
16.6. Adding UserManager Service
16.6.1. Creating a New User & Storing Password Encrypted
16.6.2. Validating Encrypted Password
16.6.3. Creating Admin User
16.6.4. Resetting User Password
16.7. Implementing User Authentication
16.7.1. AuthenticationService
16.7.2. Writing Authentication Adapter
16.7.3. Creating the Factory for AuthenticationService
16.7.4. Adding AuthController
16.7.5. Adding View Template for Login Page
16.7.6. Adding AuthManager Service
16.8. Access Filtering
16.8.1. Adding Dispatch Event Listener
16.8.2. Implementing Access Filtering Algorithm
16.8.3. Testing Access Filter
16.9. Identity Controller Plugin and View Helper
16.10. Summary
17. Role-Based Access Control
17.1. Get Role Demo Sample from GitHub
17.2. Introduction to RBAC
17.2.1. Roles and Permissions
17.2.2. RBAC Container
17.2.3. Checking Permissions
17.3. Default Roles in the Role Demo Sample
17.4. Introduction to Dynamic Assertions
17.5. Setting Up the Database
17.6. Implementing Entities
17.7. Implementing Role Management
17.8. Implementing Permission Management
17.9. Assigning Roles to a User
17.10. Implementing RbacManager
17.10.1. Setting Up Caching
17.10.2. Writing the RbacManager Service
17.11. Adding the Not Authorized Page
17.12. Modifying the AuthManager Service
17.13. Modifying the Dispatch Event Listener
17.14. Adding Access Controller Plugin and View Helper
17.14.1. Access Controller Plugin
17.14.2. Access View Helper
17.15. Using the User Module
17.16. Summary
Appendix A. Configuring Web Development Environment
Installing Apache, PHP and MySQL in Linux
Installing Apache and PHP
Checking Web Server Installation
Editing PHP Configuration
Restarting Apache Web Server
Enabling Apache's mod_rewrite module
Creating Apache Virtual Host
Installing XDebug PHP extension
Installing MySQL Database Server
Configuring the MySQL Database Server
Installing Apache, PHP and MySQL in Windows
Checking Web Server Installation
Enabling Apache's mod_rewrite module
Creating Apache Virtual Host
Installing XDebug PHP extension
Configuring the MySQL Database Server
Installing NetBeans IDE in Linux
Installing NetBeans IDE in Windows
Summary
Appendix B. Introduction to PHP Development in NetBeans IDE
Run Configuration
Running the Website
Site Debugging in NetBeans
Debug Toolbar
Breakpoints
Watching Variables
Call Stack
Debugging Options
Profiling
Summary
Appendix C. Introduction to Twitter Bootstrap
Overview of Bootstrap Files
Grid System
Defining the Grid
Offsetting Columns
Nesting Grids
"Mobile First" Concept
Bootstrap's Interface Components
Navigation Bar
Dropdown Menu
Collapsible Navbar
Inverse Navbar Style
Breadcrumbs
Pagination
Buttons & Glyphicons
Customizing Bootstrap
Summary
Appendix D. Introduction to Doctrine
Doctrine and Database Management Systems
Relational Databases
SQL vs. DQL
NoSQL Databases
Document Databases
Doctrine Architecture
Components Supporting Relational Databases
Components Supporting NoSQL Document Databases
Summary
Appendix E. Installing a ZF3 Web Application to Amazon EC2
What is Amazon Web Services?
Creating Amazon Web Services Account
Creating a Key Pair
Launching EC2 Linux Instance
Step 1 – Choose an Amazon Machine Image (AMI)
Step 2 — Choose an Instance Type
Step 3 — Configure Instance
Step 4 — Add Storage
Step 5 — Tag Instance
Step 6 — Security Group
Step 7 — Review Instance Launch
Assigning an IP Address to the Instance
Connecting to EC2 Instance through SSH
Installing Apache HTTP Server and PHP Engine
Installing the Hello World Web Application
Creating Virtual Host
Installing Zend Framework 3 with Composer
Results
Summary
Top