How to create plugin in zf3 Reviewed by Momizat on . ZF3 Plugin [php] module/Admin/config/module.config.php /* * For Plugin setting */ use Zend\ServiceManager\Factory\InvokableFactory; 'controller_plugins' => [ ZF3 Plugin [php] module/Admin/config/module.config.php /* * For Plugin setting */ use Zend\ServiceManager\Factory\InvokableFactory; 'controller_plugins' => [ Rating: 0

How to create plugin in zf3

ZF3 Plugin

module/Admin/config/module.config.php
/*
 * For Plugin setting
 */
use Zend\ServiceManager\Factory\InvokableFactory;

'controller_plugins' => [
        'factories' => [
            Controller\Plugin\AccessPlugin::class => InvokableFactory::class,
        ],
        'aliases' => [
            'access' => Controller\Plugin\AccessPlugin::class,
        ]
    ],

module/Admin/src/Controller/Plugin/AccessPlugin.php
//create Plugin file
<?php
namespace Admin\Controller\Plugin; 

use Zend\Mvc\Controller\Plugin\AbstractPlugin;

// Plugin class
class AccessPlugin extends AbstractPlugin 
{
    // This method checks whether user is allowed
    // to visit the page 
    public function checkAccess($actionName)
    {
         echo "<pre>";print_r($actionName);die;
    }
}

module/Admin/src/Controller/IndexController.php
//call plugin
public function indexAction() {
        $this->access()->checkAccess('index');
}

for use service in Plugin

module/Admin/src/Factory/Plugin/AccessFactory.php
//create custom factory file for Plugin
<?php

namespace Admin\Factory\Plugin;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Admin\Controller\Plugin\AccessPlugin;

class AccessFactory implements FactoryInterface {

   public function __invoke(ContainerInterface $container, 
                $requestedName, array $options = null)
    {
        // Get CurrencyConverter service from the service manager.
//        $currencyConverter = $container->get(CurrencyConverter::class);
        
        // Instantiate the service and inject dependencies.
        return new AccessPlugin($container);
    }

}


module/Admin/config/module.config.php

//change value of factory class
'controller_plugins' => [
        'factories' => [
            Controller\Plugin\AccessPlugin::class => Factory\Plugin\AccessFactory::class,
        ],
        'aliases' => [
            'access' => Controller\Plugin\AccessPlugin::class,
        ]
    ],
module/Admin/src/Controller/Plugin/AccessPlugin.php
//call services in Plugin
<?php
namespace Admin\Controller\Plugin; 

use Zend\Mvc\Controller\Plugin\AbstractPlugin;


use Interop\Container\ContainerInterface;
use Zend\Db\Adapter\AdapterInterface;
use Admin\Model\User;

// Plugin class
class AccessPlugin extends AbstractPlugin 
{
    // This method checks whether user is allowed
    // to visit the page 
    // Constructor.
    public function __construct(ContainerInterface $sm) {
        $this->dbAdapter = $sm->get(AdapterInterface::class);
    }
    public function checkAccess($actionName)
    {
         echo "<pre>";print_r($actionName);die;
    }
    public function getalluser($items) {
        $userModel = new User($this->dbAdapter);
        $whereCondition = array(                    
                    'is_deleted' => 'n',
                );
        $res = $userModel->getSingleRow('tbl_admin', $whereCondition);
        echo "<pre>";print_r($res);die;
//        
    }
}

module/Admin/src/Controller/IndexController.php
//call plugin for all user from DB
public function indexAction() {
//        $this->access()->checkAccess('index');
        $this->access()->getalluser('index');
}

Leave a Comment

© 2014 Powered By