2016-07-15 1 views
3

私は2つのテーブルとサービスを使用する単純な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); 
    } 
} 

の1の例ですか?スタックオーバーフローのどこか他の人が別の質問に答えて、createService関数を使って提案しましたが、これもZF3から削除されました。

+4

コントローラをビルドする際に工場を使用したことはありますか? (サービスマネージャにアクセスできる)工場では、あなたの 'AuthorTable'と' BookTitleTable'とあなたの 'SpydusServiceInterface'を読み込み、あなたのコントローラを構築することができます。 – Thelonias

+2

ZF3コントローラクラスでは、もはやServiceLocatorに対応していません。 @Theloniasが彼のコメントに書いたように、なぜあなたはすべきなのですか?ファクトリを作成し、ファクトリ内でサービスを作成し、コントローラクラスのコンストラクタに挿入します。 – Wilt

答えて

2

コントローラを作成するためのファクトリを作成することになります。この新しいクラスはFactoryInterfaceを実装します。 __invoke関数では、$コンテナインスタンスを使用して、コントローラのすべての依存関係を取得し、それらをコンストラクタの引数として渡したり、コンストラクタインスタンスに設定したりします。次に、その関数からコントローラインスタンスを返します。

これをサポートするには、コントローラーをフィールドで更新する必要があります。また、あなたの設定に工場を登録する必要があります。

+0

$ containerインスタンスに関するあなたの提案と詳細をありがとう。 Zend \ ServiceManager \ Factory \ FactoryInterface Zend \ ServiceManager \ Factory \ FactoryInterface これは少し苦労していますが、最終的にはこのコントローラのファクトリを作成することができました。今度は残りのコントローラへ! – user3017691

+0

@ user3017691あなたの問題が解決された場合は、解決策として解決策をマークしてください。 – Thelonias

3

いくつかのアプローチがありますが、最も一般的な方法を使用しています。つまり、依存関係をコンストラクタに渡します。あなたが現在お使いの$spydusServiceクラスのためにこれをやって、これも2つのテーブル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); 
      } 
     ) 
    ); 
} 
+1

が素早く質問しました。コントローラで使用するモデルが10個以上ある場合の例として、dbモデルを使用する引数の数値を増やすのは良い考えですか? – tradebel123

+0

@ tradebel123同じ質問、解決策は、DBアダプタをコントローラ引数に追加することですか? –

関連する問題