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

Matt Stauffer:
Environment specific variables in Laravel's testing environment
Nov 06, 2015 @ 10:43:09

Matt Stauffer has a quick post to his site showing how you can set up and use environment specific variables in Laravel, specifically for your testing environment.

In Laravel, it's easy to set environment variables that are specific to your testing environment. Just edit your phpunit.xml file and set them as entries in the block [...] but what if you find yourself needing to exclude these values from version control?

He talks about the project he's working on and its integration with Twilio. He need to write some tests for a class that connected to the Twilio service but wanted an easy way to swap out the production credentials with the Twilio test ones. Instead of checking in the test credentials, he dropped them into his .env settings file, one specific to the test environment.

tagged: environment variable laravel testing twilio credentials.

Link: https://mattstauffer.co/blog/environment-specific-variables-in-laravels-testing-environment

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

SitePoint PHP Blog:
Docker and Dockerfiles Made Easy!
Oct 16, 2015 @ 15:56:58

The SitePoint PHP blog has a post that aims to help you with Docker and introduces some of the basic concepts and steps to understand the Dockerfile configuration.

It’s become second nature for developers to use Virtual Machines to configure and manage their working environments. Most professionals who use VMs use Vagrant for dealing with their development environments. In this article, we’ll be moving from Vagrant to Docker, and use a small Laravel application to test that everything is working as expected.

They walk you through the installation of necessary tools, creation of a simple Docker machine and the concepts behind images and containers. They show you how to create a simple Ubuntu-based machine and configuring it to serve up the default Ubuntu+Apache page. They also cover container volumes, a method for keeping a local folder in sync with one in your instance. They help you set this up with a Laravel installation. Next up they help you configure the database container running MySQL and connect it to the Laravel instance. The tutorial also shows how to set up links between the containers, gets into some of the details of the Dockerfile structure and using "docker compose" to easily create environments based off a YAML configuration.

tagged: docker dockerfiles tutorial environment container image laravel

Link: http://www.sitepoint.com/docker-and-dockerfiles-made-easy/

Matt Stauffer:
Environment-Specific Configuration for CraftCMS Using PHPDotEnv
Sep 25, 2015 @ 10:13:21

In this post to his site Matt Stauffer shows a more real-word example of how the phpdotenv library can make configuration of your application simpler. He shows how it can be applied to a Craft CMS installation to manage domain-specific configuration details.

Craft is a fantastic CMS, but every CMS shows some pain points when you have a large team working on the same site at the same time. One of these points for me is Craft's native multi-environment configuration options, which allow you to define configuration options based on the domain name.

[...] This is great, but it's limited: You're hard-coding the configuration details into your code, which sometimes means putting sensitive information into your version control. Every developer's local installs either all have to have different domains, or if they use the same domain they need to all have the same configuration settings. And something just feels dirty about the codebase having such knowledge of every place it's going to be deployed.

He introduces the phpdotenv library and how you define its simple .env file with a basic INI structure. He then shows how to add the phpdotenv library to your installation:

  • adding it to the list of Composer installed libraries
  • update your front controller to load the configuration
  • define the .env file with your settings
  • ignore it via .gitignore

With these steps in place you can then update the Craft configuration with calls to getenv in all the right places to pull items from the phpdotenv configuration.

tagged: phpdotenv env configuration craftcms example environment tutorial

Link: https://mattstauffer.co/blog/environment-specific-configuration-for-craftcms-using-phpdotenv

Matthew Weier O'Phinney:
Fixing Version Issues When Running Composer from a Branch
Sep 11, 2015 @ 10:55:04

Matthew Weier O'Phinney has posted an article to his site showing you how to fix version issues in branches when using Composer packages and libraries in your applications.

For the Zend Framework component repositories, we occasionally need to backport changes to the 2.4 LTS releases. This requires checking out a branch based off the last LTS tag, applying patches (often with edits to translate PHP 5.5 syntax to PHP 5.3), and running tests against PHP 5.3 and 5.4.

Of course, to run the tests, you need the correct set of dependencies installed. If you have any component dependencies, that means running a composer update to ensure that you get the 2.4 versions of those components. And that's where my story begins.

He talks about some of the issues he's come across when testing components and Composer, not understanding that the environment has changed, does not load the correct versions of the necessary libraries. He first tried to fix the dependencies himself, adjusting the version numbers required but with no luck. Finally he stumbled across something on the Composer site that helped: the ability to define a "root version" environment variable that made it adhere to the versions he needed.

tagged: composer dependency branch issue incompatible environment variable

Link: https://mwop.net/blog/2015-09-09-composer-root.html

Is your database password stored safely?
Sep 08, 2015 @ 11:48:18

The Fortrabbit blog has a post that want to help you store your database password securely and away from prying attacker eyes. While they use the example of a a database password, credentials for just about any other service could be protected the same way.

How do you protect your access data? Your sensitive secrets, basically anything your PHP application uses to authenticate or authorize with other services such as databases, caches, cloud storages, image resize services, transactional mail providers. All of them. Where do you put this — easily accessible while in development and secure for production?

They start by pointing out a few places where they should not be stored: in your code, in a version control system or in an environment variable (plain text). Instead, they suggest using a combination of a secret key that's configured in the application and encrypted versions of the values in environment variables. Some code is included showing how to set this up in a Laravel-based application, but the principle can be applied independent of the framework too with some other simple tools. They end the post with some links to other articles including a "considered harmful" piece reinforcing their methods.

tagged: credential protection password database tutorial encryption environment variable

Link: http://blog.fortrabbit.com/how-to-keep-a-secret

Coding.bmail.net Blog:
Advanced logging system in PHP for careful developers
Aug 05, 2015 @ 12:19:51

On the Coding.bmail.net blog they've posted a guide to what they've called an advanced logging system in PHP for careful developers - essentially a logging system that's as "error proof" as possible and that works with as little user exposure as possible.

Being aware of all the activity and problems under the hood is essential when running big websites with lots of users, many features and, as it is usual in such cases, weak spots that must not be left untracked.

In order to be the first in knowing when errors or other events of interest happen we need a well designed logs manager. My code will provide such a feature, for PHP based websites.

They briefly outline how the complete setup will work, failing back to email if the database connection isn' available and logging based on environment. It also includes error levels and, on development only, a method for showing the errors being logged. While a good bit of this functionality could be handled by something like Monolog they do include some additional features like the email fallback, output of the errors in development mode and custom error/exception handlers.

tagged: logging advanced system custom database email environment tutorial

Link: http://coding.bmain.net/tutorials/php/advanced_logging_system_in_php_for_careful_developers

Joeri Verdeyen:
How I develop in PHP with CoreOS and Docker
Jul 29, 2015 @ 11:41:14

Joeri Verdeyen has posted a tutorial showing you how to use a combination of CoreOS and Docker as a PHP development environment. This is an alternative to the more frequently used Vagrant VM provisioning popular among developers.

I’ve been using the Vagrant provisioned-with-Ansible-setup for a while now. But for the last month(s) I’ve been playing around with things like: Docker, boot2docker, CoreOS, etcd, .. I managed to setup a fast and easy way to develop my PHP applications. Symfony2 is my preferred weapon of choice, so I’ll explain how I’m developing a Symfony2 app.

He starts with the software you'll need installed to get his example up and running, all installable via "brew". He shows how to configure the CoreOS via Vagrant and bring the box up. He then sets up the Docker client to point to the newly created VM as its server. He then creates a docker-compose.yml file to set up the necessary services including nginx, MySQL and (of course) PHP. He then shows the command to run the container, execute the configuration and ensure that all containers are configured correctly. Finally he runs the Composer installation command (Symfony2, remember) and clear the cache.

tagged: coreos docker vagrant development environment tutorial configuration symfony2

Link: https://www.jverdeyen.be/docker/how-php-symfony-coreos-docker/

Gary Hockin:
Debugging PHP Command Line (with PHPStorm and XDebug)
May 07, 2015 @ 11:47:09

Gary Hockin has a recent post showing how you can debug PHP HTTP requests from the command line using PHPStorm and XDebug. These instructions aren't specific to PHPStorm, however. They can be used in any IDE/editor that supports remote XDebug connections.

At PHP North West I had huge difficulties configuring Xdebug and both my PHP and PHP Storm so they play nice in a CLI environment. So here, for others and almost certainly my future self, is how to do it.

He goes through each step of the setup process assuming that you already have the necessary software installed. He shows how to configure XDebug for remote debugging in the ini configuration, setting up PHPStorm to receive the connection and, finally, adding the "XDEBUG_CONFIG" environment variable to tell PHP to relay the requests back to your waiting IDE.

tagged: remote debugging phpstorm commandline xdebug configure setup environment

Link: http://blog.hock.in/2015/05/04/debugging-php-command-line-with-php-storm/

Laravel News:
Homestead Now With Blackfire Support
Feb 27, 2015 @ 10:05:47

As is mentioned in this new post to the Laravel News site, the Homestead development environment now comes with support for the Blackfire.io profiler.

Blackfire Profiler by SensioLabs automatically gathers data about your code's execution, such as RAM, CPU time, and disk I/O. Homestead makes it a breeze to use this profiler for your own applications. All of the proper packages have already been installed on your Homestead box, you simply need to set a Blackfire ID and token in your Homestead.yaml file

With the configuration set up, the only installation the user needs to make is setting up the companion extension (Chrome) for your browser.

tagged: blackfire support laravel homestead development environment performance

Link: https://laravel-news.com/2015/02/homestead-now-with-blackfire-support/