PHPDeveloper.org

Nate Krantz: How I'm Writing Unit / Functional Tests

Fri, 05/22/2015 - 16:50

In a recent post Nate Krantz has shared some of his own methods around writing functional and unit tests.

So...testing. That thing that everyone says is so important but you don't really learn about it in school. I've had some trials and tribulations with testing so I'm going to just dump out some thoughts here.

He starts with a bit of background on his own experiences in development and how he finally decided that testing would "solve everything". He started with unit tests (for a CodeIgniter application) and how he got them up and running. He talks about issues he found around dependencies (and static methods) and how he made use of mocks to reduce some of the issues with dynamic loading, at least how CodeIgniter does it. Unfortunately, this didn't work out as planned so he fell back to a test database and create more effective and simpler functional tests. Code examples are sprinkled through out the post to show how he was trying to solve the problem at different points in the process.

Link: http://www.natekrantz.com/why-test-driven-development-rocks-sucks/

NetTuts.com: Programming With Yii2: Timestamp Behavior

Fri, 05/22/2015 - 15:47

NetTuts.com has posted the next part of their "Programming with Yii2" tutorial series today. This new tutorial in the series focuses on the use of the timestamp behavior to assign the current date to an object (like for create or update dates).

In this tutorial, we'll explore Timestamp Behaviors, which reduce the amount of code you need to write with each new model for the common operation of creating timestamps for inserts and updates. We'll also dive into the Yii2 source code, examining how a behavior is implemented.

He starts with a brief look at what behaviors are (a reminder for those that may have already read about the sluggable and blameable behaviors). He then gets into the Timestamp behavior specifically and how to apply it to the sample project's "Status" instances. He shows the updates needed for the behavior configuration and rules. He also looks inside the component at the code that makes it up and the "touch" method it provides.

Link: http://code.tutsplus.com/tutorials/programming-with-yii2-timestamp-behavior--cms-23329

Bigcommerce Engineering Blog: PHP Memory Optimization

Fri, 05/22/2015 - 14:18

On the Bigcommerce Engineering blog there's a new post sharing a few tips for memory optimization in your PHP applications. This includes tips you can implement at a code level without too many changes to the server or the need for external services.

Lately, I've been working on optimizing the memory of some of our backend PHP applications and wanted to share some of the tricks that I have come across, especially dealing with large set of data using PHP.

Each of their tips includes code to illustrate the technique:

  • Always cap your internal in-memory caching
  • Use layered caching
  • (Freeing) DB Resources
  • Free large blocks of memory

Some of the examples include output from the scripts showing the reduction (or non-growth) of the peak memory usage of the script.

Link: http://bigeng.io/post/119546277718/php-memory-optimization

SitePoint PHP Blog: Bootstrapping a Laravel CRUD Project

Thu, 05/21/2015 - 17:18

The SitePoint PHP blog has kicked off a new series about using the Laravel framework to create a basic CRUD application relatively easily.

In this tutorial, we're going to build and run a simple CRUD application from scratch using Laravel 5.

The target audience for the tutorial are those completely new to the world of Laravel so they start at the beginning. He walks you through the creation of a new Laravel project and the database setup and configuration. He goes through the creation of "resourceful routing" and using the Laravel "artisan" command line tool to generate the matching controller. From there he talks about views and Blade templating followed by the creation and execution of the needed database migrations. The tutorial wraps up with a look at the model system included with Laravel and how they fit in with the Eloquent ORM.

Link: http://www.sitepoint.com/bootstrapping-laravel-crud-project/

Matthew Weier O'Phinney: Splitting the ZF2 Components

Thu, 05/21/2015 - 16:55

Matthew Weier O'Phinney has a recent post about recent work that's been done to split up the componenents in Zend Framework 2 into their own repositories and linked as dependencies.

Today we accomplished one of the major goals towards Zend Framework 3: splitting the various components into their own repositories. This proved to be a huge challenge, due to the amount of history in our repository (the git repository has history going back to 2009, around the time ZF 1.8 was released!), and the goals we had for what component repositories should look like. This is the story of how we made it happen.

Matthew talks about the methods that were used to split things apart, even more so than they already were. While the components could be installed separately before, the methods used to get there were "cringeworthy". He talks about the different methods they've tried and the version bump issue that came with them, even when no changes were present. He talks about the ultimate goal of the refactor and the techniques to get there - a combination of grafts, subtree, subdirectory-filter and tree-filter through git. He covers some of the "stumbling blocks" they hit along the way including empty merge commits. The end result was a one-line command that could be executed and split out the provided component (well, with a lot of help behind the scenes).

He ends the post talking about the speed of the extraction process (hint: parallel processing is a happy thing), the ultimate results of the entire framework being split and a few lessons they learned along the way.

Link: https://mwop.net/blog/2015-05-15-splitting-components-with-git.html

Frank de Jonge: Rendering ReactJS templates server-side

Thu, 05/21/2015 - 15:17

Frank de Jonge has posted a tutorial to his site showing how you can render React.js templates server-side in PHP. He makes use of the V8JS extension to execute Javascript inside of PHP and echo out the rendered result.

The last couple of months I've been working with ReactJS quite extensively. It's been a very rewarding and insightful journey. There is, however, one part that kept coming back to me: server-side rendering. How on earth am I going to use ReactJS when I want to render my templates on the server? So, I sat down and looked at the possibilities.

He suggests two options, running a small Node application or using the V8JS extension, and opts for trying the second option to meet his needs. He talks about the "why" of rendering server-side JS and gives a brief introduction to V8JS and the workflow he'll follow to use it. He helps you get this library via Composer to make working with it easier and provides an example of how to use it. After trying out this method, he then goes back to option #1, the small Node application (what he ended up choosing). He walks through the setup of this application, showing how to set it up inside a Lumen application and using Express to output the generated templates and data. He then hooks this into the PHP application via a simple HTTP client grabbing the results and pushing them back out to the page.

Link: http://blog.frankdejonge.nl/rendering-reactjs-templates-server-side/

PMG Blog: Symfony from Scratch

Thu, 05/21/2015 - 14:41

In the latest post to the PMG blog Chris Davis shows us how to create a Symfony application from scratch, that is without using the Symfony Standard repository/skeleton application.

The end goal here is to have an application that will send a simple Hello World message. So we're going to cover the core framework stuff, but save things like templating, database access, ORMs, and forms for later. The goal here to see how to scaffold a Symfony app to better understand why symfony standard does what it does and where to deviate. We'll end up with an app that uses the Symfony 3 directory structure.

Starting with the smallest "composer.json" he can (just symfony/symfony) he walks through the creation of the application one step at a time:

  • The Application Kernel
  • Handling Web Requests
  • What's in a Bundle?
  • Stepping into Configuration
  • AppBundle
  • Hello, World

The end result is a simple page outputting a "Hello, World" message, but it gives you a good foundation to work from and understanding of the simplest pieces needed to make a Symfony application.

Link: https://www.pmg.com/blog/symfony-from-scratch/

BeMyCTO.com: Why Doctrine ORM is not suited for PHP

Wed, 05/20/2015 - 18:09

The ByMyCTO.com blog has a recent post that makes the suggestion that the Doctrine ORM isn't suited for PHP...or to put it another way why they think it's not a good option for database integration.

I know, this title sounds like a troll. But it's not, it's a fact. I'm not saying Doctrine is a bad technology or shouldn't be used. I'm just saying it's not suited for PHP and this can lead to critical problems if misused.

He covers a few different topics including:

  • Differences between Java and PHP (and the fact that Doctrine's inspiration was Hibernate)
  • The "session problem" (entity serialization)
  • Identity Map, useless in a stateless environment
  • UnitOfWork, far too complex
  • EntityManager, too magical

Despite all of these points, he does remind the reader that Doctrine isn't useless or inherently bad, it's just that he sees it as reinforcing bad behaviors and suggests using something else.

Link: http://blog.bemycto.com/software-architecture/2015-05-17/doctrine-orm-not-suited-php/

SitePoint PHP Blog: Youtube Videos in PHP: Categories, Search and Suggestions

Wed, 05/20/2015 - 17:47

The SitePoint PHP blog continues their series showing how to integrate content from the YouTube API into your application with this new tutorial. It covers the use of categories, searching and suggestions.

In the first part, we introduced the Youtube API and built a small demo to list the most popular videos on Youtube. In this part, we will extend our application to have search functionality, and we'll also list the available categories on Youtube to let the user narrow down their area of interest.

He starts with with the updates you'll need to make to the routes and controllers for the "categories" functionality and the API call needed to popular the content. He updates the "videos" page to show the category information and integrate the category into the pagination. Next up is the search, allowing the users to find videos based on a search string. The search route/controller/view functionality is added as well. He ends the post talking about a way to extend the demo and advice to keep an eye on quotas and implementing cache.

Link: http://www.sitepoint.com/youtube-videos-php-categories-search-suggestions/

NetTuts.com: Programming With Yii2: Blameable Behaviors

Wed, 05/20/2015 - 16:33

NetTuts.com has continued their series about programming with the Yii2 framework with this new tutorial talking about blamable behaviors. These behaviors can de defined similarly to a common concept in other languages/tools, mixins

In this tutorial, I'll guide you through another of Yii2's interesting behaviors: helping automate the common web development task of assigning created by and updated by user_ids across the models in your web app using DRY coding and Yii2 BlameableBehavior. We'll also create a log that records who updated the Status table for every change made.

In his example he shows the code needed to insert and update these two dates related to users of your system. He shows what kind of updates are needed to support it in your migrations and adding in the support for the BlameableBehavior in the models. He then modifies this example to not only log the latest person to modify a record but a history of IDs in a "status log" table.

Link: http://code.tutsplus.com/tutorials/programming-with-yii2-blameable-behaviors--cms-23287

Matthew Weier O'Phinney: PSR-7 Accepted!

Wed, 05/20/2015 - 15:55

As Matthew Weier O'Phinney mentions in his latest post, the PSR-7 standard (HTTP) has passed and is officially accepted as a standard by the PHP-FIG group.

The road to PSR-7 was a long and winding one. It started in summer of 2012 as a draft proposal on HTTP clients by Benjamin Eberlei, during which others proposed that perhaps a smaller standard on the HTTP message interfaces themselves - which would also allow targeting server-side applications, as those rely on the messages.

He follows the proposal's flow through the PHP-FIG process, pointing out several others who contributed along the way and what changed along the way. He also includes a section of thanks for some of the other developers and PHP-FIG members that made contributions along the way.

Link: https://mwop.net/blog/2015-05-18-psr-7-accepted.html

Fred Muya: Configuring Custom Logging in Laravel 5

Wed, 05/20/2015 - 14:22

Fred Muya has posted a guide to his site today showing you how to configure custom logging in your Laravel 5 application. He replaces the default logging methods with an injected Monolog instance.

I recently started working on a Laravel 5 project, and I'd like to share how I set up my custom file logging. I pooled the information below from several sources (acknowledged at the bottom of the article).

He walks through each step you'll need to configure the logging:

  • Overriding the bootstrap ConfigureLogging class (including the code needed)
  • Updating your composer.json to change up the PSR-4 autoloading
  • Modifying the Kernel.php file to update the constructor for the bootstrap (both HTTP and Console)

With these changes in place you can then use the normal "Log" handling Laravel provides and the magic will all happen behind the scenes. He includes two examples of this, one with a simple log string and another with something a bit more complex (and the resulting log output).

Link: https://blog.muya.co.ke/configure-custom-logging-in-laravel-5/

Community News: Recent posts from PHP Quickfix

Wed, 05/20/2015 - 13:07
Recent posts from the PHP Quickfix site:

PHP Town Hall: Episode 41: Loosely Town Hall - Dallas Edition

Tue, 05/19/2015 - 18:48

The PHP Town Hall podcast has posted their latest episode recorded live at the Lone Star PHP conference this year (2015). In episode 41, Loosely Town Hall - Dallas Edition, they're joined by Loosely Coupled podcast hosts Jeff Carouth and Matt Frost.

This episode is brought to you from thunderstruck Dallas, as part of the awesome conference that is Lone Star PHP. We are joined once again by the dynamic duo Jeff Carouth and Matt Frost off of the Loosely Coupled podcast!

We mostly make a bunch of bad jokes then drag up the audience to talk about stuff. Lets be honest, this was a bit of a crapshoot, but Elizabeth Smith goes into some detail on PECL and the problems of ownership. PSR-7 is chatted about for the 19th time and Jordi talks a bit about pickles. Also, we debate "haytch" vs. "atch" as the correct pronunciation of the "H" in HTTP. Important stuff.

You can listen to this latest episode either via the in-page audio player or by downloading the mp3 of the show. If you enjoy it, be sure to subscribe to their feed to get the latest episodes as they're released in the future.

Link: http://phptownhall.com/blog/2015/04/18/episode-41-loosely-town-hall-dallas/

NetTuts.com: Using Laravel 5's Authentication Facade

Tue, 05/19/2015 - 17:26

The NetTuts.com site has a new tutorial posted today sharing more information about the authentication facade in Laravel 5 and how to use it to implement simple, custom authentication handling.

Authentication is a part of almost all the web applications you work with. It's really boring to keep repeating all the boilerplate code in every project. Well, the good news is Laravel 5 rids you of this boredom by providing a ready-to-use authentication facade. All you need to do is configure and customize the authentication service provider to your project's needs. In this quick tip, I am going to show you exactly how to do that.

It's a seven step process to get things up and running (it sounds like a lot but all the code is provided):

  • Setting Up the Environment
  • Setting Up the Migrations
  • Configuring the Registrar Service
  • Updating the User Model
  • Updating the View
  • Securing Your Routes
  • Modifying the Default Authentication Routes

Each step includes the code needed and a brief summary of what's happening and how it effects the overall authentication setup. He also ends the post with a brief mention of the "password reset" email functionality and where the content for that email is located.

Link: http://code.tutsplus.com/tutorials/using-laravel-5s-authentication-facade--cms-23461

Anthony Ferrara: Prefix Trees and Parsers

Tue, 05/19/2015 - 16:13

Anthony Ferrara has a new post, following up from his previous look at tries and lexers, continuing along the path to apply what he learned to a HTTP routing system.

In my last post, Tries and Lexers, I talked about an experiment I was doing related to parsing of JavaScript code. By the end of the post I had shifted to wanting to build a HTTP router using the techniques that I learned. Let's continue where we left off...

He starts off with thinking that lexing and parsing the routes out into their respective tokens instead of breaking them up as many do (i.e. splitting on the slashes). He shows the results of this lexing and some parser code to handle these results and turn them into something useful. He did find that the current setup caused a lot of overhead (255 new states per character) so he optimizes the processing with a "default" trie but it was still pretty intensive.

He decided to go a different way at this point, opting for the radix tree structure instead. He includes the implementation of this tree for parsing the routes and his matching lexer updates. Finally he shows how to apply code generation to the results of these changes and how coming back to the "slash splitting" could help...

Link: http://blog.ircmaxell.com/2015/05/prefix-trees-and-parsers.html

Lorna Mitchell: PHP7: Easiest Upgrade Yet

Tue, 05/19/2015 - 15:11

In her most recent post Lorna Mitchell talks about her own experiences in getting a current application upgraded and ready to run on PHP7. It can best be summed up in a tweet from her: "Total lines of code change needed to make the @joindin API work on PHP7: zero"

With PHP7 looking increasingly stable (relatively speaking, it's still pre-alpha so it's VERY early days and anything could happen!), and work going well on the GoPHP7-ext project to get extensions converted, I have been thinking about the migration guides we'll need to help people upgrade their existing applications. To this end, I took the simplest project I currently have (http://api.joind.in) and gave it a whirl on PHP7, using Rasmus' PHP7 dev box. [...] All in all, it wasn't a great study of what kinds of things can go wrong when upgrading projects, because as far as I can tell with the test coverage that we have, it Just Works (TM).

She points out that a major contributing factor to it "just working" in PHP7 probably has to do with the few amount of dependencies. She also suggests looking at the tools you do use and see if they're already doing work to make it cooperate on PHP7 when the time comes. She describes some codebases that should "just work" with PHP7 including smaller codebases and things created with more modern tools/libraries/frameworks/etc.

Link: http://www.lornajane.net/posts/2015/php7-easiest-upgrade-yet

Sound of Symfony: Episode 8 - Concerning command buses

Tue, 05/19/2015 - 14:41

The Sound of Symfony podcast has released their latest episode today, episode #8 - Concerning command buses with hosts Magnus Nordlander and Tobias Nyholm and special guest Matthias Noback.

In this episode we talk to Matthias Noback about command-query separation, command buses, hexagonal architecture, REST and his library SimpleBus.

You can listen to this latest episode either through the in-page player or by downloading the mp3 of the show. If you enjoy the topic or the show be sure to also subscribe to their feed to get this and future episodes as they're released.

Link: http://www.soundofsymfony.com/episode/episode-8/