2016-07-25 6 views
0

Ich habe folgende Controller:zend 2 einzuspritzen Controller: Abfangbare fatale Fehler: Argument 1 bis someController geben :: __ construct() muss eine Instanz ... keine gegeben

namespace Application\Controller; 

use Application\Model\Person; 
use Zend\Mvc\Controller\AbstractActionController; 
use Application\Model\PersonTable; 

class PersonController extends AbstractActionController 
{ 
    private $table; 

    public function __construct(PersonTable $table) 
    { 
     $this->table = $table; 
    } 
    // other methods 
} 

ich versucht zu tun hier Injektion durch Dokumentation folgende:

https://docs.zendframework.com/tutorials/getting-started/database-and-models/

In Modul/Anwendung/module.php ich habe diese Funktion:

public function getControllerConfig() 
    { 
     return [ 
      'factories' => [ 
       Controller\PersonController::class => function($container) { 
        return new Controller\PersonController(
         $container->get(Model\PersonTable::class) 
        ); 
       }, 
      ], 
     ]; 
    } 

In Modul/Application/config/module.config.php ich geändert dies, so wäre es mein Controller haben:

'controllers' => array(
     'invokables' => array(
      'Application\Controller\Index' => 'Application\Controller\IndexController', 
      'Application\Controller\Person' => 'Application\Controller\PersonController' 
     ), 
    ), 

ich die Controller-Methode von der Konsole aufrufen und den Fehler:

Behebbarer fataler Fehler: Argument 1 wurde an Application \ Controller \ PersonControl übergeben. ler :: __ construct() muss eine Instanz von Application \ Controller \ PersonTable sein, keine ne gegeben, in E: \ other \ dropbox \ Dropbox \ programavimas \ kodo aufgerufen pavydzziai \ htdoc s \ zend_2_staff_register \ vendor \ zendframework \ zendframework \ library \ Zend \ ServiceM anager \ AbstractPluginManager.php in Zeile 170 und definiert in E: \ andere \ dropbox \ Dro pbox \ programavimas \ kodo pavyzdziai \ htdocs \ zend_2_staff_register \ module \ Applicati auf \ src \ Application \ Controller \ PersonController.php in Zeile 12

Warum es nicht injiziert?

Antwort

1

Weil Ihr erklärt Ihr

'Application\Controller\Person' => 'Application\Controller\PersonController' 

als invokables Klasse, müssen Sie es in den Schlüssel setzen: Fabriken.

Sie tat es auf diese Weise:

public function getControllerConfig() 
    { 
     return [ 
      'factories' => [ 
       Controller\PersonController::class => function($container) { 
        return new Controller\PersonController(
         $container->get(Model\PersonTable::class) 
        ); 
       }, 
      ], 
     ]; 
    } 

Config in Zf2 werden zusammengeführt, so wird die PersonController durch ein invokables am Ende geladen und dieser Code unbrauchbar werden.

Ich rate Ihnen, einen Gegenstand Fabrik keine anonyme Funktion zu erstellen und diese erklärt:

'controllers' => array(
      'invokables' => array(
       'Application\Controller\Index' => 'Application\Controller\IndexController', 
      ), 
      'factories' => array(
       'Application\Controller\Person' => 'Application\Factory\PersonControllerFactory' 
      ), 
     ), 

Dann ist dieses Objekt PersonControllerFactory wird eine Instanz des Controllers mit Ihrem richtigen Abhängigkeiten zurück.

Hier ein Beispiel für ein Werk des Controllers (nicht das gleiche wie Service Fabriken): https://github.com/Grafikart/BlogMVC/blob/master/ZendFramework2/module/Blog/src/Blog/Factory/PostControllerFactory.php

Und seine Konfigurations Linie https://github.com/Grafikart/BlogMVC/blob/master/ZendFramework2/module/Blog/config/module.config.controllers.php#L8