How to create your own controller plugin in ZF2?

Our step-by-step guide explains how you can create your own controller plugin in zf2.Fortunately creating controller plugin is very easy task.

Create a directory called plugin in your module controller directory, then create a file for example ,Myfunctions.php. then place the blow code.

Myfunction.php file is a php class that will extend the zf2’s AbstractPlugin, getCurrentuserID() function is our sample method , you can create ‘n’ number of method with ‘n’ number of arguments.

After creating plugin we need to register the plugin into your module config, open your plugin module’s module.config.php file , then put the blow code.

Now we can call the plugin inside the any controller by just calling this $plugin = $this->Myfunction();.

Here is the example.

That’s it.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.