我有一个简单的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中删除。

最佳答案

有两种不同的方法,但您已经在使用最常见的方法:通过构造函数传入依赖项。您当前正在为$spydusService类执行此操作,因此请更改构造函数以同时接受两个表类的参数,如下所示:

class SearchRestController extends AbstractRestfulController
{
    protected $bookTitleTable;

    protected $authorTable;

    protected $spydusService;

    public function __construct(SpydusServiceInterface $spydusService, $bookTitleTable, $authorTable)
    {
        $this->spydusService = $spydusService;
        $this->bookTitleTable = $bookTitleTable;
        $this->authorTable = $authorTable;
    }

    [etc.]
}

然后,在某个地方已经有了SearchRestController的工厂(它可能是Module.php类中的一个闭包,或者是一个独立的工厂类)。您需要修改此项以传递额外的参数:
public function getControllerConfig()
{
    return array(
        'factories' => array(
            'SearchRestController' => function ($sm) {
                $spydusService = $sm->get('SpydusService'); // this part you already have
                $bookTitleTable = $sm->get('BookTitleTable');
                $authorTable = $sm->get('AuthorTable');

                return new SearchRestController($spydusService, $bookTitleTable, $authorTable);
            }
        )
    );
}

08-04 11:40