我有一個簡單的ZF2應用程序,它使用兩個表和一個服務,我試圖將它轉換爲在ZF3上運行。我無法弄清楚如何更新服務管理器代碼。這裏的控制器將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);
}
}
,因爲這是不再ZF3支持哪些代碼我應該使用getServiceLocator()調用,而不是一個例子嗎?在堆棧溢出的其他地方,有人回答了另一個問題,並建議使用createService函數,但這也已從ZF3中刪除。
您是否考慮過使用工廠來構建控制器?在工廠(可以訪問服務管理器)中,你可以加載你的'AuthorTable'和'BookTitleTable'以及你的'SpydusServiceInterface'並用它們構建你的控制器。 – Thelonias
在ZF3控制器類中不再可以識別ServiceLocator。這就是爲什麼你應該像@ Thelonias在他的評論中寫的那樣去做。創建一個工廠,在工廠內創建你的服務並將它們注入到你的控制器類的構造函數中。 – Wilt