Zend Framework2 How to access a variable in my Ser

2019-08-30 11:05发布

问题:

I am creating my first Zend Framework2 Project. My page offers the option to create adverts. I now want to offer the option to bookmark an advert. What needs to be done is that when you are on the page of the advert, the site will check if you are logged in and if you are it will check if the advert is already bookmarked and display the message otherwise it will offer you a link to "add the advert to your bookmarks". This link will also be shown when you are not logged in and if you click on in, you will then be re-directed to the Log-in Page.

Anyway, to achieve the above target, I have created a View Helper, which gets created by a BookmarkAdvertFactory. I have also created a BookmarkAdvertService, which checks the current Bookmark status. For that I need the UserId and the AdvertId.

In my module.php I inject the userEntity to be able to get the UserId in the BookmarkAdvertService, but now I am not sure how to access the AdvertId from the Service. Can someone give me an advice?

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));

  }          
}

回答1:

You need to pass the advertId to the view helper, which can pass it to the Service.