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

How to create helper in zf3

ZF3 helper

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

'view_helpers' => [
        'factories' => [
            View\Helper\Menu::class => InvokableFactory::class,
        ],
        'aliases' => [
            'mainMenu' => View\Helper\Menu::class
        ]
    ],

module/Admin/src/View/Helper/Menu.php
//create helper file
<?php

namespace Admin\View\Helper;

use Zend\View\Helper\AbstractHelper;


class Menu extends AbstractHelper {
	public function test($messages) {
		echo $messages;
	}
}


module/Admin/view/admin/index/index.phtml
//call helper
<?php
$this->mainMenu()->test('Manish');
?>

for use service in helper

module/Admin/src/Factory/Helper/MenuFactory.php
//create custom factory file for helper
<?php

namespace Admin\Factory\Helper;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Admin\View\Helper\Menu;

class MenuFactory 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 Menu($container);
    }

}

module/Admin/config/module.config.php

//change value of factory class

'view_helpers' => [
        'factories' => [
            View\Helper\Menu::class => Factory\Helper\MenuFactory::class,
        ],
        'aliases' => [
            'mainMenu' => View\Helper\Menu::class
        ]
    ],
module/Admin/src/View/Helper/Menu.php
//call services in helper

<?php

namespace Admin\View\Helper;

use Zend\View\Helper\AbstractHelper;

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

// This view helper class displays a menu bar.
class Menu extends AbstractHelper {

    // Menu items array.
    protected $items = [];
    // Active item's ID.
    protected $activeItemId = '';

    // Constructor.
    public function __construct(ContainerInterface $sm) {
        $this->dbAdapter = $sm->get(AdapterInterface::class);
    }

    public function getallMenu($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/view/admin/index/index.phtml
//call helper for dynamic menu fron DB
<?php
$this->mainMenu()->getallMenu();
?>

Leave a Comment

© 2014 Powered By