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

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/

Auth0 Blog:
Building an app with Nette and adding authentication
Sep 21, 2017 @ 10:07:59

On the Auth0 blog there's a tutorial posted that shows you how to build an application with the Nette framework and easily integrate authentication via their own JSON web token functionality.

Nette is a free, open-source PHP framework designed for building web applications. Nette is a set of decoupled and reusable PHP packages that will make your work easier. And Nette is also known as the quick and comfortable web development framework in PHP because it has the tools that allow you to bang out PHP applications rather quickly.

[...] In this tutorial, I'll show you how easy it is to build a web application with Nette and add authentication to it. Check out the repo to get the code.

The tutorial starts by talking about some of the built-in tools the framework includes and some basic use of each. It then gets into building the application, creating the controller, view and template to output a list of characters from Game of Thrones. From there it moves into the authentication piece, showing the integration of the Auth0 plugin and how to hook it into an application on their platform. There's a bit of configuration to set it up as an authenticator but then the framework and the plugin take care of the rest.

tagged: nette framework authentication tutorial auth0 jsonwebtoken json jwt

Link: https://auth0.com/blog/building-an-app-with-Nette-and-adding-authentication/

AWS Developer Blog:
Automated Changelog in AWS SDK for PHP
Sep 01, 2017 @ 10:17:08

On the AWS Developer blog they've posted about a new update in their PHP SDK functionality: a "changelog builder" that helps with automated changelog generation.

Starting with version 3.22.10 of the AWS SDK for PHP, released February 23, 2017, the Changelog Builder automatically processes all changelog entries. Each pull request is required to have a changelog JSON blob as part of the request. The system also calculates the next version for the SDK based on the type of the changes that are defined in the given changelog JSON blob.

The update simplifies the process of adding release notes to the CHANGELOG.md file for each pull request. Each merged pull request that was part of the release results in a new entry to the CHANGELOG.md file. The entry describes the change and provides the TAG number and release date.

This changelog is generated from a required JSON document for each pull request that provides information about the type of change, category and a brief description. They explain each of these items to give a little more context as to what they should contain along with a few examples.

This is something that could definitely help to improve other libraries as well, gathering the required change information from the contributor rather than having a project administrator have to sift through the PR to locate all changes.

tagged: aws sdk automated changelog generation json requirement pullrequest

Link: https://aws.amazon.com/blogs/developer/automated-changelog-in-php-sdk-for-aws/

Mohamed Said:
Laravel/MySQL JSON documents faster lookup using generated columns
Aug 22, 2017 @ 10:55:05

Mohamed Said has an interesting post to his site showing how to use computed columns for faster lookups in a MySQL database containing JSON documents.

Laravel 5.3 is shipped with built-in support for updating and querying JSON type database fields, the support currently fully covers MySQL 5.7 JSON type fields updates and lookups.

He includes an example of using the Eloquent DB handling to search for the data in the JSON and the results. He links to more information about the feature, following it up with an example of the main topic: computed columns. In it he shows how to create (on the SQL side) a generated/computed column that allows for easier extraction of the data directly from the JSON. This makes it even simpler to get the data just as you would in a normal select. He also includes an example of creating it via the Schema functionality in the database migrations.

tagged: mysql json column lookup computed generated sql data laravel tutorial

Link: https://themsaid.com/laravel-mysql-json-colum-fast-lookup-20160709

/Dev/Hell Podcast:
Episode 92: JSON and the Argonauts
Jul 13, 2017 @ 09:24:50

The /Dev/Hell podcast, hosted by PHP community members Chris Hartjes and Ed Finkler, has posted their latest episode: Episode #92 - JSON and the Argonauts.

Chris and Ed talk about their thoughts on what might have to change once you become “internet famous” and a reminder that even if you have made mistakes in the past, you can choose to correct them in the future.

We also include a new segment called “What toys did we buy?” to reflect how the hosts numb the pain of every day living through conspicuous consumption.

Thanks to Chance Garcia for allowing us to bring another health issue to people’s attention.

You can listen to this latest episode either using the in-page audio player or by downloading the mp3 directly. If you enjoy the show, be sure to subscribe to their feed and follow them on Twitter for updates when new shows are released.

tagged: devhell podcast ep92 json argonauts chrishartjes edfinkler

Link: http://devhell.info/post/2017-06-28/json-and-the-argonauts/

SitePoint PHP Blog:
Functional Programming with Phunkie: Building a PHP JSON Parser
Jun 09, 2017 @ 12:12:29

The SitePoint PHP blog has a new tutorial posted from author Marcello Duarte showing you how to use functional programming techniques in your applications with the help of the Phunkie library. Phunkie is a library that brings functional programming functionality to PHP. This article was originally published on the Inviqa blog.

In the first part of this series we explored parsers and combinators to help you start getting values from functional programming with PHP. We covered the basics using examples, and now we’ll move onto more sequencing and other strategies.

Continuing on, they work towards the goal of making a more useful end result, a JSON parser that returns a JsonObject. First, though, he goes through several different combinators, showing code examples for each: sequencing, choice, and recursive. He also covers the repetition pattern and how to integrate separators. Finally, with this groundwork laid, he gets to the JSON parser showing each step of the way, from reading in the JSON string to returning the object.

tagged: functional programming phunkie json parser combinator tutorial

Link: https://www.sitepoint.com/functional-programming-phunkie-building-php-json-parser/

Laravel News:
Generate a JSON Feed with Laravel
May 24, 2017 @ 10:49:46

In a new tutorial posted to the Laravel News site Eric Barnes shows you how to easily create JSON feed output from the data in a Laravel application. JSON feed is a new standard that's been defined to try to bring more structure to JSON responses for feeds, following in the tradition of RSS feed structure.

JSON Feed is a new standard to formalize a JSON based RSS feed that aims to simplify creating feeds by doing away with the XML standard. Implementing a feed for your site is simple, and the spec is surprisingly clear.

[...] I just implemented this feed format for Laravel News, and you can find the results here. As a quick example let’s go through setting up a feed like this.

He then walks you through the code required to create the feed, sharing a listing of posts (the latest 20) from the Laravel News site. He then defines the main feed data, adds in each of the posts to the "items" set and returns the resulting array for translation into JSON output.

tagged: jsonfeed standard laravel tutorial output json feed

Link: https://laravel-news.com/generate-a-json-feed-with-laravel

Zend Framework Blog:
Error Handling in Expressive
Mar 24, 2017 @ 09:30:31

The Zend Framework blog has a new tutorial posted by Matthew Weier O'Phinney covering error handling techniques in Expressive with a few examples making use of some custom middleware and logic.

One of the big improvements in Expressive 2 is how error handling is approached. While the error handling documentation covers the feature in detail, more examples are never a bad thing!

In their example they're creating an API resource that returns a list of book details (ones the user has read). The goal is to use the existing error handling for everything except the custom exceptions they want to throw but keep with the JSON handling throughout. First the middleware to handling the API request is shown, complete with sorting and pagination. Then come the custom exception examples for invalid requests and server issues. These exceptions are then put into the Problem Details format with the help of another middleware. This then all tied together with the nested middleware handling Expressive provides and an example of the end result is included.

tagged: error handling expressive custom problemdetails tutorial json middleware

Link: https://framework.zend.com/blog/2017-03-23-expressive-error-handling.html

Leonid Mamchenkov:
Validating JSON against schema in PHP
Mar 13, 2017 @ 11:13:53

In this new post to his site Leonid Mamchenkov talks about validating JSON content sent to your application to ensure it matches a JSON schema definition.

GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). Staring at a slowly scrolling list of installed dependencies, I noticed something interesting.

Of course, I’ve heard of the seld/jsonlint before. It’s a port of zaach/jsonlint JavaScript tool to PHP, written by Jordi Boggiano, aka Seldaek, the genius who brought us composer dependency manager and packagist.org repository.

But JSON schema? What’s that?

From there he gets into a high level definition of what JSON schema is and the similarities it has to XML's structure. He then links to a PHP package that can help with the validation and how to use it (defining a simple JSON file, a schema and the code to perform the evaluation).

tagged: json schema validation package composer tutorial

Link: http://mamchenkov.net/wordpress/2017/03/11/validating-json-against-schema-in-php/http://mamchenkov.net/wordpress/2017/03/11/validating-json-against-schema-in-php/

SitePoint PHP Blog:
PHP Fractal – Make Your API’s JSON Pretty, Always!
Feb 27, 2017 @ 10:28:55

The SitePoint PHP blog has a tutorial posted by author Younes Rafie covering the use of the Fractal library from The PHP League to create "pretty" JSON API output.

If you’ve built an API before, I’ll bet you’re used to dumping data directly as a response. It may not be harmful if done right, but there are practical alternatives that can help solve this small problem.

One of the available solutions is Fractal. It allows us to create a new transformation layer for our models before returning them as a response. It’s very flexible and easy to integrate into any application or framework.

They use a Laravel application as the base, creating a simple database of user information and relating users to roles. The tutorial then starts in on using Fractal, building out "transformers" for the data. These transformers take in the data/object and, inside of a "transform" method, modify the output and return a "prettier" version. They show it in use in a controller, outputting a collection of user data, only returning the name and email values. The tutorial also covers a few other Fractal-related topics including pagination, including sub-resources and eager loading.

tagged: fractal api json output transform tutorial

Link: https://www.sitepoint.com/php-fractal-make-your-apis-json-pretty-always/