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

Matthias Noback:
Layers, ports & adapters
Aug 04, 2017 @ 11:43:11

Matthias Noback has a series of posts on his site sharing some of his thoughts around layers, ports and adapters in application architecture:

Looking back at my old blog posts, I think it's good to write down a more balanced view on application architecture than the one that speaks from some of the older posts from 2013 and 2014. Before I do, I allow myself a quick self-centered trip down memory lane.

He's posted all three parts of the series, each providing either some background on him and his previous work or principles and architecture suggestions:

Each part includes code and/or structure suggestions to help clarify points made along the way.

tagged: layer port adapter symfony architecture opinion

Link: https://php-and-symfony.matthiasnoback.nl/tags/architecture/

Maintain Slim PHP MVC Frameworks with a Layered Structure
Apr 07, 2017 @ 11:17:53

The Toptal.com blog has a tutorial posted by Elvira Sheina showing you how to keep a framework project "slim" and manageable in a MVC pattern using a "layered" structure. This structure adds a few extra components to the traditional MVC design to keep functionality cleaner and easier to maintain.

Fat controllers and models: an inevitable problem for most large-scale projects based on MVC frameworks such as Yii and Laravel. The primary thing that fattens controllers and models is the Active Record, a powerful and essential component of such frameworks.

She starts by talking about one of the main issues in MVC applications - "fat" controllers. In this example the controllers contain the bulk of the logic for the application making it difficult to modify and potentially reuse in other places. This is particularly bad when the Active Record pattern is used and the problem of it violating the SRP (Single Responsibility Principle of SOLID development). Instead she promotes the idea of the "layered" design using controllers, a service layer, DTOs, view decorators and a repository layer. She then shows how to implement this kind of structure and tie each of the pieces together with code examples for each piece.

tagged: tutorial mvc framework structure layer dto repository activerecord decorator service

Link: https://www.toptal.com/php/maintain-slim-php-mvc-frameworks-with-a-layered-structure

BitExpert Blog:
Enforce software layer dependencies with deptrac
Aug 19, 2016 @ 10:54:24

On the BitExpert.de blog there's a new post from Stephan Hochdörfer covering the enforcement of software layer dependencies with the help of the deptrac tool from Sensiolabs.

Deptrac is a tool recently announced by Sensionlabs. It helps you keep dependencies between the different layers in your architecture under better control by providing insight into the current state of the dependencies and warns you when unwanted dependencies get introduced.

He gives the commands to get the tool installed and how to initialize the repository with a default configuration file. He then provides an example using the Adrenaline framework and how the request/response relate to the HTTP handling. He includes the configuration changes to make for these relationships and, finally, how to run the analysis on your code to ensure the dependencies are correct.

tagged: software layer dependencies deptrac sensiolabs

Link: https://blog.bitexpert.de/blog/enforce-software-layer-dependencies-with-deptrac/

Refactoring Legacy Code: Part 7 - Identifying the Presentation Layer
Jul 03, 2014 @ 12:57:39

NetTuts.com has posted part seven in their "Refactoring Legacy Code" series today, continuing on with the refactor of their example application to improve maintainability and testability. In this latest article they focus in on the presentation layer.

In this seventh chapter of our refactoring tutorials, we will do a different type of refactoring. We observed in the past lessons that there is presentation related code scattered all over our legacy code. We will try to identify all the presentation related code that we can and we will then take the necessary steps to separate it from business logic.

The tutorial starts with a look at the Single Responsibility Principle (part of the SOLID design principles) and how it relates to the idea of clean architecture. They continue down the path of separating out the business logic and isolating it from the presentation layer (the display* handling). They create an "Extract" class that combines the logic and presentation though combination functionality. They walk you through the code, showing the changes you'll need to make and the tests to match.

tagged: refactor legacy code series part7 presentation layer isolating

Link: http://code.tutsplus.com/tutorials/refactoring-legacy-code-part-7-identifying-the-presentation-layer--cms-21593

Paul Jones:
What Application Layer Does A DI Container Belong In?
Feb 12, 2014 @ 09:11:17

Paul Jones has a new post to his site today with his thoughts about where dependency injection belongs in the application layer structure.

James fuller asks: "any thoughts about which layer of the application we should be using a DI container like Aura.Di? Highest layer possible?" Twitter is too constrained and ephemeral for a good response, so I’ll answer that question here.

Based around his definition of a dependency injection container (and service locator), he suggests that the DI container should reside outside of the normal application structure, possibly created in the bootstrap. He also talks some about class inheritance and the use of dependencies passed through from parent to child classes (and how common practices can break this).

tagged: application layer dependency injection opinion auradi

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

Joshua Thijssen:
Decoding TLS with PHP
Dec 31, 2013 @ 10:17:19

Joshua Thijssen has posted a walk-through of some work he's done to create a TLS decoder in PHP. TLS (Transport Layer Security) is a method for encrypting data being sent back and forth between the client and server, similar to how SSL is used.

As a proof of concept I wanted to see in how far I could decode some TLS data on the client side. Obviously, this is very complex matter, and even though TLS looks deceptively simple, it isn’t. To make matters worse, PHP isn’t quite helping us making things easy neither.

His solution (code posted here) goes through a few steps to finally get to the actual data:

  • Capturing TLS data
  • Gathering all the necessary fields
  • From pre-master-secret to master-secret (decoding TLS_RSA_WITH_RC4_128_SHA)
  • Partitioning our master-secret
  • Decoding our data
  • Verifying message integrity

For each step along the way he shares the relevant code and a brief description of what's happening. If you want to see the end result and try it out for yourself, check out his repository.

tagged: decode tls transport layer security protocol data tutorial

Link: http://www.adayinthelifeof.nl/2013/12/30/decoding-tls-with-php

Rob Allen:
Objects in the model layer: Part 2
Apr 02, 2013 @ 11:55:50

Rob Allen previously posted about some of his practices around the different types of objects in the model layer of his Zend Framework 2 applications. In this latest post he follows up and shares some example code for the different types.

I previously talked about the terms I use for objects in the model layer and now it's time to put some code on those bones. Note that,as always, all code here is example code and not production-ready.

He includes sample classes related to his "books" examples - a "book" entity (with title, author, id and ISBN), a mapper object to load/save/delete the entity and a service object that provides an interface for the entity to the rest of the application.

tagged: object model layer entity mapper service interface book


Lukas Smith:
Good design is no excuse for wasting time
Mar 28, 2013 @ 11:51:51

In his most recent post Lukas Smith suggests that good design isn't an excuse for wasting time. He's basically saying that Symfony2, because of how it's designed and implemented, isn't a RAD (rapid application development) framework and that it's about time for some layers to be added to help get it there.

Symfony 1.x I would put into a category of frameworks focused on RAD, aka rapid application development. [...] So for those people who were happy focusing on the 80% use case Symfony2 is a step back. Suddenly the same features take longer to implement, take longer to modify later on and on top of that the learning curve is steeper.

He suggests that work be put into "RAD layers" that can sit on top of Symfony2 and provide some of the more familiar features people are used to from things like CakePHP, Yii and CodeIgniter. There's been a few tries to accomplish this with only one getting the closest in his opinion - the KnpBundle.

tagged: good design symfony2 rapid application development framework layer


Working with Slim Middleware
Feb 21, 2013 @ 09:23:24

On PHPMaster.com Timothy Boronczyk has written up a tutorial about using the Slim microframework as a sort of "middleware" in your application - a wrapper around other functionality with an easier to use interface.

Slim is a microframework that offers routing capabilities for easily creating small PHP applications. But an interesting, and powerful, feature is its concept of Middleware. [...] I've found middleware to be an eloquent solution for implementing various filter-like services in a Slim app, such as authentication and caching. In this article I'll explain how middleware works and share with you a simple cache example that highlights how you can implement your own custom middleware.

He talks about what "middleware" is (complete with illustration) and how Slim can be used as a layer in the middleware stack. His example is a caching layer, based on Slim, that takes a request, checks the cache for it and returns it if it exists. If not, it saves the content to a database. He also includes code examples of how to use the "add" method to introduce your middleware libraries into the Slim application.

If you'd like more examples, the Slim project has several middleware examples provided in their "extras" github repository.

tagged: slim middleware tutorial cache layer microsframework


Evolving Toward a Persistence Layer
Sep 12, 2012 @ 10:51:17

On NetTuts.com there's a new article posted that introduces you to the concept of a persistence layer in a PHP application:

One of the most confusing design pattern is persistence. The need for an application to persist its internal state and data is so tremendous that there are likely tens – if not hundreds – of different technologies to address this single problem. Unfortunately, no technology is a magic bullet. [...] In this tutorial, I will teach you some best practices to help you determine which approach to take, when working on future applications. I will briefly discuss some high level design concerns and principles, followed by a more detailed view on the Active Record design pattern, combined with a few words about the Table Data Gateway design pattern.

Included in the post is a high-level application design with the business logic is at the core and the persistence technology/layer exists outside of it. They show how to create a simple, working solution for a persistence layer to handle a blog post and its contents. It talks about characterization tests, the table gateway design pattern and the possible move to the active record pattern.

tagged: persistence layer tutorial logic blog example