dshmkgq558192365 2016-04-04 09:25
浏览 27
已采纳

ZF2将代码从module.config.php移动到module.php

I've found on the web a way to declare a custom Navigation Menu using the following:

module.config.php in Acl module:

'navigation_helpers' => array(
    'invokables' => array(
        'menu' => 'Acl\View\Helper\Navigation\Menu',
    )
),

Now I'd like to pass a parameter to the custom Menu constructor. I'm quite new in ZF2 world but it seems that I have to create some kind of service in the module.php file. I've tried to put code into getServiceConfig and getViewHelperConfig methods without success (the custom menu is not "used").

Any idea/hint of the array structure and the method I should use?

Thanks a lot,

  • 写回答

1条回答 默认 最新

  • drpmazn9021 2016-04-04 11:28
    关注

    To do this you should register your menu as a factory and then you can pass parameters to the menu when instantiating the class inside the factory:

    Create a MenuFactory class:

    <?php
    
    namespace Acl\View\Helper\Navigation\Factory;
    
    /**
     * Factory to create menu
     */
    class MenuFactory implements FactoryInterface
    {
        /**
         * {@inheritDoc}
         * @return Menu
         */
        public function createService(ServiceLocatorInterface $serviceLocator)
        {
            $param = // get your param from servicelocator or create the parameter
            $menu = new Menu($param);
            return $menu;
        }
    }
    

    Now you register your menu as a factory instead of as an invokable:

    'navigation_helpers' => array(
        'factories' => array(
            'menu' => 'Acl\View\Helper\Navigation\Factory\MenuFactory',
        )
    ),
    

    If you want to read more then there is lot's of information on how to do this in ZF2 for example in this article here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了