Zend Framework2 如何访问我的服务中的变量 [英] Zend Framework2 How to access a variable in my Service

查看:27
本文介绍了Zend Framework2 如何访问我的服务中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

在我的 module.php 中,我注入了 userEntity 以便能够在 BookmarkAdvertService 中获取 UserId,但现在我不确定如何从 Service 访问 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;
  }
}

查看\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 传递给视图助手,视图助手可以将其传递给 Service.

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

这篇关于Zend Framework2 如何访问我的服务中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆