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

Zend Developer Zone:
Developing a Z-Ray Plugin 101
Nov 04, 2015 @ 10:44:13

The Zend Developer Zone has posted a tutorial showing you the basics of creating a plugin for Z-Ray, the tool from Zend that provides details and metrics around the execution of your application.

One of the great things about Z-Ray is the ability to extend it to display any info you want about your app. This is done by creating plugins. In this tutorial I’m going to describe how to create a new Z-Ray plugin. I’ll be supplying code snippets to insert in the various plugin files but of course feel free to replace it with your own code when possible.

They start by describing how Z-Ray shows its data and offering two options - the default panel or a custom panel. They choose the custom panel and show you how to:

  • create the template for the panel
  • make the module directory and zray.php
  • and Modules.php file to define the plugin

There's also a section on how the Z-Ray plugin traces through the execution of your application, illustrating with a DummyClass. They include the code to set up the Trace and define which methods and actions to watch. Finally they relay this information back out to the custom panel view via Javascript collection and the code to show the results.

tagged: zray plugin custom performance dummyclass execution tracer tutorial

Link: http://devzone.zend.com/6826/developing-a-z-ray-plugin-101/

SitePoint PHP Blog:
Powerful Custom Entities with the Diffbot PHP Client
Nov 02, 2015 @ 10:55:18

On the SitePoint PHP blog editor Bruno Skvorc continues his look at the Diffbot service and shows how to use their API to create entities representing objects from the API data.

A while back, we looked at Diffbot, the machine learning AI for processing web pages, as a means to extract SitePoint author portfolios. [...] Since then, the design of the pages we processed has changed, and thus the API no longer reliably works. In this tutorial, apart from rebuilding the API so that it works again, we’ll use the official Diffbot client to build custom entities that correspond to the data we seek (author portfolios).

He starts by setting up the environment for development (a Homestead Improved instance) and pulling in a few libraries to bootstrap the script. He shows the setup and configuration of the Diffbot client, creating a new API application via their UI and using the browser tools to help the service locate the information it needs. He then shows how to extend the client and define an "entity factory" to generate the objects requested. In this case he creates an AuthorFolio class to contain the author's number of posts.

tagged: diffbot client custom entities tutorial author portfolio api

Link: http://www.sitepoint.com/powerful-custom-entities-with-the-diffbot-php-client/

Matt Stauffer:
Creating custom @requires annotations for PHPUnit
Oct 28, 2015 @ 10:06:46

In this post to his site Matt Stauffer walks you through how he created a custom @requires annotation to use in his PHPUnit testing. He needed a way to tell a test to only run if it wasn't being executed on the Travis CI service.

I was working on a project this weekend that required skipping certain tests in a particular environment (Travis CI). [...] I remembered that there was a @requires annotation in PHPUnit that works natively to allow you to skip a test under a certain version of PHP or with certain extensions disabled, so I set out to write my own custom @requires block.

He links to an article that helped him get most of the functionality in place but decided to restructure it a bit to make the override of the checkRequirements method a bit clearer. He ends up using the Laravel Collection functionality instead of a basic foreach reducing it down to a closure that looks for an environment variable called TRAVIS and automatically mark the test as skipped.

tagged: requires annotation custom phpunit travisci skip environment variable closure

Link: https://mattstauffer.co/blog/creating-custom-requires-annotations-for-phpunit

Ross Tuck:
Formatting Exception Messages
Oct 27, 2015 @ 12:09:39

In a post to his site Ross Tuck shares some of his experience and some helpful hints around formatting exception messages and how doing so effectively can make life for fellow developers much easier.

Over the last couple years, I’ve started putting my Exception messages inside static methods on custom exception classes. This is hardly a new trick, Doctrine’s been doing it for the better part of a decade. Still, many folks are surprised by it, so this article explains the how and why.

He shares his tips as a part of a "refactoring" in a simple example, a CSV import where there are failures during the import process on certain lines. He starts with the basic Exception and works through the logic to customize it and make it more useful. He shows the inclusion of additional details in the message, abstracting out the formatting to custom methods based on the error type and using static methods for the more complex message formatting. He also suggests the creation of methods to handle specific error cases with more details than a simple single-line error in a normal exception being thrown.

When you co-locate the messages inside the exception, however, you gain an overview of the error cases. If these cases multiply too fast or diverge significantly, it’s a strong smell to split the exception class and create a better API. [...] Sometimes we underestimate the little things that shape our code. [...] Creating good environments at a high level starts with encouraging them at the lowest levels. Pay attention to what your habits encourage you to do.
tagged: format exception message custom method details static

Link: http://rosstuck.com/formatting-exception-messages/

SitePoint PHP Blog:
Managing Cronjobs with Laravel
Oct 27, 2015 @ 11:18:06

The SitePoint PHP blog has a tutorial posted showing you how to use the Laravel "scheduler" to handle cron jobs and the execution of custom commands.

There are times when your application needs to run administrative tasks periodically on the server. Whether you want to send out emails to your users or clean up the database tables at the end of the day, you will need a task scheduling mechanism to take care of the tasks, when it’s time. Cron is a task scheduler in unix-like systems, which runs shell commands at certain intervals.

They start with some of the basics of cron and how is configuration is defined. Since the Laravel scheduler makes use of the same format, they want to be sure everyone is on the same page. Next they show you how to create a custom command in Laravel, a simple script designed to be run on the command line. The tutorial shows how to execute the simple command and how to add it to your application's "commands" list in the configuration. Finally they get to scheduling the commands, defining a schedule method and using the Laravel helper methods to set the execution time to something like "daily", "hourly" or, if you need something a bit more custom, a full cron-formatted string. The post ends with the addition of the Laravel scheduler execution to the system cron, set to run every minute.

tagged: manage cronjob laravel scheduler custom tutorial command

Link: http://www.sitepoint.com/managing-cronjobs-with-laravel/

SitePoint PHP Blog:
Custom Display Suite Fields in Drupal 8
Oct 15, 2015 @ 12:50:20

The SitePoint PHP blog has posted a new tutorial that's another Drupal-focused article explaining custom Display Suite fields in the latest Drupal 8. Display Suite is a popular Drupal module that's been ported from Drupal 7 to version 8. It provides lots of features to help you easily create these custom fields and integrate them into layouts with other core fields.

Without question, Display Suite is one of the most popular modules in Drupal’s contributed modules history. It allows the creation of layouts, fields and exposes all sorts of other powerful tools we use to build the presentation layer of our Drupal sites.

[...] In this article, we are going to look at how we can create our own Display Suite field in Drupal 8 using the new OOP architecture and plugin system. To demonstrate this, we are going to create a DS field available only on the Article nodes that can be used to display a list of taxonomy terms from a certain vocabulary. And we’re going to make it so that the latter can be configured from the UI, namely admins will be able to specify which vocabulary’s terms should be listed.

He starts with a brief introduction to Drupal 8 plugins and their structure. From there he gets into the creation of the VocabularyTerms class they'll use to define their custom DSField plugin. He goes on to create the default configuration, formatters and how to show a list of the current settings (a summary). He then creates the form where the plugin can be configured through the UI and how to render the resulting field.

tagged: drupal8 tutorial custom field displaysuite

Link: http://www.sitepoint.com/custom-display-suite-fields-in-drupal-8/

Hart KT:
Custom Events in Symfony2 Bundle
Oct 12, 2015 @ 11:10:58

Hari KT has a quick tutorial posted to his site showing you how to use custom events in Symfony bundles from start to finish.

In this tutorial we will create a custom event for symfony2 bundle. Assuming you have downloaded the symfony-standard distribution to play.

He starts by creating a simple bundle (HktEventBundle) and building out the matching event class, extending the base Symfony EventDispatcherEvent. He then shows how to dispatch a simple event from a controller, triggering a html.event.page_viewed event when the request is made to this default controller. He matches this with a listener that subscribes to the event (including a handler method) and the changes you'll need to make to your configuration to wire them all together.

tagged: symfony2 bundle custom event simple tutorial introduction

Link: http://harikt.com/blog/2015/10/11/custom-events-in-symfony2-bundle/

Matt Stauffer:
Custom conditionals with Laravel's Blade Directives
Oct 12, 2015 @ 09:57:53

Matt Stauffer has a post to his site showing you how to use custom conditionals with Blade, the templating engine for the popular Laravel framework.

One of the greatest aspects of Laravel Blade is that it's incredibly easy to handle view partials and control logic. I find myself frequently extracting the contents of a loop out to a Blade partial and then passing just a bit of data into the partial.

But sometimes the repetitive code isn't the view itself, but the conditional logic I'm running.

He gives a practical example of an issue he had in some recent work he's done: a simple site for his physical trainer that needed to test for public vs non-public resources. The same evaluation code was being pasted all over the site so he looked for a better way. He introduces the idea of a custom directive in Blade and how they're converted to PHP out in the view. He shows how to make an @public directive to replace his cut-and-paste logic from before and make the template much cleaner. He includes the code for this simple directive and ends the post with some other possible options use could use the directives for.

tagged: conditional laravel directive tutorial blade custom evaluation

Link: https://mattstauffer.co/blog/custom-conditionals-with-laravels-blade-directives

SitePoint PHP Blog:
Building Custom cTools Plugins in Drupal 7
Oct 05, 2015 @ 12:46:27

The SitePoint PHP blog has a tutorial posted showing you how to build custom cTools in Drupal that will integrate with the widely used cTools tool set.

cTools is one of those critical Drupal 7 modules many others depend on. It provides a lot of APIs and functionality that makes life easier when developing modules. Views and Panels are just two examples of such powerhouses that depend on it. [...] In this article, we are going to take a look at cTools plugins, especially how we can create our very own. After a brief introduction, we will immediately go hands on with a custom module that will use the cTools plugins to make defining Drupal blocks nicer (more in tune to how we define them in Drupal 8).

He starts by briefly introducing the cTools functionality and different types of plugins included in common use. He then moves on to the "block_plugin" handling and the custom functionality you'll be walked through - a plugin encapsulating a block and its related logic. He starts by defining the plugin type with a function the cTools can locate and the code it should contain. From there the code needed to turn a block plugin into a "Drupal block" is added including a prefix to "namespace" them out. Finally the view handling is implemented and a helper function is created to load the plugin. He then shows how to define one of the block plugins complete with a title, markup and type.

tagged: custom ctool plugins drupal block tutorial

Link: http://www.sitepoint.com/building-custom-ctools-plugins-in-drupal-7/

SitePoint PHP Blog:
Drupal 8 Custom Plugin Types
Sep 14, 2015 @ 11:08:06

The SitePoint PHP blog has posted an overview from Daniel Sipos covering custom plugin types in Drupal 8 and how you can combine them (custom forms) with node entities.

In this article series of two parts, we will use this system to build a feature that allows the use of custom forms together with node entities. After we’re done, we’ll be able to do the following: configure node bundles to use one of multiple form types to be displayed together with the node display and easily define new form types by extending from a sensible base class. [...] We will get started by creating our custom plugin type. T

He starts with the plugin manager, showing you how to create a custom ReusableFormsManager in the module to set up the manager and add it to the system. He then sets up the plugin interface the manager is expecting to find. This piece defines methods to get the name of the plugin and to build the form. He then creates a simple ReusableForm annotation class and builds out the plugin base. This base class includes a form builder object used to build and output the custom form. Finally he gets into building the form and its matching interface. It's a simple "Contact Us" kind of form that outputs three fields (first name, last name, email) and a "Submit" button.

tagged: drupal8 custom plugin type tutorial form contactus

Link: http://www.sitepoint.com/drupal-8-custom-plugin-types/