首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >zf3 zend导航助手

zf3 zend导航助手
EN

Stack Overflow用户
提问于 2016-09-08 11:45:13
回答 1查看 2.5K关注 0票数 3

我正在尝试从ZF3中的一个容器实现zend导航。通过这个快速入门教程,我成功地创建了导航,直接在config/autoload/global.phpconfig/module.config.php文件中介绍导航:

https://docs.zendframework.com/zend-navigation/quick-start/

但是现在我需要让它与帮助程序一起工作,以便使用“示例中使用的导航设置”部分,从控制器中允许导航修改:

https://docs.zendframework.com/zend-navigation/helpers/intro/

这是我的Module.php

代码语言:javascript
复制
namespace Application;

use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\View\HelperPluginManager;

class Module implements ConfigProviderInterface
{
    public function getViewHelperConfig()
    {
        return [
            'factories' => [
                // This will overwrite the native navigation helper
                'navigation' => function(HelperPluginManager $pm) {
                    // Get an instance of the proxy helper
                    $navigation = $pm->get('Zend\View\Helper\Navigation');
                    // Return the new navigation helper instance
                    return $navigation;
                }
            ]
        ];
    }

    public function getControllerConfig()
    {
        return [
            'factories' => [
                        $this->getViewHelperConfig()
                    );
                },
            ],
        ];
    }
}

这是我的IndexController.php

代码语言:javascript
复制
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Navigation\Navigation;
use Zend\Navigation\Page\AbstractPage;

class IndexController extends AbstractActionController
{

    private $navigationHelper;

    public function __construct(
        $navigationHelper    
    ){
        $this->navigationHelper = $navigationHelper;
    }

    public function indexAction()
    {

        $container = new Navigation();
        $container->addPage(AbstractPage::factory([
            'uri' => 'http://www.example.com/',
        ]));

        $this->navigationHelper->plugin('navigation')->setContainer($container);

        return new ViewModel([
        ]);
    }


}

但是,我得到了以下错误:

代码语言:javascript
复制
Fatal error: Call to a member function plugin() on array in /var/www/html/zf3/module/Application/src/Controller/IndexController.php on line 50

在本教程中,他们使用以下语句:

代码语言:javascript
复制
// Store the container in the proxy helper:
$view->plugin('navigation')->setContainer($container);

// ...or simply:
$view->navigation($container);

但我不知道这个$view是什么,所以我猜想是我的$navigation来自于我的Module.php。问题在于,因为它是一个数组,所以它会抛出错误。问题如下:

  • 我做错了什么?
  • 本教程的$view从何而来?
  • 我应该从我的Module.php那里传递什么才能让它正常工作?

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2017-07-04 09:28:10

添加到module.config.php中

代码语言:javascript
复制
'service_manager' => [  
   'factories' => [
      Service\NavManager::class => Service\Factory\NavManagerFactory::class,      
    ],
],

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

在服务目录中创建工厂:

代码语言:javascript
复制
namespace Application\Service\Factory;

use Interop\Container\ContainerInterface;
use Application\Service\NavManager;


class NavManagerFactory {
/**
 * This method creates the NavManager service and returns its instance. 
 */
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
    $authService = $container->get(\Zend\Authentication\AuthenticationService::class);        
    $viewHelperManager = $container->get('ViewHelperManager');
    $urlHelper = $viewHelperManager->get('url');

    return new NavManager($authService, $urlHelper);
   }
}

创建NavManager文件:

代码语言:javascript
复制
namespace Application\Service;

class NavManager {
/**
 * Auth service.
 * @var Zend\Authentication\Authentication
 */
private $authService;

/**
 * Url view helper.
 * @var Zend\View\Helper\Url
 */
private $urlHelper;

/**
 * Constructs the service.
 */
public function __construct($authService, $urlHelper) {
    $this->authService = $authService;
    $this->urlHelper = $urlHelper;
}

/**
 * Menu render based on user role
 * 
 * @return array
 */
public function getMenuItems() {
    $navItem = array();
    $url = $this->urlHelper;
    $items = [];

    $items[] = [
        'label' => 'Dashboard',
        'icon' => 'dashboard',
        'link'  => $url('home'),
        'route' => ['home'],
    ];

    $items[] = [
        'label' => 'About Us',
        'icon' => 'business',
        'link' => $url('about', ['action'=>'index']),
        'route' => ['about'],
    ];

    $items[] = [
        'label' => 'Service',
        'icon' => 'service',
        'link' => $url('service', ['action'=>'index']),
        'route' => ['service'],
    ];

    return $items;
}

创建帮工工厂

代码语言:javascript
复制
namespace Application\View\Helper\Factory;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Application\View\Helper\Menu;
use Application\Service\NavManager;

/**
* This is the factory for Menu view helper. Its purpose is to 
 instantiate the helper and init menu items. */
class MenuFactory implements FactoryInterface {

public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
    $navManager = $container->get(NavManager::class);        
    // Get menu items.
    $items = $navManager->getMenuItems();        
    // Instantiate the helper.
    return new Menu($items);
  }

}

创建帮手:

代码语言:javascript
复制
namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;

/**
* This view helper class displays a menu bar.
*/
class Menu extends AbstractHelper {
/**
 * Menu items array.
 * @var array 
 */
protected $items = [];

/**
 * Active item's ID.
 * @var string  
 */
protected $activeItemId = '';

/**
 * Constructor.
 * @param array $items Menu items.
 */
public function __construct($items=[]) {
    $this->items = $items;
}

/**
 * Sets menu items.
 * @param array $items Menu items.
 */
public function setItems($items) {
    $this->items = $items;
}

/**
 * Sets ID of the active items.
 * @param string $activeItemId
 */
public function setActiveItemId($activeItemId) {
    $this->activeItemId = $activeItemId;
}

/**
 * Renders the menu.
 * @return string HTML code of the menu.
 */
public function render() {
    if (count($this->items)==0) {
        return ''; // Do nothing if there are no items.
    }

    // Render items
    foreach ($this->items as $item) {
        $result .= $this->renderItem($item);
    }

    return $result;

}
protected function renderItem($item) {        
   // here you can integrate with HTML
   return $item;
}
} 

在添加上述代码之后,只需在布局文件中添加以下代码:

代码语言:javascript
复制
echo $this->mainMenu()->render();
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39390067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档