Back to Blog
Middleware

Dotkernel Light improvements: PSR-15 Handlers, Vite, PHPStan

Migrating from controllers to handlers

dotkernel/dot-controller isn't going away, but we have switched to PSR-15 handlers instead. PSR-7 HTTP message interfaces defines the request handler as an individual component that processes a request and produces a response. Given that it's used in a middleware architecture, the request can be wildly different (e.g. filtered, augmented) by the time it reaches your custom code. Handlers split the code into more manageable chunks and cleaner file structure. An added benefit is it allows us to figure out an application's functionality easier. We will expand on this aspect further down in the article, in the 'Naming pattern' chapter.

Implementing PSR-15 compatible handlers

We are striving to implement every feature with the most modern standards and design patterns in mind. The PHP Framework Interop Group defines PSR-15 as common interfaces for HTTP server request handlers and HTTP middleware that use HTTP messages as described by PSR-7. The MVC design pattern is considered obsolete by respected members of the PHP community, so we are replacing it with Middleware and HTTP message design pattern.

Adopting a naming pattern for PSR-15 handlers

Now the file names reflect the functionality at a glance. We devised a naming pattern for our PSR-15 handlers that highlights the method, resource and action for each file to make navigation and onboarding that much easier. We like to keep things tidy, as well as informative.

Implementing Vite

Vite replaces webpack as our static modules bundler. It concatenates and compresses .css and .js files to enable faster downloads. It also preprocesses .scss files into .css. As far as devs using Light are concerned, Vite was configured to work similarly to webpack. We decided the technical advantages like easier dependency management and execution speed justified the migration to Vite. It's also highly recommended by the PHP community.

Replacing Psalm with PHPStan

Both Psalm and PHPStan are respected and widely-used static analysis tools. Some of the big names in the PHP ecosystem have opted for the same switch, so we decided to follow suit. Ultimately, the functionality is similar to our previous tool, but the growing interest and improved detection quality make PHPStan a great choice for our applications. PHPStan is configured to run at rule level 8 to help prevent bugs and write better code.

Other updates

Some of the smaller updates involve:

  • Adding support for PHP 8.4.
  • Enabling PHPStan and Qodana to run for PHP 8.4 as well.
  • Cleaning up and adapting the error configuration file to make use of the latest features.
  • Updating laminas/laminas-coding-standard to the latest major version.
  • Updating composer.json to make sure it's up-to-date with the latest releases for each dependency, as well as to remove any now-obsolete items.
  • Implementing a more reliable and efficient post install script to help get you up and running that much faster.
  • Removing dotkernel/dot-twigrenderer in favor of a direct implementation of mezzio/mezzio-twigrenderer.

Frequently Asked Questions

Why did Dotkernel Light switch from controllers to handlers? +

Dotkernel Light switched to PSR-15 handlers instead of dotkernel/dot-controller (which isn't going away, but is no longer used here). Since the request can be filtered or augmented by the time it reaches custom code in a middleware architecture, handlers split the code into more manageable chunks and a cleaner file structure, and make an application's functionality easier to figure out.

What naming pattern is used for the new PSR-15 handlers? +

A naming pattern was devised that highlights the method, resource, and action for each handler file, so file names reflect their functionality at a glance, making navigation and onboarding easier.

Why was Vite adopted instead of webpack? +

Vite replaces webpack as the static modules bundler, concatenating and compressing .css and .js files and preprocessing .scss files into .css. It was configured to work similarly to webpack for developers using Light, and was chosen for its easier dependency management, execution speed, and its recommendation by the PHP community.

Why was Psalm replaced with PHPStan? +

Both Psalm and PHPStan are respected, widely-used static analysis tools with similar functionality, but growing interest and improved detection quality led Dotkernel to follow other big names in the PHP ecosystem and switch to PHPStan, which is configured to run at rule level 8 to help prevent bugs and write better code.

What other smaller updates were made to Dotkernel Light? +

Other updates include adding support for PHP 8.4, enabling PHPStan and Qodana to run for PHP 8.4, cleaning up the error configuration file, updating laminas/laminas-coding-standard to its latest major version, updating composer.json dependencies and removing obsolete items, implementing a more reliable and efficient post-install script, and removing dotkernel/dot-twigrenderer in favor of a direct implementation of mezzio/mezzio-twigrenderer.