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

Tomas Votruba:
How to Slowly Turn your Symfony Project to Legacy with Action Injection
Apr 24, 2018 @ 14:55:49

Tomas Votruba has a new post to his site showing how to "turn your Symfony project to legacy" through the use of action injection for mapping controllers and methods to request handling.

The other day I saw the question on Reddit about Symfony's controller action dependency injection. More people around me are hyped about this new feature in Symfony 3.3 that allows to autowire services via action argument typehints. It's new, it's cool and no one has a bad experience with it. The ideal candidate for any code you write today.

Since Nette and Laravel introduced a similar feature in 2014, there are empirical data that we learn from.

Today I'll share the experience I have from consulting few Nette applications with dangerous overuse of this pattern and how this one thing turned the code to complete mess.

He starts off with some example code, asking where the issue is showing a call to a service handler to process the an argument. This would be used when a controller is registered as a service to help reduce the amount of work to define routes and add more "magic" for request handling. While the idea sounds good, he points out some of the issues with the approach including dependency injection problems and how, if it expands outside of controllers, it can lead to a poorly written application.

tagged: symfony injection action legacy nette dependency issue

Link: https://www.tomasvotruba.cz/blog/2018/04/23/how-to-slowly-turn-your-symfony-project-to-legacy-with-action-injection/

Paul Jones:
“Action Injection” As A Code Smell
May 17, 2017 @ 16:53:16

In this recent post to his site Paul Jones suggests that "action injection" in PHP applications should be considered a "code smell" (that is, a bad practice that could indicate that the controller class is doing too much).

Circumstance has conspired to put Action Injection discussions in front of me multiple times in the past few days. Having seen the approach several times before, I have come to think that if Action Injection is the answer, you might be asking the wrong question. I find it to be a code smell, one that indicates the system needs refactoring or reorganizing.

He first covers what "action injection" is and provides an example of how it would fit in with the use of a dependency injection container. He also points to some of the frameworks that currently support this functionality natively. With that defined, he then moves into the main idea of the post - that using functionality like this is a "code smell" that could signal something that is in need of refactoring. He then provides some suggestions on things to change and mental shifts in thinking about how your application is organized. He finishes by pointing to the Action-Domain-Responder pattern as a way of implementing this and how single-action controllers can help.

tagged: action injection code smell actiondomainresponder

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

Rob Allen:
Testing Slim Framework actions
Mar 14, 2016 @ 15:45:52

Rob Allen has a quick post to his site showing you how to test Slim actions using PHPUnit and some simple pieces of the Slim framework itself to set up the needed environment.

To test a Slim Framework action, you need a request and a response object and mock whatever is in the action. This is one way to do this.

He gives an example of a simple endpoint that just returns a JSON string. He shows the code for this endpoint and how it registers with the application for an /echo route. He then gets in to the testing on the route's matching class, making an instance of the Request class and an Environment for it to work in. He ends the post by sharing teh code to pull all of these pieces together in a simple PHPUnit test that uses the assertSame assertion to verify the JSON response output.

tagged: slimframework action unittest phpunit testing tutorial request environment

Link: https://akrabat.com/testing-slim-framework-actions/

Paul Jones:
Command Bus and Action-Domain-Responder
Mar 10, 2016 @ 16:53:47

In this post to his site Paul Jones looks at the combination of the Action-Domain-Responder pattern and the Command Bus pattern in application development. In the post he answer the question about how they fit together.

Over the past few weeks, different people have asked me where a Command Bus goes in an Action-Domain-Responder system. While I’m not a DDD expert, after brushing up on the subject a little, my answer is: "In the Domain."

He starts by reviewing the three pieces of the ADR pattern with brief descriptions of each. The then covers the Command Bus pattern, linking to several other resources with more details about the pattern itself and a quick summary of their main points. He talks about how the overall structure is a part of the Command Query Responsibility Segregation pattern and suggests that, since the Command Bus pattern is a "fire and forget" kind of thing it belongs in the Domain of ADR. He gives a brief code example and answers other questions about validation and error handling as a part of this suggested flow.

tagged: action domain responder adr commandbus architecture suggestion

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

Zaengle Blog:
Laravel as an Intermediary
Dec 03, 2015 @ 17:11:21

In this tutorial on the Zaengle blog Jesse Schutt shows you how to use a Laravel application as an "intermediary" between several services and tie them together so a single action could kick off a series of events.

One of our clients recently came to us with the following workflow they'd like the Zaengle team to implement for them: They wanted to compose a blog entry in their CMS. Upon publishing of the entry, they wanted the content of the blog entry to be emailed to a filtered group of their customer database (stored in Marketo). Finally, they wanted to be able to track email metrics from within their customer database.

[...] After brainstorming with the team and client, we decided that since there were at least 3 different systems in play (CMS, Customer Database, & Mail Processor), we needed to write a custom application that would bring all of them together.

He then walks you through the solution they came up with, showing how it makes use of webhooks, API requests and work with their own database. He talks briefly about some of the benefits of the setup and how they arranged the testing of the data flow between the pieces of the system.

tagged: laravel intermediary multiple system process action

Link: http://zaengle.com/blog/laravel-as-an-intermediary

Marc Morera:
Re-thinking Event Listeners
Aug 21, 2015 @ 14:17:34

Marc Morera has posted an interesting article to his site suggesting a re-thinking of how event listeners are used in applications and libraries.

Let’s talk about Event Listeners. Do you know what an Event Listener is? Well, if you are used to working with Symfony, then you should know what is intended for. If you don’t, don’t hesitate to take a look at the Symfony documentation. This post aims to start a small discussion about how an Event Listener should look like if we really want to keep things decoupled.

The starts with a brief summary of the post (tl;dr) for those in a hurry but goes on to explain things in a bit more detail too. He starts by laying a foundation, introducing what event listeners are. He also shows how they're commonly implemented and used (in Symfony2 specifically but it applies more generally too) to trigger actions in applications. He suggests decoupling things a bit more from the flow of the action and allowing, in this case, access to both the order and the customer (on a "order created" action). He takes it one step further and decouples the sending of an email into a service and then creates an instance of this when needed in the event and not before.

tagged: event listener decouple ecommerce email order action

Link: http://mmoreram.com/blog/2015/08/20/re-thinking-event-listeners

Paul Jones:
Service Classes, Payloads, and Responders
Aug 12, 2015 @ 15: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

Alejandro Celaya:
Working with custom column types in Doctrine. Enums.
Jul 30, 2015 @ 13:37:45

Alejandro Celaya has a post to his site showing you how to work with custom types in Doctrine, more specifically with the "enum" type.

Doctrine is currently the most used ORM in PHP. It makes it very easy to work with databases in an object oriented way. It comes with a set of built-in column types that map database types with PHP types. For example, the datetime column type, persists the value of an entity column as a datetime in the database and handles it as a DateTime object when the entity is hydrated.

Type conversions work both ways, so column types take care of casting database to PHP types and vice versa. In this article I'm going to explain how to define custom column types so that we can persist our own objects into the database and hydrate them back.

He points out that, while PHP itself lacks the "enum" data type, you can simulate it with a library like this. He uses this library to create a custom Doctrine object type that mimic enums in the getting and setting of a value to one of a few options. In this case it's values representing the CRUD methods. He shows the code to link the Type back to the Action which then gives it understanding of what the valid enum values can be. He also points out another package that he published recently that takes some of the work out of creating the boilerplate code for the enum.

tagged: package action tutorial enum type doctrine custom library

Link: http://blog.alejandrocelaya.com/2015/07/28/working-with-custom-column-types-in-doctrine-enums/

Paul Jones:
Action-Domain-Responder and the “Domain Payload” Pattern
Oct 01, 2014 @ 15:16:11

Paul Jones has a new post with more information about his proposed "Action-Domain-Responder" design pattern (a replacement for the typical MVC) and suggests a new piece, the Domain Payload pattern. This pattern would use a domain payload object to wrap the data and provide the responder with additional handling and context.

In Action-Domain-Responder the Action passes input to the Domain layer, which then returns some data for the Action to pass to the Responder. In simple scenarios, it might be enough for the Responder to inspect the data to determine how it should present that data. In more complex scenarios, though, it would make more sense for the Domain to pass back the data in a way that indicates the status of the data. Instead of the Responder inspecting the Domain results, the Domain should tell us what kind of results they are.

He shows a code example of this Domain Payload object in action, starting with some typical MVC code and refactoring it along the way into an ADR structure. He shifts from a typical model into a more domain-driven approach and describes the wrapping of the data in the payload, context for the contents (even just a class name helps) and how those relate to the actual output. You can find the resulting code in this example over on Paul's GitHub account.

tagged: action domain responder mvc adr payload wrapper context data

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

Paul Jones:
Action-Domain-Responder, Content Negotiation, and Routers
Jul 18, 2014 @ 15:17:57

In his latest post Paul Jones comes back to his proposed application structure, the idea of Action-Domain-Responder, and answers some questions about where content negotiation happens and routing.

While talking about Action-Domain-Responder on the Crafting Code Tour, one of the common questions I got was: “Where does content negotiation happen?” My response was always: “Where does it happen in Model-View-Controller?” That opened up a discussion on how content negotiation is a tricky bit that can go in different places, depending on how you want the concerns separated, and is not a problem specific to ADR.

He goes on and tries to answer the question a bit better, pointing out that "it's a problem for everyone" isn't really good enough to take action on. He works through the different pieces of the ADR pattern, trying to reason out where the right fit is. He suggests a "first filter" on the Controller level, more specifically at the Router level. That's not to say that the Router needs to know about content handling, but it does need to know how to pass that information on.

tagged: action domain responder content negotiation routing

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


Trending Topics: