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

Zend Framework Blog:
Caching middleware with Expressive
Apr 19, 2017 @ 12:12:32

On the Zend Framework blog Enrico Zimuel has posted a tutorial showing you how to use middleware caching in Zend Expressive to help increase the overall performance of your application.

Performance is one of the key feature for web application. Using a middleware architecture makes it very simple to implement a caching system in PHP.

The general idea is to store the response output of a URL in a file (or in memory, using memcached) and use it for subsequent requests. In this way we can bypass the execution of the previous middlewares starting from the second request.

Of course, this technique can only be applied for static contents, that does not require update for each HTTP request.

He shows how to implement the "cachable" middleware, starting with a version that caches the return content based on the URL requested. He then shows how to configure the caching system to specify settings like path, lifetime and the enabled/disabled state. This is stored as a PHP configuration where the autoloader can get to it and the class as a dependency on the middleware itself.

tagged: cache middleware output zendexpressive tutorial response content

Link: https://framework.zend.com/blog/2017-04-19-caching-middleware.html

Zend Framework Blog:
Scrape Screens with zend-dom
Feb 28, 2017 @ 16:46:27

The Zend Framework blog has posted another tutorial focusing on the use of one of the components that makes up the framework. In this latest tutorial Matthew Weier O'Phinney focuses on the zend-dom component and how to use it for scraping content from remote sources.

Even in this day-and-age of readily available APIs and RSS/Atom feeds, many sites offer none of them. How do you get at the data in those cases? Through the ancient internet art of screen scraping.

The problem then becomes: how do you get at the data you need in a pile of HTML soup? You could use regular expressions or any of the various string functions in PHP. All of these are easily subject to error, though, and often require some convoluted code to get at the data of interest.

[...] zend-dom provides CSS selector capabilities for PHP, via the ZendDomQuery class. [...] While it does not implement the full spectrum of CSS selectors, it does provide enough to generally allow you to get at the information you need within a page.

He gives an example of it in use, showing how to grab a navigation list from the Zend Framework documentation site (a list of items in a <ul> tag). He also suggests some other uses of the tool including use in testing of your application, checking content in the page without having to hard-code specific strings.

tagged: zendframework zenddom scrape content html dom xml tutorial

Link: https://framework.zend.com/blog/2017-02-28-zend-dom.html

The Ultimate Guide to Building a WordPress Plugin
Dec 23, 2016 @ 12:07:41

For those newer to the world of WordPress, you might only be casually familiar with WordPress plugins and their use. You might have only installed them and used them before but have you wondered what it would take to make your own? In this new tutorial from Toptal.com Ratko Solaja gives you a "ultimate guide" to getting started down the road of custom WordPress plugin development.

Plugins are a vital part of WordPress websites that need specific functionalities. While the official WordPress repository has more than 45,000 plugins from you to choose from, many of these plugins miss the mark.

Just because a plugin is in the repository doesn’t mean it won’t hinder its performance or compromise its security. So what can you do? Well, you can build your own.

He starts with the planning stages of his example plugin (a real-world project helps when learning new things) - one that allows users to save content for later reading. He outlines the goals of the settings screen, how saving will work, messages to the user and what the "saved" screen will do. He recommends starting with a boilerplate plugin and working from there. He then goes through each step of the development process:

  • Handle activation and deactivation
  • Create a plugin settings page
  • Create the plugin functionality
  • Make the plugin modular
  • Generate translation files

The end result is a complete plugin with both the required frontend and backend functionality to make the "save content" feature work. All code is provided and plenty of links to more information and other resources are sprinkled throughout the article.

tagged: toptal wordpress plugin guide tutorial content example

Link: https://www.toptal.com/wordpress/ultimate-guide-building-wordpress-plugin

Matthias Noback:
Creating virtual pages with Sculpin
Dec 14, 2016 @ 09:28:58

Matthias Noback has continued his series looking at using Sculpin to create static pages for "in-project documentation" with this second post focusing more on the creation of "virtual pages". These pages allow the insertion of dynamic content into pages pulled from other sources (in this case, from the source of the project).

Previously we looked at how to use the static site generator Sculpin to generate in-project documentation. When Sculpin builds the HTML files and assets it looks at what files are in the source/ directory and processes them based on certain rules (e.g. "parse Markdown files", "format HTML files with Twig", etc.). The purpose for my "living documentation" project is to also dynamically generate documentation based on PHP and configuration files from the main project.

[...] The Sculpin documentation mentions several ways to generate dynamic content for a static site, as opposed to the standard way of adding static Markdown or HTML files. [...] So, I dug into the source code of Sculpin to find out what would be a reasonable extension point. It turned out that the best way to approach this was to create a custom data source (which is currently not documented).

He then walks you through the creation of a custom data source with "dataSourceId" and "refresh" methods the tool uses to pull in the dynamic data. He shows how to set up the script to be called at any time and have the same effect (Sculpin does this when the "server" is running) and defining the service in your Sculpin configuration.

tagged: sculpin static site generator virtual page dynamic content tutorial

Link: http://php-and-symfony.matthiasnoback.nl/2016/12/creating-virtual-pages-with-sculpin/

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/

Rob Allen:
Filtering the PSR-7 body in middleware
May 16, 2016 @ 09:25:30

Rob Allen has a post to his site showing how you can filter the body content of your response in a PSR-7 compatible system via some simple middleware.

Sometimes, there's a requirement to alter the data in the Response's body after it has been created by your controller action. For example, we may want to ensure that our brand name is consistently capitalised. One way to do this is to create middleware that [uses str_ireplace to replace a string]. That's great, but what happens if the new string is shorter than the old one? For instance, suppose I want to replace all uses of "nineteen feet" with "19FT".

In his example, replacing that part of the content would result in an odd string because of how they'd be replaced in the stream. He shows how to use the in-memory handling of PHP's fopen to create a new content instance to push back into the response and back out to the waiting application.

tagged: psr7 middleware filter content strireplace stream fopen tutorial

Link: https://akrabat.com/filtering-the-psr-7-body-in-middleware/

SitePoint PHP Blog:
First Look at Pagekit CMS – Clean, Extensible, Fast, But…
Apr 26, 2016 @ 10:55:55

On the SitePoint PHP blog there's a post from Bruno Skvorc introducing the Pagekit CMS, a content management system that's "clean, extensible and fast" (but it does come with some caveats).

Pagekit hit version 1 recently, and as I’d been looking at personal blogging engines, I thought it’d only be fair to check it out. Granted, blogging is merely a subset of the functionality Pagekit can offer, but a good basic test-drive subset nonetheless.

He walks you through the installation and configuration of a new Pagekit-based site using their own installer script (after downloading it from their site). He then goes through some of the basic features of the CMS including native Markdown support, how the editor looks and how the results render. He includes a guide on setting up a blog too using a "blog" plugin and an extension to add in better syntax highlighting. He also looks at other features of the CMS including custom layouts and "pretty" URL support. He points out some security changes you'll want to make out of the box to protect sensitive files and briefly touches on deploying the site to production and links to their own guide for additional help.

tagged: pagekit cms content management introduction tutorial project

Link: http://www.sitepoint.com/first-look-at-pagekit-cms-clean-extensible-fast-but/

Symfony Finland:
What eZ Platform adds to Symfony
Feb 12, 2016 @ 10:44:01

On the Symfony Finland blog Jani Tarvainen has written up a new post sharing some of the things that eZ Platform adds to Symfony and what kind of functionality it brings with it on top of the usual Symfony featureset.

eZ Platform is a Content Management System built with the Symfony Full Stack framework. While this may be clear to developers who have worked with, it maybe somewhat vague for the unitiated, especially when compared with Concrete5 or Drupal, which have adopted Symfony components into their core. [...] The relationship between Symfony and eZ Platform is very similar to Expression Engine using Code Igniter or EPiServer using ASP.NET MVC.

He then goes on to talk about the things Symfony includes by default including the request/response structure, internationalization handling and Twig integration. From there he lists out the things that the eZ Platform adds on top of the standard Symfony including:

  • a content repository
  • dynamic routing
  • a user interface
  • user and permission management

He ends the post with a look at some of the other bundles and features eZ Platform also provides around HTTP caching, image manipulation and more.

tagged: ezplatform symfony project framework content management system

Link: https://www.symfony.fi/entry/what-ez-platform-adds-to-symfony

Statamic v2 Beta: First Impressions of a new Laravel-based flat-file CMS
Feb 01, 2016 @ 13:37:14

On the Tighten.co blog they've posted their own review of Statamic, the flat-file based content management system with a Laravel backend. Statamic is a project that hopes to provide easy content management, responsive layouts and plenty of features to make an easy-to-use and robust CMS.

Among the developers I know who used to use ExpressionEngine but have since left, most work in Craft and/or Laravel. I kept hearing folks mention Statamic, but all I knew about it was that it was flat file, which wasn't particularly compelling to me.

Fast forward two years, and they've re-written the entire application to run on Laravel (now released as v2 beta). [...] Their documentation is hilarious, the community is welcoming and helpful, and the code—granted, I'm only a few weeks in—seems super easy to work with. So, what's the deal? Why have we set up Tighten's blog on Statamic?

The post then goes on to talk about the "quest" for a good Laravel-based content management system. They also talk about some of the essentials they see a CMS needing to be effective: good user interaction (UI/UX), how much and how difficult it is to customize and how it is configured. For each point they talk about how Statamic does things and their own verdict on the software and how good it does at filling these requirements.

tagged: statamic beta laravel content management system cms flatfile

Link: http://blog.tighten.co/statamic-v2-beta-first-impressions-of-a-new-laravel-based-flat-file-cms

Chris Fidao:
Laravel and Content Negotiation
Jan 18, 2016 @ 11:57:35

Chris Fidao has a quick post to his site looking at content negotiation in Laravel apps using some of the framework's own built-in functionality.

Here's a little bit about content negotiation. An HTTP client, such as your browser, or perhaps jQuery's ajax method, can set an Accept header as part of an HTTP request. [...] This header is meant to tell the server what content types it is willing to accept.

He starts with a bit of illustration as to what the Accept header is and what kinds of values it supports (and how it looks as a HTTP header). He then shows how to check the Accept header value inside the current request. He also shows the "shortcut" Laravel provides to test if the Accept header specifically references JSON with the wantsJson method. He also mentions the accepts and prefers methods for checks that need to be a bit more in-depth.

tagged: content type accepts negotiation wantsjson prefers

Link: http://fideloper.com/laravel-content-negotiation