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

Dependencies in Disguise
Sep 28, 2015 @ 08:48:27

On the PHP.cc's site has an article that looks at dependencies in disguise based on a "workshop" one of their members, Stefan Priebsch, gave at the recent Bulgaria PHP Conference.

Yesterday I gave a presentation at the [Bulgaria PHP Conference](https://thephp.cc/dates/2015/09/bulgaria-php-conference) (a great event, by the way). Following an [ad-hoc workshop](https://twitter.com/s_bergmann/status/647732967087939584) that I gave as part of the hallway track and an entertaining hackathon, I decided it was too late to join the party and went back to the hotel with some other speakers. Checking out how the day was reflected in social media, I contributed a few more tweets to a [conversation](https://twitter.com/tim_bezhashvyly/status/647861115721003008) that had started earlier in the day ([here](https://thephp.cc/dates/2015/09/bulgaria-php-conference/solid-mvc) are the slides of my talk that people are referring to). I am writing this to clarify my point, and help everybody to understand better.

He talks about dependency injection as a best practice that's followed in libraries all over the PHP ecosystem, making it easier to work with objects and their needs. Sometimes this means using a dependency injection container and others it's just constructor/method injection. He talks about how these objects are build in factory methods and recommends making one factory but points out that this only really works when all the objects you need are known up front. However, he gives several (code) examples of places where this could be difficult and how some are using service locators to solve the problem. He points out, however, that this then expands the API of the application out way too far, opening it up to objects all across the application when there may be no need. This is where the hidden dependencies can come in, things masked behind the use of a single service locator. He recommends solving the issue with more customized locators, as in his example of routing locator used to handle dependencies for a POST HTTP request.

tagged: dependency disguise injection service locator bestpractice solid development

Link: https://thephp.cc/news/2015/09/dependencies-in-disguise

Paul Jones:
Service Classes, Payloads, and Responders
Aug 12, 2015 @ 10:52:27

Paul Jones has written up a post talking about service classes, payloads and responders and how they can help pull logic out of controllers and into more reusable chunks. It's inspired by comments and methods mentioned in another earlier post from Revath Kumar.

Revath Kumar has a good blog post up about extracting domain logic from controllers and putting that logic in a service class. After reading it, I commented that with a little extra work, it would be easy to modify the example to something closer to the Action-Domain-Responder pattern. In doing so, we would get a better separation of concerns (especially in presentation).

Paul applies some of the concepts that Revath outlined to the ADR pattern, suggesting that service classes should always return Payloads and the reduction of functionality in the controller overall. He includes an example of what the resulting code would look like, following along with the "orders" scenario outlined in Revath's post.

tagged: service class payload responder adr action domain responder designpattern

Link: http://paul-m-jones.com/archives/6172

Revath S Kumar:
PHP : Service classes
Aug 07, 2015 @ 12:08:40

Revath S Kumar has a post to his site that wants to help get you started writing service classes for your application. Service classes are useful when interacting with multiple pieces of data that need either interaction or correlation.

When I started with MVC in PHP, I used to write the whole logic in controller, then when I learned about skinny controllers fat models I reduced the code in controller and moved logic into models. But that was not enough. [...] In order to make this more convenient and reusable I thought of abstracting the logic for creating the order into service classes. I got this idea of service classes from Ruby on Rails. So when I came back to PHP world I thought of using service classes.

He uses examples of service classes he's written for a Yii framework based application, showing a before and after example of his "Orders" controller. He moves most of the logic from the controller (less reusable) into a service class that creates the order record based on the data its been provided. He uses exceptions to handle validation errors, making it simpler in the controller to catch multiple exception types in one place.

tagged: service class introduction yiiframework tutorial orders

Link: http://blog.revathskumar.com/2015/08/php-service-classes.html

Rob Allen:
Accessing services in Slim 3
Jun 23, 2015 @ 10:51:36

Rob Allen has a new post to his site today showing you how to access services in a Slim 3 application using container injection instead of the previous "getInstance" method.

One of the changes between Slim Framework 2 and 3 is that the application singleton has gone. [...] In general, you didn't need access to $app itself, but rather you wanted access to something that the app knows about, such as a database adapter, or the router for access to the urlFor method to create a URL to a route. With Slim 3, there is no getInstance() on App, so you need to inject the instances of whatever you need where ever you need them.

He shows you how to create a simple Slim dependency injection container (service locator?) and push two kinds of objects in for later reuse. He shows how to reference this container from inside of your routes in both the callable/closure and class contexts. He also includes an example of referencing the same container from inside middleware (again in both the closure and class contexts).

tagged: slim microframework framework slim3 service access container this

Link: http://akrabat.com/accessing-services-in-slim-3/

SitePoint PHP Blog:
Deploying PHP apps to DigitalOcean with Dploy.io
Jun 22, 2015 @ 12:35:23

On the SitePoint PHP blog today editor Bruno Skvorc shows you how to deploy your PHP applications with Dploy.io, a service that aims to make deploying and hosting your applications simpler.

In this tutorial, we’ll take a look at how to deploy a PHP application with Dploy, a tool that’s free (and full-featured) for a single application, which makes for a perfect test case on whether or not it’s worth paying for. Before continuing, go ahead and sign up for a free account. Specifically, we’ll deploy a simple app I made on DigitalOcean.

He helps you get a Digital Ocean droplet set up with the necessary requirements, the software you'll need to install and the configuration changes to make. He then moves over to Dploy and shows how to set up a basic configuration through their web control panel. This includes the environment, deployment mode and the branch of the repository to pull from. He also includes instructions for setting the correct permissions on the system and how to start the deployment. He shows screenshots of the resulting status update and how to check to be sure everything's working as expected. He ends the post with a quick look at making deployments following this via the special "[deploy]" tag in the commit message.

tagged: deploy application tutorial dployio service digitalocean

Link: http://www.sitepoint.com/deploying-php-apps-digitalocean-dploy-io/

Marc Morera:
Lazy Commands in Symfony
May 08, 2015 @ 08:13:22

In the latest post to his site Marc Morera about the use of "lazy services" with Symfony2. In his examples, he uses a command line application to illustrate his point, but it could apply elsewhere as well.

Since Symfony version 2.4 you can define your controllers and commands as services. This is so useful as long as you need to treat your classes as much decoupled as possible. [...] When we define as lazy a service, this is not instanced when is injected, but only when is accessed. [...] The point here is to define our service intended to work with the model as lazy.

He shows how to implement this kind of "lazy" handling in a command, registering the commands into the services but not creating the instances of them until they're used. He includes some example code showing how this is set up and offers a few tips on the implementation and common issues to think about.

tagged: symfony2 command lazy service register tutorial

Link: http://mmoreram.com/blog/2015/05/08/lazy-commands-in-symfony/

Knp University:
Symfony Service Expressions: Do things you thought Impossible
May 06, 2015 @ 12:50:13

The Knp University site there's a new post showing you an interesting thing you can do with Symfony2 service definitions - use more complex expressions to load and supply service dependencies.

[Using] the @= means that you’re using Symfony’s Expression Language, which let’s you mix dynamic logically into your normally-static service definitions. Normally, if you want to inject a repository, you need to register it as a service first, using a factory. And while that’s fine (and probably better if you’re injecting the factory a lot), using the expression language is well, kinda cool.

They include an example of the compiled container code that would be produced from the example expression. They also show how to use the same expression language to read configuration information and pass it in as an argument to the created service. The show the pull of a configuration setting "email_from_username" and how to inject it via the expression language.

tagged: service expression language tutorial introduction configuration example

Link: http://knpuniversity.com/blog/service-expressions

SitePoint PHP Blog:
User Authentication in Symfony2 with UserApp.io
Mar 19, 2015 @ 09:18:18

On the SitePoint PHP blog Daniel Sipose has written up a tutorial showing you how to use the UserApp.io service to authenticate users for your Symfony2 applications.

UserApp.io is a handy user management tool and API. It provides a web interface to deal with user accounts (and the many features this involves) and an API to hook them into your own web application. The purpose of this service is to make it easier and safer to manage user authentication by not having to worry about that on your own server. It has SDKs and various wrappers for many programming languages and frameworks and the price is affordable. Yes, it comes with a price but you can get started freely with quite a lot of things to play around with.

He makes use of this library (his own creation) and the UserApp.io SDK to hook into Symfony2's own Security component authentication handling. He starts by explaining some of the classes he'll be creating including the form authenticator, a user provider, the logout handler and an custom exception. The full code is included for each as well as the changes you'll need to make to the YAML configuration to hook it all together.

tagged: user authentication symfony2 userappio service tutorial

Link: http://www.sitepoint.com/user-authentication-symfony2-userapp-io/

Matt Stauffer:
Conditionally Loading Service Providers in Laravel 5
Mar 12, 2015 @ 10:16:35

Matt Stauffer has a new post to his site showing you how to conditionally load providers in your Laravel-based application with some additional code in the AppServiceProvider.

Since Laravel 5 flattened a lot of the environment-specific structures, much of the configuration that was once stored in different config directories for each environment has now moved into .env files. But one that can't just live in .env is the environment-dependent loading of service providers.

He starts with a look at the normal service provider loading process, using the application configuration and adding them to the "providers" list. This loads them every time a request is made, even if they're not needed. His solution adds some code to the "register" method in the AppServiceProvider class to check the environment (like "production" versus "dev") and loads different providers based on the result.

tagged: condition load service provider laravel5 appserviceprovider register

Link: https://mattstauffer.co/blog/conditionally-loading-service-providers-in-laravel-5

SitePoint PHP Blog:
Push your Drupal Site’s Events to your Phone with Pushover
Feb 12, 2015 @ 12:54:12

The SitePoint PHP blog has a new tutorial posted (by Daniel Sipos) about pushing notifications from your Drupal-based application via the Pushover service.

In this article I am going to show you how you can integrate Pushover with your Drupal site. I will illustrate a couple of examples of how you can use Pushover to notify yourself as soon as something happens on your site. The code I write in this article is also available in this repository so you can just clone that if you want to follow along.

He starts with an introduction to Pushover and what kinds of features it offers for the handling of push messages (with the app being not free, but "very affordable"). He help you get everything you need set up including a Pushover account and the Pushover class to use in a custom Drupal module. He includes the code you'll need to configure the module to use the library and a method to create the Pushover class instance. He then shows how to send messages for things like the addition of a new comment and user login via hooks, sending a message when an administrator logs in.

tagged: drupal tutorial pushover push message service mobile application

Link: http://www.sitepoint.com/push-drupal-sites-events-phone-pushover/