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

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/

Robert Basic:
Events in a Zend Expressive application
Aug 05, 2016 @ 09:40:47

Robert Basic has written up a new post sharing a method he came up with for event handling in a Zend Expressive application. He makes use of Zend's own EventManager component to integrate it with some of his work from a previous post.

Three weeks ago I wrote a post on how to utilize Tactician in a Zend Expressive application. Today I want to expand on that post a little by adding the possibility to trigger and listen to events using the Zend EventManager component.

Using events allows our application to respond to different events that occur during a request.[...] This allows for a better separation of concerns in some cases, because if we take this approach, our code that deals with [the current functionality] doesn’t care any more what happens after that

He then gets into the code, showing how to install the EventManager component and how to create/inject an event manager into a current object (a Command). He then shows how to attach en event to the handler and perform an action when the event is hit. He points out one issue with this kind of setup, however: the need for all dependencies to be created prior to the event being attached. Fortunately the Zend EventManager comes with DI container support making it simpler to access dependencies needed during the firing of the event.

tagged: zendexpressive framework application event eventmanager tutorial overview introduction

Link: https://robertbasic.com/blog/events-in-a-zend-expressive-application/

Alejandro Celaya:
Creating a content-based Error Handler for Zend Expressive
Jul 29, 2016 @ 09:26:38

In a post to his site Alejandro Celaya shares a method he worked up for creating a content-based error handler in Zend Expressive - a method of changing the error output based on the content it was passed and the Accept header provided.

In one of my tests of the REST API I saw that when an error occurs (404, 405 or 500), I was getting an HTML response, which is not easy to handle when the client is expecting JSON.

I started to dig on how to fix this problem and thought that using ErrorMiddleware (which is invoked in case of an error) should be the solution, but after some tests I saw that it is only invoked if a regular middleware invokes the next one by passing an error as the third argument or an uncaught exception is thrown. When a route is not matched (404) or it is matched with an incorrect HTTP method (405), the error middleware is not invoked.

After confirming (on Twitter) that this was the intended result he went about looking for another option. He looked into using "Final Handlers" that are called when nothing else matches in the middleware execution chain. They didn't provide one for JSON handling, however, so he had to create his own (code is included in the post) and explains a bit of how it's handling the data and HTTP response code. Unfortunately using this handler made the error output always return JSON so another piece was needed, the content-based detection handler that switches between types based on the Accept header.

tagged: content error handler zendexpressive tutorial json output

Link: http://blog.alejandrocelaya.com/2016/07/29/creating-a-content-based-error-handler-for-zend-expressive/

Master Zend Framework:
Can You Create Apps in Zend Expressive as Easily as With Laravel?
Jul 27, 2016 @ 13:23:01

On the Master Zend Framework site Matthew Setter has written up a post that tries to answer the question: "can you create Zend Expressive apps as easily as with Laravel?" He works through each of the major features in the frameworks (controllers, routing, views, etc) and compares the two and how easy they make it for the developer.

Laravel is the PHP framework For web Artisans, able to create applications nary with the speed of thought. Zend Framework, on the other hand, is the enterprise-ready framework; one that can build the largest of applications, for companies in the Fortune 500. I was asked, recently, if applications could be built as easily in Zend Expressive as in Laravel. Recently I sought to find out. Here's what I found.

He starts off with more of an "overview comparison" of his own experience building a simple application with Laravel and how, interestingly, there's not an easy way to make a direct comparison between the two. He also mentions picking the right tool for the job and not "fitting a square peg in a round hole" or trying to use the same development practices between the two. From there he then gets into more of the specifics of the features:

  • Forms and Entities (and Form ViewHelpers)
  • Routes and Controllers
  • View Helpers
  • Database Access
  • Data Models
  • Database Migrations and Testing
  • Testing

Each of the sections comes with some brief code snippets and examples from either side of the fence, helping you get a better idea of how they differ. He finishes off the post sharing his own opinions on the comparison between the two....but you'll have to read the article to find out about those.

tagged: laravel zendexpressive framework comparison features easy

Link: http://www.masterzendframework.com/zend-expressive-or-laravel/

Alejandro Celaya:
Project Scalability with Zend Expressive
Jul 21, 2016 @ 09:46:10

In a new post to his site (also posted on the Zend Developer Zone) Alejandro Celaya looks at scalability with Zend Expressive, a lightweight framework from Zend, the creators of the Zend Framework.

I've been working with some different frameworks lately. One of them is Zend Expressive, and I've come to the conclusion that I don't need to choose between different frameworks; depending on the project, Expressive always fits my needs and scales from small projects to bigger applications.

He starts off by looking at the "microframework approach" that Zend Expressive takes, making it easier to get up and running for smaller applications. He points out that this setup is fine when the application is small, but what happens as it grows - it just wouldn't scale well and be manageable. He talks about the setup he uses for larger scale applications, moving the configuration to dynamic config files and making use of more complex dependency injection. He also talks some about modularity in applications, the "middleware paradigm" and how he set up controller-style dispatching (versus just the default closures method).

tagged: zendexpressive tutorial scalability framework microframework update functionality

Link: http://blog.alejandrocelaya.com/2016/07/21/project-scalability-with-zend-expressive/