Zend Framework
Create Unit Testing Cases in Zend Framework
A solid unit test suite is essential for ongoing development in large projects, especially those with many people involved. Going back and manually testing every individual component of an application after every change is impractical. Your unit tests will help alleviate that by automatically testing your application’s components and alerting you when something is not working the same way it was when you wrote your tests. The Zend Framework 2 API uses PHPUnit, and so does this tutorial application. A detailed explanation of unit testing is beyond the scope of this tutorial, so we will only provide sample tests for the components in the pages that follow. This tutorial assumes that you already have PHPUnit installed.
Create CDN View Helper in Zend Framework 2
In this article, you will learn how to create a custom view helper in Zend Framework 2. A concrete example will be used; a helper which generates links for a subdomain, intended for storing static files. This is especially useful if you wish to use a Content Delivery Network (CDN). With very little modification, the helper can be made generic to support links to subdomains for all purposes. Note: Zend Framework 2 has reached end of life. In 2019 the project moved to the Linux Foundation and was renamed Laminas, so the Zend\ namespaces below are now Laminas\ (for example Zend\View\Helper\AbstractHelper becomes Laminas\View\Helper\AbstractHelper). The view-helper mechanics are otherwise unchanged, so this post is kept as a historical walkthrough. For a real project, run the namespaces through the Laminas migration guide and follow the current custom view helper docs.
Sending Email with Zend Framework 2 Using Template
Sending a templated HTML email is a one-liner in some frameworks, but Zend Framework 2 ships no such helper, so you have to wire up the view renderer, the MIME message, and the transport yourself. This post walks through that setup. Note: Zend Framework 2 has reached end of life. In 2019 the project moved to the Linux Foundation and was renamed Laminas; the Zend\* classes shown below now live under Laminas\* (e.g. Laminas\Mail, Laminas\View). The technique is unchanged, so this post still applies once you swap the namespaces.
Zend Framework 2: Get Parameters
The easiest way to read request parameters in a Zend Framework 2 controller is the Params plugin, introduced in beta5. It has utility methods to make it easy to access different types of parameters. As always, reading the tests can prove valuable to understand how something is supposed to be used. Note: Zend Framework 2 has been deprecated. The project moved to the Linux Foundation in 2019 and lives on as Laminas; the Zend\ namespace became Laminas\, so the plugin shown here is now Laminas\Mvc\Controller\Plugin\Params (in the laminas/laminas-mvc package). The API is unchanged, so the examples below still apply after that rename.
Zend Framework 2: Redirect to 404 page in Controller
I’ve been getting into trouble for several hours with redirecting to the 404 page in Zend Framework 2. Note: Zend Framework 2 has been deprecated. Zend Framework was migrated to the Laminas Project in 2020, where development continues today. The notFoundAction() technique below still applies in laminas-mvc. I asked about this on Stack Overflow. What Didn’t Work Before that, I was using the following code: $this->getResponse()->setStatusCode(404); return; This sets the HTTP status code to 404, which looks fine at first. But it only changes the response header; it doesn’t dispatch the error controller, so once I built a custom 404 page I found that it never rendered.