Creating a Custom ZF2 Controller Plugin

Creating a custom controller plugin in Zend Framework 2 is easy! First, begin by creating a new class. I will put this in the Application\Controller\Plugin namespace.

namespace Application\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;

class HelloWorld extends AbstractPlugin {
    public function __invoke() {
        return $this->hello();
    }

    public function hello() {
        return 'Hello World!';
    }
} 

As you can see, all we have to do is to extend the AbstractPlugin class. All this abstract class does is that it allows an instance of a controller to be injected into the plugin. For convenience, we use the magic method __invoke such that we can use the class as if it were a method. This is not required, but it is a matter of convenience. We will see another approach shortly.

To register the controller plugin, simply open your module’s configuration file (commonly module.config.php) and make sure that it contains the nested key – something like the below.

return array(
	// ...

	'controller_plugins' => array(
		'invokables' => array(
			'HelloWorld' => 'Application\Controller\Plugin\HelloWorld',
		),
	),
	
	// ...
);

Because the custom controller plugin has no dependencies, it can be instantiated directly, hence why we can add it as an invokable. If you need or wish to read up on invokables or factories, then read my article about ZF2’s service manager. If you need dependencies injected into your custom controller plugin, then you can do this in a variety of ways (they are all discussed in the previous link), but you will probably want to do something like this.

return array(
	// ...

	'controller_plugins' => array(
		'factories' => array(
			'HelloWorld' => function($pluginManager) {
				/** @var \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator */
				$serviceLocator = $pluginManager->getServiceLocator();
				return new HelloWorld($serviceLocator->get('Your\Dependency'));
			}
		),
	),
	
	// ...
);

Here we add a factory to which the plugin manager is passed as an argument. This is all handled by Zend Framework. From the plugin manager, we have access to the service manager in case we need it to retrieve dependencies that we wish to inject into our custom controller plugin. This is of course optional, so you can instantiate classes directly instead if you wish. The above example assumes that a constructor exists in your custom controller plugin that accepts your dependency as its first parameter.

Now all we have to do in order to use our controller plugin is the following.

class IndexController extends AbstractActionController
{
	public function indexAction()
	{
		$message = $this->helloWorld();
		return new ViewModel();
	}
}

$message now contains the value that we returned within our controller plugin. If you need more than a single method within your controller plugin, then remove the __invoke magic method and use the plugin like below.

class IndexController extends AbstractActionController
{
	public function indexAction()
	{
		$plugin = $this->helloWorld();
		$message = $plugin->hello();
		return new ViewModel();
	}
}

Please note that your controller plugin can be accessed using both camel case (helloWorld) and pascal case (HelloWorld), because the plugin manager normalizes the name. However, I recommend accessing it with camel case, because we are simulation that it is a method that is accessible within the controller.

Anyways, that is all it takes to write a custom ZF2 controller plugin. I hope this helped. Thank you for reading!

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *