An Overview of Fortissimo Commons

Nov 22 2013

Fortissimo is a chain of command (CoCo) framework written in PHP. Its main goal is to enable developers to write highly re-usable component-based web applications. Recently we added a new GitHub project, Fortissimo Commons, to hold dozens of utilities that we use over and over again with Fortissimo.

In this post I give an overview of these commands.

First Things First: Using Fortissimo-Commons

Just like the rest of the Masterminds PHP projects, Fortissimo-Commons is installed via Composer. Just add these lines to your composer.json:

"require": {
  "masterminds/fortissimo": "dev-master",
  "masterminds/fortissimo-commons": "dev-master",

The first includes Fortissimo itself, and the second includes Fortissimo-Commons.

As a general reminder, a Fortissimo chain of command looks something like this:

    ->does('\Fortissimo\Commons\HTTP\AddHeaders', '_set_headers')
      ->using('code', 500)
    ->does('\Fortissimo\Commons\Arrays\Build', 'json')
      ->using('error', 'An error occurred')->from('cxt:errorTitle')
      ->using('message', 'An error occurred')->from('cxt:errorMessage')
      ->using('object', array('error' => 'No data'))->from('cxt:json')

(The above is a real route from an app in production. It sends JSON errors to a jQuery client.)


There are a few utilities for working with arrays. In general, they make it easy to extract context data into arrays or to put an array's contents into context.


Take values out of your context and put them into an array.


    ->does('Fortissimo\Commons\Arrays\Build', 'a')
        ->using('foo', 'bar')

The example above will put an array named a into the context. Assuming that $_GET['user'] is matt the array will look like this:

  'foo' => 'bar,
  'name' => 'matt'


Extract does the opposite of Build: It takes an associative array and merges it into the context. This is a convenient way to put existing data into the context.



As the name implies, this provides a print formatter at the command level:

    ->does('Fortissimo\Commons\Format\Sprintf', 'a')
        ->using('format', 'My name is %s')

Assuming $_GET['user'] is matt, the above will put a into the context with the value My name is matt. Note that for the formatter, arguments are 1-indexed integers: 1 replaces the first % var, 2 replaces the second, and so on.


Fortissimo is written for web applications, and this part of Commons has some of the utility commands that we often need for web apps.


Add arbitrary HTTP headers. This is very useful for setting content types and other standard HTTP headers.


This utility function provides a standard way to build a URL from a relative path. It uses information from $_SERVER to piece together a full URL without you needing to configure anything.

There's an example of this command in the Redirect section below.


This command adds cache control headers that external caches and reverse proxies use. It's great for working with Akamai, Squid, and Varnish.


With this command you can force clients who connect over HTTP to redirect to the HTTPS (secure) version of the site.


This provides convenient access to the HTTP 3xx family of redirects. It differs from Fortissimo's internal Reroute facility in that this returns an HTTP message to the client, and the client must then make a new request for the correct URL.

    ->does('Fortissimo\Commons\HTTP\Redirect', 'a')
        ->using('redirect_type', 301)
        ->using('url', '')

The above will cause any browswer that accesses foo to be permanently redirected to The HTTP spec requires that url is an absolute URL (domain and protocol included). Since building the url is easy with the CreateURL command above, the two could be combined like this:

    ->does('Fortissimo\Commons\HTTP\CreateURL', 'redirectTo')
        ->using('path', '/another/path')
    ->does('Fortissimo\Commons\HTTP\Redirect', 'a')
        ->using('redirect_type', 301)

A common form posting idiom is to redirect a browser after a form submission. This command is useful for that.


This package is newly migrated from Fortissimo itself into Fortissimo Commons. So far it only has one command.


This command simplifies the process of receiving a file and then storing it on the local filesystem.


With JSON quickly supplanting XML as a data interchange format, it's good to have some convenient JSON commands.


Take data from the context, decode it using json_decode(), and then put it back into the context.


Understandably, this does the opposite of the DecodeJSON command.


This is a convenience command for taking any old PHP object or array, converting it to JSON, and sending it back to the client.


PHP applications frequently make use of the HTTP session (and PHP's $_SESSION) to temporarily store data. Fortissimo Commons has a few utilities for helping with this.


Store an arbitrary value in the session.

Here's an example:

    ->does('Fortissimo\Commons\Session\PutValue', 'a')
        ->using('name', 'username')

Assuming that $_GET['user'] is matt, this would set $_SESSION['username'] to matt. Remember that in from() calls you can access session data like this: from('session:username').


This removes an item from the $_SESSION and returns it into the context.


This deletes a value from the $_SESSION. Unlike PopValue it does not put the removed value into the context.

General Utilties


Apply any of the PHP filter_var() filters to anything in your context.


As we keep working on Fortissimo Commons this set will grow. There are also some other generally useful libraries for Fortissimo, such as the Fortissimo-Twig and Fortissimo-Guzzle libraries. Because they have external dependencies they can't be added to Fortissimo-Commons, but they are very useful nonetheless.