Application Integration

Usage in a laminas-mvc Application

The following example shows one potential use case of laminas-cache within a laminas-mvc based application. The example uses a module, a controller and shows the resolving of dependencies of the controller by configuration.

Preparation

Before starting, make sure laminas-cache is installed and configured.

Installation Requirements

laminas-cache is shipped without a specific cache adapter to allow free choice of storage backends and their dependencies. So make sure that the required adapters are installed.

The following example used the filesystem adapter of laminas-cache:

$ composer require laminas/laminas-cache-storage-adapter-filesystem

Configure Cache

To configure the cache in a laminas-mvc based application, use either application or module configuration (such as config/autoload/*.global.php or module/Application/config/module.config.php, respectively), and define the configuration key caches.

In this example, the global configuration is used and a separate file is created for the cache configuration. Create a configuration file with name like config/autoload/cache.global.php and it will automatically be included:

return [
    'caches' => [
        'default-cache' => [
            'adapter' => Laminas\Cache\Storage\Adapter\Filesystem::class,
            'options' => [
                'cache_dir' => __DIR__ . '/../../data/cache',
            ],
        ],
    ],
];

The factory Laminas\Cache\Service\StorageCacheAbstractServiceFactory uses the configuration, searches for the configuration key caches and creates the storage adapters using the discovered configuration.

Cache Named config Is Not Possible

A cache named config is not possible due to internal service conflicts with MVC configuration. The service named config is reserved for project configuration and thus cannot be used with the caches configuration.

Create Controller

Create a controller class and inject the cache with the interface for all cache storage adapters via the constructor, e.g. module/Application/Controller/IndexController.php:

namespace Application\Controller;

use Laminas\Cache\Storage\StorageInterface;
use Laminas\Mvc\Controller\AbstractActionController;

final class IndexController extends AbstractActionController
{
    public function __construct(
        private readonly StorageInterface $cache
    ) {}

    public function indexAction(): array
    {
        if (! $this->cache->hasItem('example')) {
            $this->cache->addItem('example', 'value');
        }

        echo $this->cache->getItem('example') // value;

        // …

        return [];
    }
}

Register Controller

To register the controller for the application, extend the configuration of the module. Add the following lines to the module configuration file, e.g. module/Application/config/module.config.php:


namespace Application;

use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;

return [
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => ConfigAbstractFactory::class,
        ],
    ],
    // …
];

The example uses the config factory from laminas-servicemanager which allows any string to be used to fetch a service from the application service container, like the name of the configured cache: default-cache.

This means that the factory searches for an appropriate configuration to create the controller and to resolve the constructor dependencies for the controller class.

Add Factory Configuration For Controller

Extend the module configuration file to add the configuration for the controller. Use the name of the cache (default-cache), which was previously defined in the configuration of the caches, to retrieve the related cache storage instance:


namespace Application;

use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;

return [
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => ConfigAbstractFactory::class,
        ],
    ],
    ConfigAbstractFactory::class => [
        Controller\IndexController::class => [
            'default-cache',
        ],
    ],
    // …
];

Using Multiple Caches

The use more than one cache backend, the factory Laminas\Cache\Service\StorageCacheAbstractServiceFactory allows to define multiple cache storages.

Extend the cache configuration in config/autoload/cache.global.php and add more cache adapters:


return [
    'caches' => [
        'default-cache' => [
            'adapter' => Laminas\Cache\Storage\Adapter\Filesystem::class,
            'options' => [
                'cache_dir' => __DIR__ . '/../../data/cache',
            ],
        ],
        'secondary-cache' => [
            'adapter' => Laminas\Cache\Storage\Adapter\Memory::class,
        ],
        'dummy-cache' => [
            'adapter' => Laminas\Cache\Storage\Adapter\BlackHole::class,
        ],
    ],
];

Installation Requirements

Make sure that the used storage adapters are installed:

$ composer require laminas/laminas-cache-storage-adapter-memory laminas/laminas-cache-storage-adapter-blackhole

Change Used Adapter for Controller

To use a different cache adapter for the controller, change the related module configuration and use one of the previously defined names:


namespace Application;

use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;

return [
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => ConfigAbstractFactory::class,
        ],
    ],
    ConfigAbstractFactory::class => [
        Controller\IndexController::class => [
            'dummy-cache',
        ],
    ],
    // …
];

Learn More