gpt4 book ai didi

php - ZF2 在工厂获取参数

转载 作者:行者123 更新时间:2023-12-04 19:28:27 26 4
gpt4 key购买 nike

我有一个动态类别导航。在导航工厂中,我想从 route 获取一个参数。我该怎么做?

在我看来

<?php echo $this->navigation('CategoryNavigation')->menu()->setUlClass('list-group'); ?>

在我的 module.php 中:

    public function getServiceConfig()
{
return array(
'factories' => array(
'CategoryNavigation' => 'Application\Navigation\CategoryNavigation',
)
);
}

这是我的导航工厂。我需要从定义了 {{ store slug }} (2x) 的路径中获取 slug。

    <?php

namespace Application\Navigation;

use Zend\ServiceManager\ServiceLocator;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Navigation\Service\DefaultNavigationFactory;

class CategoryNavigation extends DefaultNavigationFactory
{
protected $sl;

protected function getPages(ServiceLocatorInterface $serviceLocator)
{
if (null === $this->pages) {

$em = $serviceLocator->get('Doctrine\ORM\EntityManager');
$storerepository = $em->getRepository('ApplicationShared\Entity\Store');
$store = $storerepository->findOneBy(array('slug' => '{{ store slug }}'));

$fetchMenu = $store->getCategories();

$configuration['navigation'][$this->getName()] = $this->buildNavigation($fetchMenu);

if (!isset($configuration['navigation'])) {
throw new Exception\InvalidArgumentException('Could not find navigation configuration key');
}
if (!isset($configuration['navigation'][$this->getName()])) {
throw new Exception\InvalidArgumentException(sprintf(
'Failed to find a navigation container by the name "%s"',
$this->getName()
));
}

$application = $serviceLocator->get('Application');
$routeMatch = $application->getMvcEvent()->getRouteMatch();
$router = $application->getMvcEvent()->getRouter();
$pages = $this->getPagesFromConfig($configuration['navigation'][$this->getName()]);
//
$this->pages = $this->injectComponents($pages, $routeMatch, $router);
}
return $this->pages;
}

protected function buildNavigation($elements, $parentid = null){
foreach ($elements as $index => $element) {
if (!$element->getParent()) {
$branch[$element->getCategoryName()] = $this->navigationItem($element);
}
}

return $branch;
}

protected function navigationItem($element){

$branch['label'] = $element->getCategoryName();
$branch['route'] = 'store/type/catalog';
$branch['params'] = array('slug' => '{{ store slug }}','category' => $element->getSlug());
if(count($element->getChildren()) > 0){
foreach($element->getChildren() as $element){
$branch['pages'][] = $this->navigationItem($element);
}
}

return $branch;
}
}

或者是否有更好的方法来实现这一目标?

最佳答案

找了几个小时,最后在栈上问,现在半个小时就找到了答案。

我只需要在我的工厂做广告

$router = $serviceLocator->get('router');
$request = $serviceLocator->get('request');

// Get the router match
$routerMatch = $router->match($request);
$this->slug = $routerMatch->getParam("slug");

关于php - ZF2 在工厂获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24571194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com