Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Zend Framework Blog:
Logging PHP applications
Sep 13, 2017 @ 10:56:38

On the Zend Framework blog there's a new post in their series highlighting components of the main framework. In this new tutorial they look a the Zend-log component and how it can be used for logging in your application.

Every PHP application generates errors, warnings, and notices and throws exceptions. If we do not log this information, we lose a way to identify and solve problems at runtime. Moreover, we may need to log specific actions such as a user login and logout attempts. All such information should be filtered and stored in an efficient way.

[...] Zend Framework offers a logging component, zend-log; the library can be used as a general purpose logging system. It supports multiple log backends, formatting messages sent to the log, and filtering messages from being logged.

The tutorial then walks you through the installation of the component and some basic usage of it to write directory to the 'php://output' stream. It then shows you how to set up custom formatting on the message and a few other examples of it in use:

  • logging PHP events (errors/exceptions)
  • using the built-in data (level) filtering functionality
  • custom processors
  • working with multiple backends for storage

The post ends with a look at using the Zend-log package in either a traditional MVC application or one based more on middleware (like an Expressive application).

tagged: zendframework component zendlog logging tutorial mvc middleware

Link: https://framework.zend.com/blog/2017-09-12-zend-log.html

SitePoint PHP Blog:
Rapid Development of Zend Expressive Modules
Sep 07, 2017 @ 11:49:43

The SitePoint PHP blog has a new post from Kirk Madera sharing some of the things he's learned about creating modules in Zend Expressive. His goal is to make it as easy as possible and enable "rapid development" in your application. This is the second part of a series on rapid development with Zend Expressive from Kirk (part one is here).

I have learned a few tricks when writing Zend Expressive modules that I want to share with you.

Please follow the previous post first to set up a proper working environment. I explained how to install and configure Zend Expressive with Doctrine, Gulp, and an abstract reflection factory – it’ll take a total of 10 minutes.

In this tutorial, we’ll build a simple read-only blog module (a page listing blog posts from a database) in minutes, demonstrating the kind of rapid development one is capable of with Zend Expressive.

He then shows how to use the command line tooling to create the module and how it is registered with Composer. He then shows the creation of the "blog" entity and database tables followed by the routing changes. Next he fleshes out the actions that will respond to the requests on each route and the templates that will be served as a part of the response.

tagged: rapid development application zendexpressive zendframework tutorial module

Link: https://www.sitepoint.com/rapid-development-zend-expressive-modules/

Generating an Autoloader for a Legacy PHP Codebase
Sep 07, 2017 @ 09:11:22

The php[architect] site has posted a new tutorial from editor Oscar Merida showing you how to create an autoloader for a legacy codebase using one of three options.

If you’ve inherited a legacy code base, you may find it does not use an autoloader and has an idiosyncratic directory and file hierarchy for its Classes, Interfaces or Traits. Worse yet, it might not use name spaces consistently or at all. So you can’t use a posting on Twitter. [...] In this post, I’ll detail the three solutions I found: using Composer’s classmap autoloader, Symfony classmap generator (deprecated), or Zend Framework’s ClassFileLocator.

He then goes through each of the tools mentioned above and shows how to implement them to locate class files and auto-generate the autoloader files. They each have slightly different methods of getting the class files from the current code but they all end up with basically the same result: a classmap (set of relations between classes and the files they live in).

tagged: autoloader legacy codebase tutorial composer symfony3 zendframework

Link: https://www.phparch.com/2017/09/generating-autoloader-legacy-php-codebase/

SitePoint PHP Blog:
Rapid Enterprise App Development with Zend Expressive
Aug 29, 2017 @ 15:41:34

The SitePoint PHP blog has posted a new tutorial from author Kirk Madera showing you how to use Zend Expressive to aid in more rapid development in your applications.

If you’ve ever done a Zend Framework quick start, you’ve probably never worked in Zend Framework. The quick start has historically been anything but quick, and it’s easy to lose interest and move on to the next thing.

Zend Expressive greatly improves upon this experience with the wizard driven composer create-project command. However, it can still be daunting to set up because there are so many choices to make up front. This tutorial guides you through my recommended setup for rapid development which will yield an enterprise level, robust application.

He then walks you through the creation of a Zend Expressive project from scratch and how to ensure it's up and running as expected. He then walks you through the structure of the application, software it includes (like Doctrine and Gulp) and examples of them in use. The post ends with a look at creating console commands using the Symfony Console component.

tagged: rapid development application zendexpressive zendframework tutorial introduction

Link: https://www.sitepoint.com/rapid-enterprise-app-development-zend-expressive/

Zend Framework Blog:
Specialized Response Implementations in Diactoros
Aug 24, 2017 @ 14:14:38

On the Zend Framework blog today Matthew Weier O'Phinney has written up a post showing how to implement specialized responses in Diactoros, a PHP package that makes it easier to work more directly with the request and response in your application.

When writing PSR-7 middleware, at some point you'll need to return a response.

Maybe you'll be returning an empty response, indicating something along the lines of successful deletion of a resource. Maybe you need to return some HTML, or JSON, or just plain text. Maybe you need to indicate a redirect.

But here's the problem: a generic response typically has a very generic constructor. [...] Fortunately, Diactoros provides a number of convenience implementations to help simplify the most common use cases.

He then spends the rest of the article showing some example responses types and the code to make them work:

  • EmptyResponse
  • RedirectResponse
  • TextResponse
  • HtmlResponse
  • JsonResponse

Each one is included as a part of the Diactoros package. For more information, check out the documentation for the package and accompanying examples.

tagged: tutorial diactoros package response example http zendframework

Link: https://framework.zend.com/blog/2017-08-24-diactoros-responses.html

Master Zend Framework:
How to Create a Zend Expressive Module
Jul 26, 2017 @ 11:14:52

On the Master Zend Framework site Matthew Setter has written up a tutorial showing you how to create a Zend Expressive module to help organize your application and keep related files all in one place.

I've been reflecting recently on the things that I commonly have to do when I begin building Zend Expressive applications. Of the list that I created, I found that one of the most common — and uninteresting — of them is setting up the rendering of static page content. [...] To solve this, I've usually manually created modules to handle the rendering of static page content. These module's usually had little more than a PageAction class that returned an HtmlResponse object, whose body was a rendered template file.

[...] If you're in a similar position to where I was and want to collate the logic into a reusable module, then follow along with me in this tutorial I'm going to step you through how to create a module which you can reuse across any Zend Expressive project.

He starts by briefly describing a "module" in the context of a Zend Expressive application and how they relate (or don't) to the modules in Zend Framework v2. He then starts in on the module creation, showing how to set up the environment, change the Composer configuration to autoload correctly and describes what the end result should look like. From there the tutorial starts on the code required for the classes covering the basics of the class, exception handling, configuration and the creation of a factory to make a module instance.

tagged: zendexpressive tutorial module static content zendframework

Link: http://www.masterzendframework.com/create-a-zend-expressive-module/

Alejandro Celaya:
Reusing factories in Zend ServiceManager
Jul 25, 2017 @ 10:03:33

Alejandro Celaya has a new post to his site showing the Zend Framework users out there how you can reuse factories in Zend/ServiceManager. Factories are heavily utilized by the component to create the objects the service returns. Factories tend to be single-use, however, but Alejandro has shown a way around that.

I like zend-servicemanager because it is explicit, and you are always in control of what's done, without loosing flexibility in the process. Since this container expects you to define factories for every service, you usually end up writing, testing and maintaining a lot of factories that doesn't add value to the application.

That's why it is so important to properly reuse factories when possible, not only because you will have to maintain less classes, but because the ServiceManager will instantiate less objects at runtime when it can reuse a factory.

He then talks about ways you can set up shared factories in your application including the use of an abstract factory class or a concrete factory to return other dependencies required. He also shows how to use the ConfigAbstractFactory that allows for the injection of dependencies based on a configuration (similar to the "wiring" in other dependency injection containers). Finally he shows the use of the ReflectionBasedAbstractFactory that handles the injection in about the same way but instead of basing it on a configuration it uses PHP's own reflection to try to determine the class and autoload it into the current system.

tagged: factory zend servicemanager zendframework configuration reflection abstract tutorial

Link: https://blog.alejandrocelaya.com/2017/07/21/reusing-factories-in-zend-servicemanager/

Master Zend Framework:
How to Migrate from Zend Expressive Version 1 to 2 with Command-Line Tooling
Jun 27, 2017 @ 10:43:30

On the Master Zend Framework site Matthew Setter has written up a new tutorial showing you how to migrate from Zend Expressive v1 to v2 with the help of some command line tooling support provided as a part of recent updates to the project.

In part one of this series, we started learning about the tooling support available for Zend Expressive, provided by Zend Expressive Tooling There, we learned how we can use the package to create, register, and deregister middleware, and scaffold new modules. But that's only half of what the package can do.

Here, in part two, let's learn about the other half, which removes some of the heavy lifting required when migrating Zend Expressive applications from version one to two.

He first defines some of the main differences between the two versions, a checklist of things the tooling will help you more automagically update. He talks more specifically about migration support, moving to "programatic pipelines" in Expressive v2 and scanning for deprecated error middleware. There's also information about locating the legacy request and response handling and how they're refactored to the newer format.

tagged: tutorial zendexpressive zendframework migrate version update tooling commandline

Link: http://www.masterzendframework.com/tooling/migrating-to-version-2/

Zend Framework:
Convert objects to arrays and back with zend-hydrator
Jun 21, 2017 @ 11:32:01

The Zend Framework blog has posted another in their series of component spotlights, focusing in on a single component of the framework and its use. In this latest article they cover the zend-hydrator component, useful for converting objects to arrays and back.

APIs are all the rage these days, and a tremendous number of them are being written in PHP. When APIs were first gaining popularity, this seemed like a match made in heaven: query the database, pass the results to json_encode(), and voilà! API payload! In reverse, it's json_decode(), pass the data to the database, and done!

Modern day professional PHP, however, is skewing towards usage of value objects and entities, but we're still creating APIs. [...] Zend Framework's answer to that question is zend-hydrator. Hydrators can extract an associative array of data from an object, and hydrate an object from an associative array of data.

They start with the command to get the zend-hydrator package installed (and a dependency they'll need for their examples, zend-filter). A code example is included that shows how to convert a "book" object to an array using the ReflectionHydrator. Next is an example of switching it back, changing the array of data back into a book object. Next comes the integration with zend-filter, showing how to filter values out of objects/arrays you might not want in the end result by adding the filter to the hydrator. Also included are examples of modifying data (strategies), filtering on property names, delegation of the translation based on object type and a few other features included in the component that could be helpful.

tagged: zendframework component zendhydrator tutorial introduction filter translate

Link: https://framework.zend.com/blog/2017-06-21-zend-hydrator.html

Rob Allen:
Simple way to add a filter to Zend-InputFilter
Jun 21, 2017 @ 09:16:29

Rob Allen has a quick new post to his site sharing a simple way to add a filter to the Zend-InputFilter component when it's in use on your site.

Using Zend-InputFilter is remarkably easy to use. [...] How do you add your filter to it though?

He starts with an example of putting the component to use in requiring and filtering the value in "my_field" for the data provided. He then shows how to add his "simple filter that does absolutely nothing", the MyFilter, to the current set. He also shows the creation of a "filter factory" class that registers the custom filter into the chain with an alias of "MyFIlter". You can then use it just like you would any other filter and define it in your rules specification.

tagged: zendframework zendinputfilter component custom filter tutorial factory

Link: https://akrabat.com/simple-way-to-add-a-filter-to-zend-inputfilter/