두 테이블과 서비스를 사용하는 ZF2 응용 프로그램이 간단합니다. ZF3에서 실행되도록 변환하려고합니다. 서비스 관리자 코드를 업데이트하는 방법을 알아낼 수 없습니다. 다음은 컨트롤러이 더 이상 ZF3에서 지원 될 때 어떤 코드 나는 getServiceLocator() 호출 대신 사용해야합니다 ZF2 서비스 관리자를 ZF3으로 변환
<?php
namespace LibraryRest\Controller;
use Zend\Mvc\Controller;
use Library\Service\SpydusServiceInterface;
use Library\Service\SpydusService;
use Library\Model\BookTitle;
use Library\Model\BookTitleTable;
use Library\Model\Author;
use Library\Model\AuthorTable;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class SearchRestController extends AbstractRestfulController {
protected $bookTitleTable;
public function getBookTitleTable() {
if (! $this->bookTitleTable) {
$this->bookTitleTable = $this->getServiceLocator()->get ('BookTitleTable');
}
return $this->bookTitleTable;
}
protected $authorTable;
public function getAuthorTable() {
if (! $this->authorTable) {
$sm = $this->getServiceLocator();
$this->authorTable = $sm->get ('AuthorTable');
}
return $this->authorTable;
}
protected $spydusService;
public function __construct(SpydusServiceInterface $spydusService) {
$this->spydusService = $spydusService;
}
public function getList($action, $first, $last) {
if ($action == 'search') {
return $this->searchAction ($first, $last);
}
}
public function searchAction() {
$message = array();
$results = array();
$first = urldecode ($this->params()->fromRoute ('first'));
$last = urldecode ($this->params()->fromRoute ('last'));
if ($this->getAuthorTable()->getAuthorId ($first, $last) === false) {
$results [0] = array ('count' => 0, 'message' => 'This author not found in database', 'first' => $first, 'last' => $last, 'title' => '', 'titleCode' => '', 'link' => '', 'authorId' => '');
} else {
$authorId = $this->getAuthorTable()->getAuthorId ($first, $last)->author_id;
$books = $this->spydusService->searchBooks ($first, $last);
$count = count ($books);
foreach ($books as $foundTitle) {
if ($foundTitle->getMessage() == 'Nothing found') {
$results [0] = array ('count' => 0, 'message' => 'Nothing found by library search', 'first' => $first, 'last' => $last, 'title' => '', 'titleCode' => '', 'link' => '', 'authorId' => '');
break;
}
$searchBooks = $this->getBookTitleTable()->getId ($foundTitle->getTitle(), $authorId);
if ($searchBooks->count() == 0) {
$addUrl = "http://newlib.rvw.dyndns.org/library/search/add/" . $authorId . '/' . $foundTitle->getTitle();
$results [] = array ('count' => $count, 'message' => $foundTitle->getMessage(), 'title' => $foundTitle->getTitle(), 'titleCoded' => $foundTitle->getTitleCoded(), 'first' => $foundTitle->getAuthorFirst(), 'last' => $foundTitle->getAuthorLast(), 'link' => $foundTitle->getLink(), 'authorId' => $authorId, 'addUrl' => $addUrl);
}
}
}
if (count ($results) == 0) {
$results [0] = array ('count' => 0, 'message' => 'Nothing found by library search', 'first' => $first, 'last' => $last, 'title' => '', 'titleCode' => '', 'link' => '', 'authorId' => '');
}
return new JsonModel ($results);
}
}
중 하나의 예? Stack Overflow에서 다른 사람이 다른 질문에 대답하고 createService 함수를 사용하여 제안했지만 ZF3에서도 삭제되었습니다.
컨트롤러를 구축하기 위해 공장을 사용해 보셨습니까? (서비스 매니저에 접근 할 수있는) 팩토리에서는'AuthorTable'과'BookTitleTable'과'SpydusServiceInterface'를 적재하고 컨트롤러를 구성 할 수 있습니다. – Thelonias
ZF3 컨트롤러 클래스에서는 더 이상 ServiceLocator를 인식하지 않습니다. 그래서 @Thelonias가 그의 코멘트에 쓴대로해야한다. 팩토리를 생성하고 팩토리에서 서비스를 생성 한 다음 컨트롤러 클래스의 생성자에 삽입하십시오. – Wilt