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

Alejandro Celaya:
My thoughts after migrating some projects to Zend Expressive 2
Mar 28, 2017 @ 10:15:20

Alejandro Celaya has a new post to his site sharing some of his thoughts after migration applications to Zend Expressive 2 and some of his experiences along the way upgrading to this latest version.

The day Zend Expressive 2 was released I was super excited. I have been using it a lot for both professional and personal projects, so I'm quite used to it.

Since I've been using it in many projects, being able to update all of them to version 2 was a challenge, but I can say, I have succeed.

He talks about the projects themselves first, his own site at alejandrocelaya.com and shlink.io, and what kind of functionality they have. He then briefly covers the process to get them migrated and some of the changes he needed to make including:

  • adding an error hander
  • moving to the new error handling middleware strategy
  • using the support for interop middleware (single-pass)
  • small router changes due to using a custom router

He ends the post looking at the shift in programmatic approach Zend Expressive 2 uses (versus v1 handling) and changes he made to his middleware handling to reflect it.

tagged: zendexpressive2 zendexpressive upgrade application process changes

Link: https://blog.alejandrocelaya.com/2017/03/27/my-thoughts-after-migrating-some-projects-to-zend-expressive-2/

Zend Framework Blog:
Migrating to Expressive 2.0
Mar 14, 2017 @ 12:36:17

The Zend Framework blog has a tutorial they've posted showing you how to migrate up to Zend Expressive v2.0 from a v1.x application and some of the things that could break along the way.

Last week, we released Expressive 2. A new major version implies breaking changes, which often poses a problem when migrating. That said, we did a lot of work behind the scenes to try and ensure that migrations can happen without too much effort, including providing migration tools to ease the transition.

In this tutorial, we will detail migrating an existing Expressive application from version 1 to version 2.

Using this repository for a testbed, they walk through the steps for the update:

  • Creating a migration branch
  • Updating dependencies
  • using the zendframework/zend-expressive-tooling to aid in the migration
  • scanning your code to find possible breaking locations
  • moving over to the programmatic pipeline structure

They also include some bonus information covering self-invoking functions, the zend-config-aggregator and development mode. The post ends with the updates you'll need to make to middleware to implement the http-interop/http-middleware MiddlewareInterface.

tagged: zendframework zendexpressive v2 migration guide tool

Link: https://framework.zend.com/blog/2017-03-13-expressive-2-migration.html

Alejandro Celaya:
Managing PUT requests with file uploads in psr-7 and middleware PHP applications
Mar 07, 2017 @ 13:17:01

Alejandro Celaya has posted a new tutorial to his site covering the handling of PUT requests in PSR-7 applications for file uploads via middleware.

It has been a long time since I first realized that handling file uploads in non-POST requests (like PUT) wasn't an easy task. One could assume the $_FILES array should be populated regardless the HTTP verb, but actually, PHP doesn't do it on its own.

After a long time wanting to find a solution to this problem, I've finally dedicated the time to get something functional, that allows file uploads to be transparently handled regardless the HTTP verb (it works the same way in POST, PUT and PATCH requests).

Since nowadays I try to work with psr-7/middleware based applications, I have created a Zend Expressive app that registers a middleware capable of parsing a multipart/form-data request body, populating the request's uploaded files array and parsed body array. This way, you can call $request->getUploadedFiles() or $request->getParsedBody() in any PUT or PATCH action, the same way you would do in a POST action.

His example application shows a simple HTML form that, when submitted, changes the HTTP request type based on a radio option selected at the bottom. He walks through the steps that the application takes to handle the upload via this middleware that makes it possible to work with the body of the PUT the same way as other requests. He goes through each part of the code that's required to make the middleware flow work and finishes up the post looking at a few other things to consider (like opting for POST over PUT for file uploads).

tagged: zendexpressive application tutorial psr7 middleware put request fileupload upload

Link: https://blog.alejandrocelaya.com/2017/03/06/managing-put-requests-with-file-uploads-in-psr-7-and-middleware-php-applications/

Zend Framework Blog:
Announcing Expressive 2.0
Mar 07, 2017 @ 11:43:29

On the Zend Framework blog today Matthew Weier O'Phinney has posted the official announcement of the release of Zend Expressive v2.0, the latest major release with several large changes.

Today we're excited to announce Expressive 2.0! What has changed since 1.0 was released last year? The short version: we've been providing changes that standardize, simplify, and streamline application development in Expressive.

Updates in this latest version include:

  • PSR-11 (ContainerInterface) support
  • Programmatic pipelines
  • An emphasis on modularized applications
  • Extensible routing and dispatch middleware

The post gets into details on each of the above topics and several others. It also shows how to install this latest version via Composer (or install the skeleton application to get up and running quickly).

tagged: zendframework zendexpressive v2 release announcement major version

Link: https://framework.zend.com/blog/2017-03-07-expressive-2.html

Robert Basic:
Using Doctrine DBAL with Zend Expressive
Dec 22, 2016 @ 11:19:53

Robert Basic has written up a quick post to his site sharing details on how you can use Doctrine's DBAL with Zend Expressive without having to use the entire Doctrine ORM. DBAL is Doctrine's abstraction layer that makes it easier to work with your database at a higher level than writing manual SQL statements.

The database abstraction and access layer — Doctrine DBAL — which I prefer over other abstraction layers like Zend DB. My good friend James, aka Asgrim, has written already how to integrate Zend Expressive and Doctrine ORM.

But what if want to use only the DBAL with Zend Expressive, and not the entire ORM? It’s pretty easy as all we need to do is write one short factory that will create the database connection using the connection parameters we provide to it.

He includes the code snippet you'll need to define a "ConnectionFactory" to set up the connection and the configuration needed to allow it to connect to the database. He then shows how to set up the DI container with the new container factory as a dependency and use it by pulling the "db" object out of the container.

tagged: doctrine dbal zendexpressive tutorial example factory

Link: https://robertbasic.com/blog/using-doctrine-dbal-with-zend-expressive/

Master Zend Framework:
How to Simplify Expressive Configuration with Interop-Config
Nov 17, 2016 @ 09:58:59

On the Master Zend Framework site there's a tutorial posted showing you how to simplify your Zend Expressive configuration with the help of the interop-config package.

Zend Expressive (and Zend Framework) are great frameworks, ones designed not to constrain you in almost any way. You’re in charge. You set the scene. You make it do just what you want it to do. Unlike other frameworks, you’re not bound to work with a specific way. You’re free to work in, almost, whatever way you want. But that comes at a price.

Consequently, using Zend Expressive can give you too much freedom — especially when it comes to configuration. That’s why I was happy to hear about Interop-Config some time ago from my friend Sandro Keil.

Interop-Config is a library which helps ensure that you have a valid configuration for your code. It can provide default options, as well as enforce mandatory options, ensuring that it has a well laid out structure, and is easy to understand.

He starts by briefly talking about the package itself and what kinds of features it brings along with it. The tutorial then shows how to get the package installed and a simple base configuration. With that in place it then shows you how to access this configuration via the "ConfigurationTrait" and a "dimensions" method. From there you can then easily get configuration data from the DI container (and see if it exists with a "canRetrieveOptions" method). The post finishes up showing you how to add default values, making options mandatory and a bit about defensive programming methodologies in using the tool.

tagged: zendexpressive configuration interopconfig package tutorial zendframework

Link: http://www.masterzendframework.com/simplify-expressive-configuration-with-interop-config/

Master Zend Framework:
Whoops, I Forgot The Error Handler
Sep 21, 2016 @ 12:57:32

The Master Zend Framework site has posted a new tutorial for those out there looking for a bit more from their error handler than just some basic text. In this tutorial Matthew Setter introduces you to the Whoops error handler and how to use it in your Zend Expression application.

Ever experienced HTTP 500’s, but found that your error logs are empty. Ever had no clue why or how this could be happening? Perhaps you forgot to enable the Whoops error handler.

That’s right, perhaps, when you were setting up a Zend Expressive application, you made the mistake which I made recently when you used the Zend Expressive Skeleton Installer. [...] If you chose n, and used a templating engine, then TemplatedErrorHandler, would have been used as PHP's default exception handler.

As a result, no exceptions will be written in your logs. Sure, you’ll see that a 500 error has occurred somewhere in your application. But, the only information you’ll have is [a simple 500 error message].

He notes that the next step for most developers is the log files, trying to find a hint there of what might have broken. If you chose the default logger, nothing will be there as it captures those issues and pushes them to the basic error template (but doesn't output them). He points to where in the configuration you can check to see if you enabled the Whoops error handler and how to test it after you've made the switch. The reason the default is the basic message is that, in production, you don't want information exposed and log messages/code shown to just anyone - that's a big security risk.

tagged: whoops error handler zendexpressive enabled tutorial output

Link: http://www.masterzendframework.com/whoops-errorhandler/

Master Zend Framework:
The 3-Step Guide to Downloading Files in Zend Expressive
Aug 29, 2016 @ 10:37:44

On the Master Zend Framework site there's a tutorial posted showing you how to set up file download functionality in a Zend Expressive-based application.

A common requirement of web-based applications is to upload and download files. But, out of the box, there’s no simple way to download them in Zend Expressive. This tutorial shows you how - step-by-step.

Recently, I was asked on Twitter by @dgoosens, about how to download files using Zend Expressive. The timing was pretty good, as I’d done a simple implementation in a recent Zend Expressive project. So I knocked up a quick example and he, @acelayaa, and I talked it over, making various changes and suggestions along the way.

So, In today’s tutorial, I’m going to walk through a 3-step process for downloading files when using Zend Expressive.

He breaks the process down into three parts:

    1. The Download Functionality
    1. Using the Download Method
    1. Running the Application & Downloading the File

He includes code or commands for each step, showing you exactly how to set up this simple piece of functionality. Additionally it's implemented as a (mostly) self-contained method using the Stream handler to set the required headers and body.

tagged: zendframework zendexpressive file download tutorial stream

Link: http://www.masterzendframework.com/downloading-files-in-zend-expressive/?platform=hootsuite

Master Zend Framework:
How To Use Laravel’s Eloquent ORM with Zend Expressive
Aug 25, 2016 @ 11:43:47

On the Master Zend Framework site there's a new tutorial posted showing you how to integrate Laravel's Eloquent database library with Zend Expressive to replace the usual options of Doctrine or ZendDb.

When working with Zend Expressive, if you were to choose a database layer for your application, I guess that the first two names to come to mind would be either ZendDb or Doctrine. But what about Laravel’s ORM: Eloquent? That’s right, Eloquent!

I’ve been getting to know Laravel a bit better recently, primarily for the comparison I wrote about it and Zend Expressive not that long ago. The tutorial was exceptionally well received. So I thought I’d dig a bit further, and see if I could use Eloquent instead of my standard ZendDb. It turns out, with a bit of work, you can do it!

In today’s tutorial, I’m going to step you through how to make it available, using a project based off of the Zend Expressive Skeleton Installer.

He starts by installing the Eloquent package and setting it up with a simple configuration (using a Homestead environment). With that configuration set up, he shows how to make the Eloquent connection available to the rest of the application. Once the connection is set up he shows how to create a first model - Capital - and using it to create a new record. He also includes examples of retrieving all records and mentions three things that seem missing from this Eloquent install: the "artisan" command, migrations and database seeing functionality.

tagged: zendframework zendexpressive eloquent orm laravel integration tutorial

Link: http://www.masterzendframework.com/using-eloquent-with-expressive/

Master Zend Framework:
How To Simplify Zend Expressive Configuration with ConfigProviders
Aug 15, 2016 @ 12:42:19

The Master Zend Framework site has a new tutorial posted helping you simplify the configuration on your Zend Expressive application with the help of ConfigProviders, a handy feature that lets you split up the configuration into logical "chunks" as PHP classes.

Given Zend Framework’s design (and accompanying flexible nature), this [configuration complexity] can easily be the case if we’re not careful. [...] Specifically, we’ll likely end up with a config/autoload directory polluted with a plethora of configuration, including for dependencies, routing, and middleware.

[...] As it turns out, this was something which was already identified by other developers, including the Zend Framework contributors. [...] In there, he mentioned ConfigProvider classes as a simple way of enabling ZendForm ViewHelpers, which aren’t enabled by default in Zend Expressive. As I looked at the composition of the file, I realized that this was the answer I needed to solve the configuration issue I created for myself.

A screencast is included in the post showing off the solution but the code an explanation are below that as well for those more interested in reading than watching a video. He walks you through the creation of the configuration provider including setting up the dependency configuration, updating the route handling and, finally, actually using the provider in your global configuration.

tagged: zendexpressive configuration provider configprovider tutorial screencast example

Link: http://www.masterzendframework.com/configproviders-classes/