Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Zend Framework 2 Cookbook
Zend Framework 2 Cookbook

Zend Framework 2 Cookbook: If you are pretty handy with PHP, this book is the perfect way to access and understand the features of Zend Framework 2. You can dip into the recipes as you wish and learn at your own pace.

Arrow left icon
Profile Icon Josephus Callaars Profile Icon joseph@callaa.rs
Arrow right icon
€41.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (4 Ratings)
Paperback Dec 2013 340 pages 1st Edition
eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Josephus Callaars Profile Icon joseph@callaa.rs
Arrow right icon
€41.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (4 Ratings)
Paperback Dec 2013 340 pages 1st Edition
eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Zend Framework 2 Cookbook

Chapter 2. Translating and Mail Handling

In this chapter we will cover:

  • Translating your application

  • Localizing your application

  • Sending mail

  • Receiving mail

Introduction


An application wouldn't be an application if it couldn't react to the users. One simple but effective way of reacting is obviously displaying text and sending e-mails. Over the last couple of years internationalization (i18n) and localization (l10n) have become increasingly important. Nowadays users expect to be greeted in their language, and even receive automated e-mails from applications in a normal day's work.

Translating your application


In this recipe we will be using the Zend Framework 2 skeleton as a base, but we will create a new module to show how it all works.

Getting ready

For this recipe, we assume that you have a working Zend Framework 2 application/skeleton in place. To ensure that we can actually run the code that we produce in the recipe, we need to make sure that the intl and gettext extensions in PHP are enabled.

For translating the strings we will be using Poedit, a cross-platform open source application used for translating gettext catalogs. The current version is 1.5.5 and can be found at http://www.poedit.net/ website. We are using gettext as this is a widely used internationalization and localization system for writing multilingual applications. The files generated by Poedit have the extensions .po or .mo. The .po file is used for editing; let's say this is an uncompiled translation file. The .mo file is the compiled translation file, which is used in our application.

How to do...

Localizing your application


In this recipe, we will explain localization and its uses. Localization differs from internationalization in the way that localization refers to, for example, numeric, date and time formats, and the use of currency.

How to do it…

In this recipe we will be discussing the ever so important localization of our application.

So it begins

When a user hits our website, we most likely want the user to automatically go to the right language. Although, there are several methods of doing this, we will be using a manual check to see if the language the user prefers is also in our list of languages.

We do this by a couple of simple tricks:

  • First, we are getting the Accept-Language headers from the HTTP request

  • Then we iterate through them and see if one of the languages mentioned in the header matches the language we have

  • Lastly, we set the language to the language we have found, or if nothing is found, the fallback language is set

Let's see how this looks in our Module.php code:

/...

Sending mail


Sending e-mail through sendmail is usually a pretty standard way of working, as it is probably one of the most used ways of transporting e-mail (or proxying the e-mail to an SMTP server) on a Linux-based system. On most Linux servers sendmail is already installed and therefore it's very easy to start sending e-mail with that.

That is why we will be discussing this method of sending e-mail first, so that we can start off easy.

How to do it…

In this recipe we will discuss the method of sending mail from within our application.

Transport\Sendmail

Let's take a look at the following example of sending an e-mail through sendmail, and although this functionality is placed in a controller, in real life this needs to stay far away from that and be placed safely away in a model:

<?php

namespace Application\Controller;

// We need the following libraries at a minimum to 
// send an e-mail.
use Zend\Mail\Message;
use Zend\Mail\Transport\Sendmail;
use Zend\Mvc\Controller\AbstractActionController...

Receiving mail


Now, let's deal with the part of receiving mails.

Getting ready

In this recipe we will be giving examples on the different methods of connecting to mailbox through ZF2, and therefore it would be nice if we had access to a mailbox we connect to. Of course this is not required, but it sure adds to the fun to have an actual working mailbox.

How to do it…

We will now discuss receiving e-mail within an application, which can be useful on some occasions.

Connecting to an IMAP mail server

The first method of connecting to a mail server is through IMAP. The protocol basically lets us connect to the mail server, and looking in the different folders on the server if there are unread e-mails.

Let's take a look at our example:

<?php
// Usually this sort of code is defined in the Model,
// but to test it out we can place it in the 
// controller as well.
namespace Application\Controller;

// We need these classes to initiate an IMAP connection
use Zend\Mail\Storage\Imap;
use Zend\Mvc\Controller...
Left arrow icon Right arrow icon

Key benefits

  • 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

Description

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.

Who is this book for?

Zend Framework 2 Cookbook" is for PHP developers who are fairly advanced in programming in PHP. It will also be useful for developers who have a keen interest in expanding their knowledge outside the boundaries of simply scripting pages together. As unit testing and MVC will be discussed, it is beneficial for the reader to know what these technologies are, although experience with developing applications is not necessarily essential.

What you will learn

  • Set up Zend Framework 2 on a Linux web server
  • Make your application accessible in other countries and languages
  • Choose and create custom output display renderings
  • Set up modules and use them as widgets
  • Connect and query different types of databases
  • Create the best authentication method
  • Optimize framework applications
  • Debug and test framework applications
Estimated delivery fee Deliver to Spain

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Dec 19, 2013
Length: 340 pages
Edition : 1st
Language : English
ISBN-13 : 9781849694841
Vendor :
Zend Technologies
Languages :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Spain

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Publication date : Dec 19, 2013
Length: 340 pages
Edition : 1st
Language : English
ISBN-13 : 9781849694841
Vendor :
Zend Technologies
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 61.98
Zend Framework 2 Cookbook
€41.99
Instant Zend Framework 2.0
€19.99
Total 61.98 Stars icon

Table of Contents

9 Chapters
Zend Framework 2 Basics Chevron down icon Chevron up icon
Translating and Mail Handling Chevron down icon Chevron up icon
Handling and Decorating Forms Chevron down icon Chevron up icon
Using View Chevron down icon Chevron up icon
Configuring and Using Databases Chevron down icon Chevron up icon
Modules, Models, and Services Chevron down icon Chevron up icon
Handling Authentication Chevron down icon Chevron up icon
Optimizing Performance Chevron down icon Chevron up icon
Catching Bugs Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(4 Ratings)
5 star 50%
4 star 25%
3 star 0%
2 star 25%
1 star 0%
ovunc tukenmez Apr 02, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The expectations will be very high for a cookbook since the reader would want to see a solution for every difficulty he/she faces with.When it comes to ZF2, if you are beginner, things may look strange even if you look official programmer's reference guide of Zend Framework 2.The books starts with the basics. In this chapter you see how to setup skeleton application and routing (with the explanation of different "route types"), a short explanation of DI, how to create config files, in which order they merges and why we need local and global ones. ZF2's event driven architecture is also explained with an example.In the second chapter, you see how to translate and localize your application with the I18 module. You see different ways to setup translation files (with arrays, using gettext or ini files) You will also learn how to use Poedit to translate .po files. How to select different languages identifying client language is also explained. Next you skip to the mail section where you will understand how to send email with different approaches.From chapter 3 to 7, most used zf2 components during development process are explained. Using forms to get the input from the user, validating and filter them, working with view, what are the view helpers and how to use and create them, switching to different output using view strategy/renderer, details of layout system, how to configure and connect to database, which database engines that available, why we should createStatement() function instead of query() function, CRUD operations with TableGateway, advanced select query generation, db profiling, details of modules, modules and services are explained in these chapters.In 7th chapter different authentication methods are explained. You will also learn how to setup and create auth service, how to force HTTPS protocol, certificate validation.In the next chapter, different caching mechanisms are explained in order to optimize performance.Last chapter is dedicated to error handling and unit testing.In the appendix you will learn how to set up the essentials with the Zend Server Community Edition or using phpcloud service for your development process. Basic ZF2 structure is also explained here. As a bonus, there is a flow chart diagram to understand what happens in the system after a request made.If it had the Navigation (with ACL) section, it would be better however once you get the idea how the framework components work, it becomes very easy task for you.In conclusion, it is nice to have this book for a reference even if you already familiar with ZF2 to learn new ways to do things.
Amazon Verified review Amazon
William H Scholtz Jan 20, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Believe me, since I switched to Zend framework 2, mid 2012, I have purchased and used all the manuals out there. I just wish this cookbook was available earlier. It is easy to read, the examples can not be more easier to explain, and is good for beginners right up to advanced users.I strongly recommend this book, id you need to quickly start to work, and understand, the ZF 2 API.No, let me re phrase, if you are building application in the ZF2 environment, this cookbook is a must!This book is power. Again, why was it only published now?William H Scholtz
Amazon Verified review Amazon
Lenka Krafková Apr 10, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
The book is written literally as a cookbook. However, as a cookbook doesn't make you a professional cook, this book also won't make you a professional developer. It contains number of recepies (such as "using database") that show you how to use certain parts of ZF2. This approach has some pros and some cons - it's good that you can find the part you're interested in (like the db) easily and learn how to use it. But there is one problem with this cookbook - the parts are not connected to each other. It's like a manual, but much better for beginners as it explains things in plain language in more logical order. It starts with the ZF2 basics (routing, events, DI) and slowly moves towards more complex topics such as authentication or performance optimization. I think that a user without any prior ZF2 knowledge would be happy with the book. I, as a little more advanced user, missed the more complex topics and connection between the parts.
Amazon Verified review Amazon
Brianleigh65 Aug 02, 2015
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Far too opaque - should be made clear this is a book for experts
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela