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

Matthias Noback:
Road to dependency injection
Jun 12, 2018 @ 09:31:59

In a new post to his site Matthias Noback walks you down a road to dependency injection, sharing his process of migrating a codebase from static calls to a more modern, robust dependency injection pattern.

I've worked with several code bases that were littered with calls to Zend_Registry::get(), sfContext::getInstance(), etc. to fetch a dependency when needed. I'm a little afraid to mention façades here, but they also belong in this list. The point of this article is not to bash a certain framework (they are all lovely), but to show how to get rid of these "centralized dependency managers" when you need to.

He talks about this common use case of "statics everywhere" and some of the problems with coupling your code to a static way of doing things. He walks through the steps towards modernization including injecting the container itself into the controller and shifting logic out into services. He also makes suggestions on what to do when constructor injection isn't possible and how to deal with "temporal coupling".

tagged: dependencyinjection refactor codebase static inject container tutorial

Link: https://matthiasnoback.nl/2018/06/road-to-dependency-injection/

Tomas Vortuba:
Why You Should Combine Symfony Console and Dependency Injection
May 09, 2018 @ 10:34:53

In a new post to his site, Tomas Votruba has a post sharing his thoughts about why you should combine Symfony Console and dependency injection and how it can help tp resolve issues with the overuse of static methods and functions.

I saw 2 links to SymfonyConsole in today's Week of Symfony (what a time reference, huh?). There are plenty of such posts out there, even in Pehapkari community blog: Best Practice for Symfony Console in Nette or Symfony Console from the Scratch. But nobody seems to write about the greatest bottleneck of Console applications - static cancer. Why is that?

He starts with some examples of the current status in PHP console applications including PHP_CodeSniffer, PHP CS Fixer and PHPStan. Each of these load commands to add functionality to the application which, as the application grows and changes, could be difficult to maintain in the future. Instead he recommends making use of dependency injection. He then talks about how containers are already a part of the console applications mentioned and finishes up focusing on the combination of the SymfonyConsole and SymfonyDependencyInjection components.

tagged: symfony console dependency injection combination tutorial container

Link: https://www.tomasvotruba.cz/blog/2018/05/07/why-you-should-combine-symfony-console-and-dependency-injection/

Learnk8s.io Blog:
How to deploy Laravel to Kubernetes
Apr 30, 2018 @ 13:10:24

The Learnk8x.io blog has a tutorial they've posted showing you how to deploy a Laravel application to Kubernetes. Kubernetes is a system created to make it simpler to deploy containerized applications in a clustered context.

Vagrant is very good with setting up a local environment similar to a remote server. However, in production, you will most likely require more than just one web host and one database. You’ll probably have separate services for several requirements. You also need to have mechanisms in place to ensure that the application is always online and that the servers can efficiently balance the load.

In this article, I’ll explain how to deal with the simple requirement of running a Laravel application as a local Kubernetes set up.

The article then gets into the "whats" and "whys" of using Kubernetes and an introduction to the Minikube local Kubernetes environment. It runs through some of the prerequisites to getting the deployment set up and provides the Docker configuration to create and link the containers together. With that defined it provides the commands to get the environment up and running and the application deployed. The final part of the tutorial covers the scaling of the system and allowing ingress to the application through a certain URL.

tagged: kubernetes tutorial deploy laravel application docker container

Link: https://learnk8s.io/blog/deploying-laravel-to-kubernetes

Robert Basic:
Docker containers for PHP with PHPDocker.io
Mar 28, 2018 @ 10:16:40

On his site Robert Basic covers a useful site for the PHP developers out there using (or wanting to use) Docker for their development. In this post he talks about the PHPDocker.io service that allows for the easy creation of a Dockerfile customized to the needs of your own environment.

Recently I’ve been also learning a bit more about PHPDocker.io.

He includes an example of the Docker configuration that's output from the service for a simple webserver+MySQL+PHP-FPM setup. He also includes a small update to allow the MySQL server to store its data on the local system rather than inside the container (where it would be lost on each build).

tagged: docker container phpdockerio service configuration tutorial

Link: https://robertbasic.com/blog/docker-containers-for-php-with-phpdocker-io/

Andrew Embler:
Automated Dependency Injection Using Containers
Mar 13, 2018 @ 11:58:42

In a previous post to his site Andrew Embler provided a "concrete guide to dependency injection", a method to help make code easier to test and more flexible/maintainable. In this latest post he continues the series and moves to the next topic: dependency injection containers.

I recently posted about dependency injection, a guide that I hope was easy to read and understand. It's a topic that's confusing, due in no small part to its name. As I mention there, the same term defines the simple process of externally providing instance variables to a class as it does to the Container object's magical auto-instantiation of object (which also "injects dependencies.")

I'd like to talk a bit more about that. Now that we understand the term "Dependency Injection" (which is simply providing classes to classes through a constructor or a setter), let's talk about how we might make this a little less onerous.

He starts with an example from the concrete5 project where a class would require three object instances each time it would be created. Fortunately the software includes a make command that does some of this work for you. It makes use of a dependency injection container behind the scenes to get the instances it needs. He then covers cascading dependencies, some special logic for class creation and how to "get fancy with it" by defining custom logic when an instance is created.

tagged: dependency injection container concrete5 instance tutorial

Link: http://andrewembler.com/2018/03/automated-dependency-injection-using-containers

Youssouf El Azizi:
Laravel & Docker, Zero config with Vessel
Nov 28, 2017 @ 13:28:52

On the Hackernoon site Youssouf El Azizi has posted a quick tutorial showing you how to use Vessel with Docker and Laravel to create a "zero config" setup quickly and easily.

This article is a step-by-step to get started with Vessel ( the new way to setup the Laravel setup config with docker).

[...] Vessel is a lightweight docker environment for Laravel, that means it is just a small set of files that set up a local docker development environment per project and there is no extra thing to install globally, expect Docker system itself.

The tutorial then walks you through the process you'll need to follow to get the environment up and running including the installation of Docker/Docker-compose. It then walks through the installation of Vessel and what images (containers) it will set up when executed. The post ends with a "cheat sheet" you can use when working with Vessel providing some common commands.

tagged: tutorial laravel docker configuration setup vessel container

Link: https://hackernoon.com/laravel-docker-zero-config-with-vessel-60e1aa173ea8

Nikola Poša:
Using DIC the right way
Sep 05, 2017 @ 10:24:31

In a new post to his site Nikola Poša looks at dependency injection containers and shares what he thinks is the right way to use them in your applications.

DIC stands for Dependency Injection Container, which is a tool that manages the construction and wiring up of application services. It closely relates to the letter "D" of a SOLID acronym - Dependency Inversion Principle and is employed to facilitate adhering to the principle.

By their nature, DI Containers are also Service Locator implementations, design pattern that is the exact opposite to Dependency Injection. Because of that, DI Container is a double-edged sword which can mislead you if not used wisely, and ironically bring your code into a state in which there is no dependency injection at all.

He starts off by talking about two kinds of code in an application: core versus assembly. In this case "core" code is the piece of the application that are then used by "assembly" code to make things happen. He suggests that the DIC shouldn't leak into the core and should be put behind a separation between the core code and assembly code. He includes some sample code illustrating what he means and the idea of splitting out the DIC configuration to help that layer clean.

tagged: dependency injection container tutorial core assembly code abstraction leak

Link: http://blog.nikolaposa.in.rs/2017/09/03/using-dic-the-right-way/

BitExpert Blog:
Why using code as DI config is a win!
Jul 26, 2017 @ 10:58:21

In a post to the bitExpert.de site Stephan Hochdörfer explains why he thinks that using code over configuration in a DI container is a better approach than static configuration definitions.

In my recent talk on introducing Disco - the DI container with the damn coolest name(tm) - I talk about why I believe that using XML or any other non-code configuration (YAML, JSON, ...) is not a good idea. This stirred some twitter discussion recently which led to this blog post.

Just for the record, for a very long time I was part of the XML camp - just browse my collection of old talks to see for yourself. I praised XML a lot as being the only true DI configuration format.

He then goes through some of the main issues he sees with using something like XML for the dependency container's configuration:

  • An XML editor won't give you code-completion for PHP classes or methods.
  • Refactoring won't work properly in an XML configuration file.
  • An XML editor is not capable of doing proper type checks.
  • XML is just too verbose.

For each item he provides a brief explanation and an example of XML where it helps to illustrate the point.

tagged: xml configuration code disco dependency injection container opinion

Link: https://blog.bitexpert.de/blog/why-using-code-as-di-config-is-a-win/

How to Register & Use Laravel Service Providers
Jul 20, 2017 @ 14:08:19

On the TutsPlus.com site they've posted a new tutorial showing you how to register and use service providers in Laravel and how it relates to the service container functionality.

If you've ever come across the Laravel framework, it's highly unlikely that you haven't heard of service containers and service providers. In fact, they're the backbone of the Laravel framework and do all the heavy lifting when you launch an instance of any Laravel application.

In this article, we're going to have a glimpse of what the service container is all about, and following that we'll discuss the service provider in detail. In the course of this article, I'll also demonstrate how to create a custom service provider in Laravel.

The article starts with an overview of the service container and service providers, describing what they are and providing some example code/configuration to clarify the concepts. It then gets into the creation of your own custom service provider. In their case they create a provider that doesn't really do anything but it does help to show how to make it, register it and put it to use in a controller.

tagged: tutorial register use laravel service provider serviceprovider container

Link: https://code.tutsplus.com/tutorials/how-to-register-use-laravel-service-providers--cms-28966

Get Started Running Laravel in a Docker Container
Mar 07, 2017 @ 14:07:28

The Scotch.io blog has a new tutorial posted showing you how to get a Laravel application up and running in a Docker container with a minimal amount of work. The article assumes you already have a working knowledge of Laravel and Docker, so if you're not familiar with those you'll need to read up there first.

Laravel and Docker are two very popular tools of choice when considering building for the web. Although both of them do very different things, they can both be combined to create amazing products.

For our use case, we will be running Laravel in a Docker container. This is going to be a simple demonstration on how to use both products to create real-life applications. Nothing heavy.

The tutorial starts with some of the prerequisites you'll need before getting started but quickly gets into the commands you'll need to set up your environment. Using a custom Docker container they show you the configuration to get the container set up, how to build the database connection, bring the container up and verify all is working as expected.

tagged: docker laravel container tutorial introduction installation configuration

Link: https://scotch.io/tutorials/get-started-running-laravel-in-a-docker-container