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

Laravel News:
Creating Your Own Configuration in Laravel
Sep 19, 2017 @ 12:14:16

On the Laravel News site there's a new tutorial posted showing you how to create your own custom configuration for those that are newer to the framework.

If you are new to Laravel, you might not yet know how you can create your configuration files in your projects and a few other helpful things that will help you master configuration. As always, the official documentation provides great information, and there are a few things we’ll cover here that I think will help people new to the Laravel framework.

They walk you through a complete guide to the Laravel configuration handling including:

  • what pieces are involved and where they belong
  • accessing the configuration service
  • working with configuration values
  • creating a custom configuration
  • caching the configuration for better performance

Each item on the list has a brief description helping to illustrate the point and commands/code examples where appropriate.

tagged: laravel configuration custom tutorial introduction

Link: https://laravel-news.com/creating-configuration-in-laravel

Mark Baker:
Closures, Anonymous Classes and an alternative approach to Test Mocking (Part 3)
Sep 19, 2017 @ 11:58:39

Mark Baker has posted the third part of his series looking at an alternative way to handle mocking in the tests for your PHP application. In this latest part of the series he shows how to modify one of PHPUnit's own mocking examples to use an anonymous class.

I have heard people say that you shouldn’t test abstract classes or traits, only the concrete classes that implement or use them. I don’t follow that approach: unit testing is all about testing in isolation from anything that might affect those tests. Testing a concrete class that implements an abstract one, or uses a trait, means that the abstract class or trait is no longer fully isolated, but is being tested within the scope of the whole of that concrete class. We should still always test concrete classes as well; but we should also test the abstract classes and traits as individual units.

So just how do we test something that cannot be instantiated on its own?

He shares one tactic that some developers use - a class designed only for testing - but suggests that this "pollutes" the codebase. Instead he shows how to replace mocking for traits and abstract classes with an anonymous class that's more "disposable". He also shows how to modify this approach to handle calling protected methods in the class the anonymous class extends.

tagged: closure anonymous class alternative mock tutorial part3 series

Link: https://markbakeruk.net/2017/09/18/closures-anonymous-classes-and-an-alternative-approach-to-test-mocking-part-3/

Peter Lafferty:
HTTP Request Validation With Silex
Sep 18, 2017 @ 12:15:48

On his Medium blog Peter Lafferty has written up a post showing you a method for HTTP request validation in Silex, the microframework from the creators of Symfony.

This article covers three validation scenarios: routes, query strings [and] POST with a JSON body.

He starts with a simple Silex application that creates a "RESTful" API with endpoints providing emojis back when queried (three endpoints). He then uses this to show how to validate:

  • routes for their expected values in the URL
  • using a ValidatorService provider to build a set of assertions (GET request)
  • using the same service to create assertions for the JSON content of a POST request

All code required is included in the post including the correct handling of the emoji output via a UTF-8 JSON response handler.

tagged: http validation silex tutorial service assert url get post

Link: https://medium.com/@peter.lafferty/http-request-validation-with-silex-9ebd7fb37f37

Dragos Holban:
How to Setup Docker for Your Symfony Project
Sep 18, 2017 @ 11:48:31

In a tutorial posted to his Medium.com site Dragos Holban continues his series walking you through Symfony for PHP applications. In this latest part of the series he shows you how to use Docker to set up an environment for a Symfony-based application.

As you probably know, I am a big Symfony fan :) In the last few years I used Vagrant to set up my Symfony development environment more or less as described here. But we now have Docker and it’s time to move forward with a new setup. Docker containers are generally more lightweight than Vagrant virtual machines, so starting and stopping them is extremely fast. They also take a lot less disk space.

He starts by sharing an example Dockerfile and docker-compose.yml to help set up and configure the environment to be ready for use. The configuration sets up Apache, PHP 7, MySQL, git, Composer and several PHP extensions. It then installs a fresh instance of Symfony and runs a bash file to finish the setup. He shares the contents of this file and the VirtualHost configuration for the web server. Finally he shows how to run the setup via the Docker command line tool and what you can expect to see when things are running smoothly.

tagged: docker symfony setup configure dockerfile compose tutorial series

Link: https://medium.com/@dragosholban/how-to-setup-docker-for-your-symfony-project-ff9b1bf44e68

Laravel News:
Testing With PhpSpec
Sep 18, 2017 @ 10:22:26

On the Laravel News site there's a new tutorial posted introducing you to testing with PhpSpec, a specification-driven testing tool that helps you model the specification your application needs to match.

PhpSpec is a testing tool based on the concept of emergent design using specification. You may have heard of Behavior Driven Development (BDD), and PhpSpec is a tool to use at the spec level or SpecBDD. We also mentioned PhpSpec in our recent roundup of Laravel Testing Resources, which includes ways you can incorporate PhpSpec into your Laravel workflow if you are interested in trying out SpecBDD.

If you’ve never tried PhpSpec, one of the things that I love about PhpSpec is that the process generates code for you and guides you through the SpecBDD process through the command line. Let me show you what I mean with a quick tutorial.

The tutorial then walks you through the installation of PhpSpec and how to run it from the command line before making any tests. They then walk through the configuration of the tool and share a sample workflow with seven steps including describing a specification, writing out expected behavior and verifying the behavior. Code examples (and command line calls) are included in each step of the way, defining an example "calendar" specification with the common functionality (add, subtract, divide, etc). The post finishes with some links to where you can find more information about PhpSpec and spec driven development.

tagged: phpspec specification testing calculator tutorial development

Link: https://laravel-news.com/testing-with-phpspec

SitePoint PHP Blog:
Game Development with React and PHP: How Compatible Are They?
Sep 15, 2017 @ 12:43:52

The SitePoint PHP blog has posted a new tutorial from Christopher Pitt that tries to answer the question wondering if React and PHP are compatible for game development.

“I’d like to make a multiplayer, economy-based game. Something like Stardew Valley, but with none of the befriending aspects and a player-based economy.”

I started thinking about this the moment I decided to try and build a game using PHP and React. The trouble is, I knew nothing about the dynamics of multiplayer games, or how to think about and implement player-based economies. I wasn’t even sure I knew enough about React to justify using it.

I once watched a talk by dead_lugosi, where she described building a medieval game in PHP. Margaret inspired me, and that talk was one of the things that led to me writing <a href="https://www.amazon.com/dp/1484224922>a book about JS game development. I became determined to write about my experience. Perhaps others could learn from my mistakes in this case, too.

With the foundation laid, he starts in on the setup of the backend for the game: a PHP server running a server compatible with multiple websocket (React) requests. He chose Aerys for the HTTP and websocket functionality and includes the code to create the server and the packages he required. He shares some of the code to create the server and a "Hello world" endpoint the frontend will use. He then moves over to the frontend side of things, showing the packages he installed via NPM and the Laravel Mix configuration to use Webpack to bundle up the files required. He then walks through the integration of the front and back end code including the connection of the websockets.

For those that want to see the end result all together he has posted it to GitHub in a complete form for this part of the series.

tagged: react tutorial reactphp game development part1 series websocket

Link: https://www.sitepoint.com/game-development-with-reactjs-and-php-how-compatible-are-they/

Laravel News:
Laravel 5.5 Now Includes TrustedProxy
Sep 15, 2017 @ 11:07:03

On the Laravel News site there's a new post about a feature introduced in version 5.5 of the popular PHP framework: a proxy that's included by default as a part of the TrustedProxy feature.

Laravel v5.5 was released just a week ago at Laracon EU. You may have noticed that the v5.5 composer.json file requires the similar functionality Symfony has).

tagged: laravel proxy package feature cloudflare tutorial framework

Link: https://laravel-news.com/trusted-proxy

Zend Framework Blog:
Emitting Responses with Diactoros
Sep 15, 2017 @ 09:14:05

On the Zend Framework blog they've posted a tutorial from project lead Matthew Weier O'Phinney showing how to use the Diactoros package to create response emitters. These emitters are just methods of output related to HTTP handling (like headers, response codes and normal text output).

When writing middleware-based applications, at some point you will need to emit your response.

PSR-7 defines the various interfaces related to HTTP messages, but does not define how they will be used. Diactoros defines several utility classes for these purposes, including a ServerRequestFactory for generating a ServerRequest instance from the PHP SAPI in use, and a set of emitters, for emitting responses back to the client. In this post, we'll detail the purpose of emitters, the emitters shipped with Diactoros, and some strategies for emitting content to your users.

He starts by describing what "emitters" are in the context of PSR-7 applications and shows the code definition of the EmitterInterface, the base of all emitter classes. After covering some of the emitters that Diactoros includes he shows how to create one to emit file information and how you can mix and match them in a emitter "stack" to perform more that one operation on the output.

tagged: diactoros tutorial emitter response output example interface

Link: https://framework.zend.com/blog/2017-09-14-diactoros-emitters.html

SitePoint PHP Blog:
Symfony Console Beyond the Basics – Helpers and Other Tools
Sep 14, 2017 @ 11:12:42

The SitePoint PHP Blog has a new tutorial posted from author Claudio Ribeiro that takes you beyond the basics with the Symfony Console component, showing you how to use some of the helpers and other tools included in its functionality.

It’s undeniable how useful console commands can be when developing software. Not too long ago we re-introduced the Symfony Console component.

This component allows us to create structured and testable CLI commands. We created some simple commands and tested them; but when our commands become bigger and more complex, we need a different set of tools.

This is what we are going to look at today: advanced Symfony console tools.

He then walks you through the installation of the component via Composer and the initial creation of a "FizzBuzz" console command class (and other related classes). He then shows how to use several of the helpers:

  • the "question" helper to ask for user input
  • the "table" helper for outputting structured data
  • the "progress bar" helper to visually output progress of a task

There's also examples included showing how to call a command from inside a command and changing up color and style of the output.

tagged: tutorial symfony console advanced helpers command

Link: https://www.sitepoint.com/symfony-console-beyond-the-basics-helpers-and-other-tools/

Sameer Borate:
Switching WordPress to HTTPS
Sep 14, 2017 @ 09:19:38

On his site Sameer Borate gives you a quick tutorial on how to migrate your WordPress site to HTTPS to help secure the communications between it and your users.

Finally, after much deliberation, I decided to move my blog to https. For the past few months I had encountered articles pointing that google gives a higher ranking for sites served over https. I had decided against https as I was afraid it may slow down by blog. However, the [a message about Chrome's HTTP/HTTPS support] from google forced me to take the issue seriously and finally move to https.

[...] Luckily during the same time Godaddy was offering SSL certificates at a discounted price. So taking this as a sign I bought the SSL certificate and moved my blog to HTTPS. Surprisingly installing SSL on Godaddy was a breeze. There was no configuration involved and the SSL certificate was applied within a few minutes.

He assumes that you know how to get the certificate installed, so that part isn't covered. He does cover the shift of the WordPress site over including changes that'll need to be made to web server and WordPress application configurations. He ends the post briefly talking about any speed difference he saw with the site (hint: not much).

tagged: wordpress https secure certificate settings tutorial configuration

Link: https://www.codediesel.com/wordpress/switching-wordpress-https/