编程语言
首页 > 编程语言> > php – 将ZF2服务管理器转换为ZF3

php – 将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 );
    }
}

我应该使用什么代码而不是getServiceLocator()调用,因为ZF3不再支持这个代码?在Stack Overflow的其他地方,有人回复了另一个问题并建议使用createService函数,但这也已从ZF3中删除.

解决方法:

有几种不同的方法,但您已经使用了最常见的方法:通过构造函数传递依赖项.您目前正在为$spydusService类执行此操作,因此更改构造函数以接受两个表clases的参数,例如:

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);
            }
        )
    );
}

标签:php,zend-framework3,zend-framework2
来源: https://codeday.me/bug/20190828/1757167.html