Literally me

Laminas Controller Plugin

Say no more…

Code

namespace MyApp;

class MyPlugin extends \Laminas\Mvc\Controller\Plugin\AbstractPlugin {
    public function __invoke() {
        return $this;
    }

    public function say() {
        return 'Hello!';
    }
}

Configuration

'controller_plugins' => [
    'aliases' => [
        'myplugin' => \MyApp\MyPlugin::class
    ],
    'factories' => [
        \MyApp\MyPlugin::class => \Laminas\ServiceManager\Factory\InvokableFactory::class
    ]
]

Note

You can create your own factory class so as your object can access the service manager or do some special prepare for thereof. To make a factory class, just create a class with postfix Factory and implements from interface FactoryInterface.

namespace MyApp;

class MyPluginFactory implements \Laminas\ServiceManager\FactoryInterface {
    public function __invoke(\Interop\Container\ContainerInterface ContainerInterface $container, $requestedName, array $options = null) {
        ///$container is the service manager
        
        return new MyPlugin();
    }
}

Usage

In any controller:

$this->myplugin();//return object MyPlugin
$this->myplugin()->say();//quick access to a member function

Happy coding!