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

Nwanze Franklin:
Deep dive into middlewares in Laravel
Dec 14, 2017 @ 12:46:48

Nwanze Franklin has posted a tutorial to the Dev.to site sharing a deep dive into middlewares in Laravel. Middleware is a powerful tool that can allow you to work with the request and response objects in your application in a more reproducible and contained manner.

What is a Laravel middleware? It is a feature in Laravel which provides a mechanism for filtering HTTP requests entering your application. This allows you to hook into Laravel request processing work flow to perform some kind of logic that decides how your application works.

What would you use middleware for? Protecting your routes, setting headers on HTTP responses, logging requests to your application, sanitizing incoming parameters, enable site-wide maintenance mode [and] manipulating responses generated by your application.

The tutorial then starts in on the code, showing how to create a custom middleware and the code that's generated by the artisan command. It covers the differences between global and route middleware, how to register a middleware and assigning it to a route. It ends with a look at using parameters in middleware and how to access them from the controller.

tagged: middleware laravel tutorial introduction framework route global

Link: https://dev.to/franko4don/deep-dive-into-middlewares-in-laravel-doo

TJ Miller:
An Approach to Testing Middleware
Nov 29, 2017 @ 09:06:19

TJ Miller has posted an article where he shares some suggestions about testing middleware in your applications. Since middleware is usually only executed as a part of the normal request/response cycle it is difficult to test in isolation.

I’ve always tested middleware in one of two ways, a unit test with mocks and asserting the callback in the handle method or integration tests on application routes.

[...] I wanted to find a simple approach that verified the following: middleware is active and configured correctly (global, group, single use) [and that the] middleware is functioning correctly

He briefly discusses the difference between unit testing and integration testing before showing how to set up unit tests for a Laravel-based middleware: a check for a JSON response, testing to ensure logging is working as expected and that a middleware is correctly assigned to a route.

tagged: testing unittest integration middleware laravel tutorial

Link: https://medium.com/@sixlive/an-approach-to-testing-middleware-c547fc942848

Rob Allen:
Implementing CORS in Zend Expressive
Nov 15, 2017 @ 09:20:13

In a new post to his site Rob Allen shows you how to implement CORS in a Zend Expressive application through the use of a simple middleware wrapper that sends the appropriate headers.

On a recent project, I needed to implement CORS support for my Expressive API. The easiest way to do this is to use Mike Tuupola's PSR-7 CORS Middleware.

As this is a standard Slim-Style PSR-7 middleware implementation, we need to wrap it for Expressive, so we make a factory. [...] We then register this in our AppConfigProvider::getDependencies() by adding to the factories key.

He includes the code and configuration changes required to make it all work and includes example output of a request (with headers) from a curl call to the API. He also includes a section on working with JSON error responses and ProblemDetails for when there are issues related to the current CORS policy definition.

tagged: cors tutorial zendexpressive middleware json error problemdetails

Link: https://akrabat.com/implementing-tuupola-cors-in-expressive/

Rob Allen:
OuputBuffer Middleware for Expressive
Oct 25, 2017 @ 09:56:14

In a quick post to his site Rob Allen shares code for an OutputBuffer middleware for use with the Zend Expressive framework. Zend Expressive is a middleware-driven framework that makes use of several Zend Framework components to get the job done.

When developing an Expressive application, if you use var_dump(), echo, print_r(), etc when you get [an] error.

This occurs after your data has been displayed and makes perfect sense as Expressive is unable to send headers as you've already started sending the body. To solve this, you need some middleware.

He includes the code for the middleware that grabs the current output being pushed to the user and, using PHP's own output buffering, takes all output and pushes it into the body, preventing any messages about output before the rendering takes place.

tagged: zendexpressive outputbuffer output middleware example

Link: https://akrabat.com/ouputbuffer-middleware-for-expressive/

Laravel News:
Testing Laravel Middleware with HTTP Tests
Sep 28, 2017 @ 12:10:05

On the Laravel News site today there's a tutorial posted showing you how to test your middleware with HTTP tests and how they can help in more practical testing of your application.

In this post, I’d like to demonstrate a practical example of testing a middleware using HTTP tests. Testing at the HTTP level can make your tests more resilient to change and more readable.

On a recent episode of Full Stack Radio (#72) with Adam Wathan and Taylor Otwell, it was refreshing to hear them find a lot of practical value in HTTP testing. I have found HTTP tests to be easier to write and maintain, but did feel like I was Doing Testing Wrong™ somehow or that I was cheating by not mocking and isolating everything. If you haven’t listened to this episode yet, give it a listen, it’s full of good, practical testing advice.

He starts by laying out the example middleware he wanted to test, one for validating and securing webhooks coming back from the Mailgun service to handle incoming email. The code for the middleware is included, defining the handle, buildSignature and verify methods. He then uses the artisan command to make a test and provides the code to test and ensure that the middleware forbids requests that aren't POST.

tagged: laravel http testing middleware mailgun webhook post tutorial

Link: https://laravel-news.com/testing-laravel-middleware

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

TutsPlus.com:
Understand the Basics of Laravel Middleware
Jul 26, 2017 @ 12:53:38

On the TutsPlus.com site there's a tutorial recently posted sharing the basics of middleware in Laravel applications - reusable pieces of code that operate on the request and response during the execution of your code.

In this article, we'll dive deep into the Laravel framework to understand the concept of middleware. The first half of the article begins with an introduction to middleware and what it's actually used for.

As we move on, we'll cover how to create custom middleware in a Laravel application. After creation of your custom middleware, we'll explore the options available to register it with Laravel so that it could be actually invoked during the request processing flow.

The tutorial starts by explaining what middleware is and how it functions in the flow of the Laravel application (as well as possible uses). It then starts in on the creation of custom middleware designed to redirect mobile users to the "mobile site" for the service. With the test for that in place the post then shows how to apply the middleware to the application on a route.

tagged: laravel middleware introduction custom mobile example

Link: https://code.tutsplus.com/tutorials/understand-the-basics-of-laravel-middleware--cms-29147

Master Zend Framework:
Create Modules and Middleware with Command-Line Tooling Support
Jun 12, 2017 @ 10:44:05

The Master Zend Framework site has posted a new tutorial covering an aspect of the Zend Expressive project that doesn't seem to get enough coverage: the tooling included with the skeleton application.

For the longest time, Zend Framework hasn't had the strongest command-line tooling and scaffolding support. In stark contrast, other frameworks — especially Laravel, and its excellent Artisan command — have had far stronger tooling support and as a result have been far easier to build projects with. However, that's all changed!

Since the release of Zend Expressive Skeleton 2.0.2, Zend Expressive's command-line tooling support has been rapidly developing. It's provided through a package called, aptly, Zend Expressive Tooling.

The post briefly describes what features the tooling brings and then gets right into the installation process (via Composer). The tutorial then walks you through the commands it provides to create middleware, modules and handle migration to latest versions of the framework. The module and middleware topics are then covered more in-depth, showing code and commands required.

tagged: zendexpressive tooling middleware module commandline tutorial

Link: http://www.masterzendframework.com/tooling/create-modules-and-middleware/

Zend Framework Blog:
Authorize users using Middleware
May 04, 2017 @ 09:40:56

Previously the Zend Framework blog posted a tutorial showed you how to authenticate a user with middleware in a Zend Expressive application. In this latest post in the series they move on to the next step: authorization.

In a previous post, we demonstrated how to authenticate a middleware application in PHP. In this post we will continue the discussion, showing how to manage authorizations.

We will start from an authenticated user and demonstrate how to allow or disable actions for specific users. We will collect users by groups and we will use a Role-Based Access Control (RBAC) system to manage the authorizations.

To implement RBAC, we will consume zendframework/zend-permissions-rbac.

They start by pulling in the Zend Expressive tooling support and creating the Permission module. The tutorial then gets into the basics of using a role-based access control system and the roles they're going to define: admin, editor and contributor (as well as what each can do). The roles are then defined in a configuration file and briefly explained. Next is the creation of the middleware to authorize the user and its action in the application, performing a check against the roles of the user on the requirements of the action. There's also an example of setting up role inheritance and how to configure the resulting route to fire off the authorization handling.

tagged: zendexpressive middleware authorization zendrbac rbac rolebased accesscontrol tutorial

Link: https://framework.zend.com/blog/2017-05-04-authorization-middleware.html

Medium.com:
Using Guzzle 6 Middleware in a Laravel Application
May 03, 2017 @ 11:10:36

In this recent post on Medium.com author Paul Redmond shows how to use Guzzle 6 middleware in a Laravel application instead of the framework's own functionality.

The most significant change between Guzzle 5 and 6 is moving away from the event system I grew so accustomed to in Version 5 to middleware in version 6. Needless to say, it was a big adjustment for me at first and it felt like a downgrade. After my initial grumbling, the upgrade guide explains the reasoning for the change.

[...] I prefer to keep my dependencies as up-to-date as possible so I decided to learn Guzzle 6 and become more familiar with the middleware. The concepts are pretty straightforward and I have a few patterns that I like to use when building out middleware within my Laravel applications.

He then shares some code he's used to generate an authorization header and how to add it into the Laravel application as a service using the "tagged" middleware functionality. Finally he shows it in use making a simple request to the endpoint and showing the response results, including the authorization header.

tagged: guzzle middleware laravel application tutorial tagged integration

Link: https://medium.com/@paulredmond/using-guzzle-6-middleware-in-a-laravel-application-7fbd6d966235