У меня есть простое приложение ZF2, которое использует две таблицы и службу, и я пытаюсь преобразовать ее для запуска на ZF3. Я не могу решить, как обновить код сервис-менеджера. Вот пример одного из контроллеровПреобразование ZF2 Service Manager в 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);
}
}
Какой код следует использовать вместо (вызов getServiceLocator), так как это больше не поддерживается в ZF3? В другом месте на Stack Overflow кто-то ответил на другой вопрос и предложил использовать функцию createService, но это также было удалено из ZF3.
Рассматривали ли вы использование фабрики для сборки контроллера? На заводе (который имеет доступ к диспетчеру сервиса) вы можете загрузить свои «AuthorTable» и «BookTitleTable», а также ваш «SpydusServiceInterface» и построить с ними контроллер. – Thelonias
В классах контроллера ZF3 уже не известно ServiceLocator. поэтому вы должны делать то, что @Thelonias написал в своем комментарии; создать фабрику, создать свои службы внутри фабрики и ввести их в конструктор вашего класса контроллера. – Wilt