2017-03-15 7 views
0

\ module \ Album \ src \ Album \ FactoryフォルダにAlbumControllerFactory.phpを作成しました。controlleralbumcontroller(別名:Controller AlbumController)を作成しようとしたときに、このインスタンスタイプに対して無効なファクトリが登録されました

namespace Album\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use Album\Model\Album; 
use Album\Form\AlbumForm; 

class AlbumController extends AbstractActionController 
{ 

protected $_albumTable; 

public function __construct(AlbumTable $albumTable) 
{ 
    $this->_albumTable = $albumTable; 
} 


public function indexAction() 
{ 
    return new ViewModel(array(
     'albums' => $this->getAlbumTable()->fetchAll(), 
    )); 
} 

}

use Interop\Container\ContainerInterface; 
use Zend\ServiceManager\FactoryInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 
use Album\Controller\AlbumController; 

class AlbumControllerFactory implements FactoryInterface 
{ 
/** 
* 
* @param ContainerInterface $container 
* @param string $requestedName 
* @param null|array $options 
* @return AlbumController 
*/ 
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
{ 
    $class = $requestedName ? $requestedName : AlbumController::class; 
    $albumTable = $container->get('Album\Model\AlbumTable'); // get service from service manager 
    $controller = new $class($albumTable); 

    return $controller; 

} 
/** 
* Provided for backwards compatibility; proxies to __invoke(). 
* 
* @param ContainerInterface|ServiceLocatorInterface $container 
* @return AlbumController 
*/ 
public function createService(ServiceLocatorInterface $container) 
{ 
    return $this($container, AlbumController::class); 
} 
} 

上記そして、私のコントローラで私は私の工場を取得するコードを以下したエラーを示します

そして、次のようなとして私module.config.phpに私は、コントローラを設定している:

return array(
'controllers' => array(
'factories' => array(
    Controller\AlbumController::class => Factory\Controller\AlbumControllerFactory::class, 
), 

)を、

ので、私はそれとして私のモジュールの作業罰金を作るために、順序、この問題を解決するにはどうすればよいですファクトリを作成する前にうまくいきましたが、廃止予定のメッセージが表示されていました。私が何をしたのか、そしてこれを解決する方法を教えてください。

+0

メインFactoryディレクトリ内のすべてのあなたの工場を維持しますが、彼らのために正しい名前空間を設定してくださいとmodule.config.phpファイルにすることができますが、 '持っていますか'module.config.php'ファイルの先頭にあるAlbum \ Controller'と' Album \ Factory'を使用しますか? – SzymonM

+0

まだ同じエラー:@SzymonM – Shoaib

+0

'AlbumControllerFactory'クラスの名前空間とは何ですか? – SzymonM

答えて

0

あなたの問題は名前空間についてです:

あなたが言った:

I have created AlbumControllerFactory.php in folder \module\Album\src\Album\Factory

しかし、あなたは

Controller\AlbumController::class => Factory\Controller\AlbumControllerFactory::class, 

Factory\Controller\AlbumControllerFactory呼び出しが正しい完全修飾クラス名(FQCN)

ではありません設定ファイル

にこれを入れてください

設定ファイルで宣言された名前空間はアルバムであると仮定します。ここでは

Controller\AlbumController::class => Factory\AlbumControllerFactory::class 
+0

私はこれを試みたが、同じエラーがまだあります。 @Hooli – Shoaib

+0

名前空間の問題があると言っているので、@SzymonMで与えられた完全な例はあなたの問題を解決するはずです。 – Hooli

1

は、Zend 2/3にファクトリを使用する方法の完全な例です。 module.config.phpファイルに名前空間がありませんが、名前空間とディレクトリを比較してください。

module 
    - Album 
    - config 
     module.config.php 
    - src 
     - Controller 
      AlbumController.php 
     - Factory 
     - Controller 
      AlbumControllerFactory.php 
    - Module.php 

工場

namespace Album\Factory\Controller 

use Interop\Container\ContainerInterface; 
use Zend\ServiceManager\FactoryInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 
use Album\Controller\AlbumController; 

class AlbumControllerFactory implements FactoryInterface 
{ 
/** 
* 
* @param ContainerInterface $container 
* @param string $requestedName 
* @param null|array $options 
* @return AlbumController 
*/ 
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
{ 
    $class = $requestedName ? $requestedName : AlbumController::class; 
    $albumTable = $container->get('Album\Model\AlbumTable'); // get service from service manager 
    $controller = new $class($albumTable); 

    return $controller; 

} 
/** 
* Provided for backwards compatibility; proxies to __invoke(). 
* 
* @param ContainerInterface|ServiceLocatorInterface $container 
* @return AlbumController 
*/ 
public function createService(ServiceLocatorInterface $container) 
{ 
    return $this($container, AlbumController::class); 
} 
} 

コントローラ

namespace Album\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use Album\Model\Album; 
use Album\Form\AlbumForm; 

class AlbumController extends AbstractActionController 
{ 

protected $_albumTable; 

public function __construct(AlbumTable $albumTable) 
{ 
    $this->_albumTable = $albumTable; 
} 


public function indexAction() 
{ 
    return new ViewModel(array(
     'albums' => $this->getAlbumTable()->fetchAll(), 
    )); 
} 

} 

モジュール:

は、次のディレクトリ構造を持っている必要があります。 config.phpの

namespace Album; 

use Album\Controller; 
use Album\Factory; 

return array(
'controllers' => array(
    'factories' => array(
     Controller\AlbumController::class => Factory\Controller\AlbumControllerFactory::class, 
    ), 
), 

私は、分離の工場を持ちたいが、あなたは

+0

より完全な回答 – Hooli

関連する問題