Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
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
$28.99 $32.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (4 Ratings)
eBook Dec 2013 340 pages 1st Edition
eBook
$28.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Josephus Callaars Profile Icon joseph@callaa.rs
Arrow right icon
$28.99 $32.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (4 Ratings)
eBook Dec 2013 340 pages 1st Edition
eBook
$28.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$28.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
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

Billing Address

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

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 : 9781849694858
Vendor :
Zend Technologies
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
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

Billing Address

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.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
$199.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
$279.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 $ 79.98
Zend Framework 2 Cookbook
$54.99
Instant Zend Framework 2.0
$24.99
Total $ 79.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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.