Zend Framework2 如何访问我的服务中的变量

Zend Framework2 How to access a variable in my Service

我正在创建我的第一个 Zend Framework2 项目。我的页面提供了创建广告的选项。我现在想提供为广告添加书签的选项。需要做的是,当您在广告页面上时,该网站将检查您是否已登录,如果您已登录,它将检查广告是否已加入书签并显示消息,否则它将为您提供 link 到 "add the advert to your bookmarks"。此 link 也会在您未登录时显示,如果您点击进入,您将被重定向到登录页面。

无论如何,为了实现上述目标,我创建了一个 View Helper,它由 BookmarkAdvertFactory 创建。我还创建了一个 BookmarkAdvertService,它检查当前书签状态。为此,我需要 UserId 和 AdvertId。

在我的 module.php 中,我注入了 userEntity 以便能够在 BookmarkAdvertService 中获取 UserId,但现在我不确定如何从该服务访问 AdvertId。有人可以给我建议吗?

module.php

'Advert\BookmarkLink' => function ($sl) {
                        $entityManager = $sl->get('doctrine.entitymanager.orm_default');
                      //$advertId = '395';
                        $myService = new Service\BookmarkAdvertService($advertId);
                        $myService->setEntityManager($entityManager);

                        $repository = $entityManager->getRepository('Advert\Entity\Bookmark');
                        $myService->setRepository($repository);

                        $authService = $sl->get('zfcuser_auth_service');
                        $userEntity = $authService->getIdentity();
                        $myService->setUserEntity($userEntity);

                        return $myService;
                    },  

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
              'displayBookmarkLink' => 'Advert\View\Helper\BookmarkAdvertFactory')
}    

Service\BookmarkAdvertService.php

namespace Advert\Service;

use Advert\Entity\Bookmark as BookmarkEntity;
use Advert\Repository\BookmarkRepository;
use Doctrine\ORM\EntityManager;
use Zend\Debug\Debug;


class BookmarkAdvertService 
{
  protected $location;
  protected $userEntity;

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

  }

  public function setEntityManager(EntityManager $entityManager)
  {
    $this->entityManager = $entityManager;
  }

  public function setRepository(BookmarkRepository $repository) {
    $this->repository = $repository;
  }

  public function setUserEntity($userEntity)
  {
    $this->userEntity = $userEntity;
  }

  public function getUserEntity()
  {
    return $this->userEntity;
  }


  public function checkAdvertBookmarkStatus()
  {
    $userId = $this->getUserEntity()->getId();
    $bookmarkStatus = $this->repository->getBookmarkStatus($this->advertId,$userId);

    return $bookmarkStatus;
  }

}

View\Helper\BookmarkAdvertFactory.php

namespace Advert\View\Helper;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class BookmarkAdvertFactory implements FactoryInterface
{

  public function createService(ServiceLocatorInterface $serviceLocator)
  {
    $locator = $serviceLocator->getServiceLocator();
    $service = $locator->get('Advert\BookmarkLink');
    $helper  = new BookmarkAdvert($service);

    return $helper;
  }
}

View\Helper\BookmarkAdvert.php

namespace Advert\View\Helper;
use Zend\View\Helper\AbstractHelper;

class BookmarkAdvert extends AbstractHelper
{

  protected $service;

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

  public function __invoke()
  {
    $bookmarkStatus = $this->service->checkAdvertBookmarkStatus();
    return $this->render($bookmarkStatus);
  }

  public function render($bookmarkStatus)
  {
    return $this->getView()->render('advert/partial/bookmarklink', array('bookmarkStatus' => $bookmarkStatus));

  }          
}

您需要将 advertId 传递给视图助手,视图助手可以将其传递给服务。