Module/Library for creating content driven websites with and Zend Expressive

4.3.0 2018-11-26 10:49 UTC


Build Status Test Coverage


This module/library's purpose is to ease development of content driven websites using's content API as a backend service.

If you haven't heard of Prismic before, you can find out about it here.


This module is only suitable for Zend Expressive ^3.0 and PHP ^7.1

Furthermore, it uses a fork of the official php library which you can see here: netglue/prismic-php-kit. This fork is quite different to the official kit and I recommend looking through the docs/code to make yourself aware of the differences if you are already familiar with the official lib.


Install with composer ala composer require netglue/expressive-prismic

This should also ask you if you want to inject the config provider too.


$ composer install
$ vendor/bin/phpunit

Basic Configuration

This library exposes the Prismic API instance in your container as Prismic\Api. At the very least, you'll need to configure your credentials thus:

    return [
        'prismic' => [
            'api' => [
                'token' => 'Permanent Access Token',
                'url' => '',

Defining Routes

In order to allow you to specify properties of a document to look out for during routing, you must map the route parameter names you want to use to the prismic document/api equivalent. The defaults are:

    'prismic' => [
        'route_params' => [
            'id'       => 'prismic-id',
            'bookmark' => 'prismic-bookmark',
            'uid'      => 'prismic-uid',
            'type'     => 'prismic-type',
            'lang'     => 'prismic-lang',

So, assuming the above, to define a route to a bookmarked document, you would configure something like this:

     * @var \Zend\Expressive\Application $app
     * @var \Zend\Stratigility\MiddlewarePipeInterface $middlewarePipe
    $app->route('/', [$middlewarePipe], ['GET'], 'home')
            'defaults' => [
                'template' => 'page::default',
                'prismic-bookmark' => 'home',

Normally, to save yourself some effort, you'd have a template that's capable of rendering perhaps any page of a given type such as a 'case-study' type. Let's say you want the url /case-studies/{case-study-uid}, then you'd define a route like this (If you are using FastRoute):

    $app->route('/case-studies/{prismic-uid}', [$middlewarePipe], ['GET'], 'case-studies')
                'defaults' => [
                    'template' => 'my:case-study',
                    'prismic-type' => 'case-study',

Cache Busting Webhook

You will see in Factory\PipelineAndRoutesDelegator that two routes are wired in by default, one of these is the webhook to bust the cache. In order to use it, you will need to provide the shared secret that sends in it's webhook payload to a local configuration file or Config Provider like this:

    return [
        'prismic' => [
            'webhook_secret' => 'SomeSuperToughSharedSecret',

By default the secret is null, so if you don't set it the Validation middleware will cause a TypeError due to the secret string being a constructor dependency. If it's really important that you don't use a shared secret, you'll have to override ValidatePrismicWebhook with your own middleware or configure your own pipeline and webhook handlers.

The Url of the webhook will be /prismicio-cache-webhook - given a valid Json payload containing a matching shared secret, the pre-configured middleware will empty the cache attached to the Prismic API instance.

The webhook route points to a middleware pipe named ExpressivePrismic\Middleware\WebhookPipe so if you want to modify the pipeline to do other things, or replace it entirely, just alias that pipe to different factory or implement a delegator factory for the pipe.

Link Resolver

The Link Resolver is a concept introduced by Prismic to turn documents, or document link fragments into local urls and there's a concrete implementation in this package at ExpressivePrismic\LinkResolver.

Using the same setup for routing parameters, it tries to use the Expressive URL helper to generate local URLs. It's setup in the container as Prismic\LinkResolver as well as ExpressivePrismic\LinkResolver and throughout the package it's retrieved by the name of Prismic\LinkResolver so it's easy to replace with your own concrete implementation if you need one.


There's another route that's auto-wired like the cache busting webhook for initiating previews. All you have to do is add the URL in the settings on your Prismic repository and clicks on the preview button in the writing room will put the site in preview mode. You can see how this is configured in Factory\PipelineAndRoutesDelegator - the URL is /prismic-preview

In 4.2.0, the middleware responsible for initiating a preview session was altered to catch an exception that is thrown when the preview token has expired and subsequently return a simple html response with a descriptive error. Previously, in this situation, an uncaught exception would have occurred.

View Helpers

URL Helper $this->prismicUrl()

This view helper will generate a local URL using the link resolver. It's __invoke() method accepts

  • string - Treated as a Document ID
  • \Prismic\Document
  • \Prismic\Document\Fragment\LinkInterface

Fragment Helper $this->fragment()

This view helper operates on the current resolved document and provides an easy way of rendering simple fragments to views. It does not require the fully qualified fragment name, ie. documentType.fragmentName and instead you can pass it just 'fragmentName'.

$this->fragment()->get('title'); will return the fragment object.

$this->fragment()->asText('title'); will return the text value of the fragment.

$this->fragment()->asHtml('title'); will return the HTML value of the fragment.

CMS Managed Error Pages for Production

Error handling is wired in by default… If you are using the Whoops error handler in development, you'll need to disable development mode composer development-disable to experience custom errors and 404's

404 Errors

In the event of a 404, by default, Expressive will execute the \Zend\Expressive\Handler\NotFoundHandler. This module provides a pipeline in \ExpressivePrismic\Middleware\NotFoundPipe that initialises previews and experiments, locates a bookmarked error document in the Prismic API and renders that document to a template.

To take advantage of pretty CMS managed 404's, first you will have to specify in your configuration the bookmark name for the error document in your repository and the template name to render like this:

    return [
        'prismic' => [
            'error_handler' => [
                'template_404'   => 'some::template-name',
                'bookmark_404'   => 'some-bookmark',

Note that in development mode, 404's experienced after a route has matched, i.e. the Prismic Document can't be found, will cause a DocumentNotFound exception, thereby delegating to the Whoops error handler.


Presenting a pretty error page during errors and exceptions are handled in much the same way as 404's. Again, you'll need to configure a bookmark and a template name used to render the content.

    return [
        'prismic' => [
            'error_handler' => [
                'template_error'   => 'some::template-name',
                'bookmark_error'   => 'some-bookmark',

The fallback (i.e. when the error document cannot be retrieved from the api) for exception situations is a simple plain text message stating that an error occurred. This fallback is not currently configurable to be anything more fancy than that.