1

コントローラーに Factory を実装しようとしています:

class NumberControllerFactory implements FactoryInterface{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
    return new NumberController($container->get(Bar::class));
}

public function createService(ServiceLocatorInterface $services)
{
    return $this($services, NumberController::class);
}

}

エラーが発生しました:

Fatal error: Declaration of Number\Factory\NumberControllerFactory::__invoke() must be compatible with Zend\ServiceManager\Factory\FactoryInterface::__invoke(Interop\Container\ContainerInterface $container, $requestedName, array $options = NULL) in C:\xampp\htdocs\MyProject\module\Number\src\Number\Factory\NumberControllerFactory.php on line 10

Zend 3でサービスマネージャーがコントローラーから削除されたため、モデルをコントローラーに注入したいので、これが必要です。

https://framework.zend.com/manual/2.4/en/ref/installation.htmlに記載されているスケルトンを使用しました

composer.json は次のとおりです。

    "require": {
    "php": "^5.6 || ^7.0",
    "zendframework/zend-component-installer": "^1.0 || ^0.3 || ^1.0.0-dev@dev",
    "zendframework/zend-mvc": "^3.0.1",
    "zfcampus/zf-development-mode": "^3.0"
},

この問題がわかりません。たとえば、次のような多くのチュートリアルを読みました。

https://zendframework.github.io/zend-servicemanager/migration/

助けていただけますか?

現在、このメソッドは Zend\ServiceManager\Factory\FactoryInterface::__invoke と互換性があると思います

4

2 に答える 2

3

モデルをコントローラーに注入するには、以下のように module.config.php で構成しながらファクトリー クラスを作成する必要があります。

 'controllers' => [
    'factories' => [
        Controller\AlbumController::class => Factory\AlbumControllerFactory::class,
    ],
 ],

ここで、AlbumController は Album モジュールのコントローラ クラスです。その後、module\Album\src\Factory 内に AlbumControllerFactory クラスを作成する必要があります。このクラスでは、以下のコードを記述する必要があります。

  namespace Album\Factory;

  use Album\Controller\AlbumController;
  use Album\Model\AlbumTable;
  use Interop\Container\ContainerInterface;
  use Zend\ServiceManager\Factory\FactoryInterface;


  class AlbumControllerFactory implements FactoryInterface
  {
     public function __invoke(ContainerInterface $container,      $requestedName, array $options = null)
     {
      return new AlbumController($container->get(AlbumTable::class));
     }
  }

コントローラー クラス (AlbumController) 内に以下のコードを記述する必要があります。

   public function __construct(AlbumTable $album) {
     $this->table = $album;
   }

このようにして、モデル クラスをコントローラー クラスに挿入できます。

于 2016-11-25T18:05:23.050 に答える
0

ありがとうアザール。

私の問題は、私が使用したときの理由でした:

         'factories' => array(
        \Number\Controller\NumberController::class => \Number\Factory\NumberControllerFactory::class
     )

それはうまくいきませんでした、404がありました...私は使用しなければなりませんでした:

'Number\Controller\Number' => \Number\Factory\NumberControllerFactory::class

ドキュメントでは、完全なクラス名 ::class.

なぜそれが機能しないのか誰かが知っていますか?

于 2016-11-26T12:02:43.827 に答える