2016-06-19 30 views
5

Próbuję użyć kontrolera w Slim jednak zachować uzyskiwanie błądSlim problem sterownik: musi być instancją ContainerInterface, wystąpienie Slim \ opakowaniu danego

PHP za połów błąd krytyczny: Argument 1 przekazany do
TopPageController :: __ construct() musi być instancją ContainerInterface,
instancja Slim \ Container podane

Moja index.php

<?php 
use \Psr\Http\Message\ServerRequestInterface as Request; 
use \Psr\Http\Message\ResponseInterface as Response; 

require '../vendor/autoload.php'; 
require 'settings.php'; 

spl_autoload_register(function ($classname) { 
    require ("../classes/" . $classname . ".php"); 
}); 

$app = new \Slim\App(["settings" => $config]); 
$app->get('/', function (Request $request, Response $response) { 
    $response->getBody()->write("Welcome"); 
    return $response; 
}); 
$app->get('/method1', '\TopPageController:method1'); 
$app->run(); 
?> 

Mój TopPageController.php

<?php 
class TopPageController { 
    protected $ci; 
    //Constructor 
    public function __construct(ContainerInterface $ci) { 
     $this->ci = $ci; 
    } 

    public function method1($request, $response, $args) { 
     //your code 
     //to access items in the container... $this->ci->get(''); 
     $response->getBody()->write("Welcome1"); 
     return $response; 
    } 

    public function method2($request, $response, $args) { 
     //your code 
     //to access items in the container... $this->ci->get(''); 
     $response->getBody()->write("Welcome2"); 
     return $response; 
    } 

    public function method3($request, $response, $args) { 
     //your code 
     //to access items in the container... $this->ci->get(''); 
     $response->getBody()->write("Welcome3"); 
     return $response; 
    } 
} 
?> 

Dzięki. Używam Slim 3.

+1

Nie masz odpowiedniej przestrzeni nazw na swoim interfejsie ContainerInterface. Powinien to być 'Interop \ Container \ ContainerIterface' – geggleto

Odpowiedz

12

Twój kod wydaje się być oparty na dokumentacji Slim 3 pod adresem http://www.slimframework.com/docs/objects/router.html, która nie zawiera całego wymaganego kodu, aby uniknąć zgłaszania wyjątku.

Zasadniczo masz dwie opcje, aby działało.

Wariant 1:

Importowanie nazw w index.php, podobnie jak to jest robione dla Request i Response:

use \Interop\Container\ContainerInterface as ContainerInterface; 

Opcja 2:

Zmień konstruktor z TopPageController do:

public function __construct(Interop\Container\ContainerInterface $ci) { 
    $this->ci = $ci; 
} 

TL; DR

Powodem jest wyjątek, że klasa Slim\Container jest za pomocą interfejsu Interop\Container\ContainerInterface (patrz kod źródłowy):

use Interop\Container\ContainerInterface; 

Ponieważ Slim\Container jest rozszerzając Pimple\Container, wszystkie następujące powinny być poprawnymi (działającymi) deklaracjami typu dla metody kontrolera:

public function __construct(Pimple\Container $ci) { 
    $this->ci = $ci; 
} 

... albo nawet ...

public function __construct(ArrayAccess $ci) { 
    $this->ci = $ci; 
} 
0

Wystarczy wkleić poniżej kod w kontrolerze

use \Psr\Http\Message\ServerRequestInterface as Request; 
use \Psr\Http\Message\ResponseInterface as Response; 
use \Interop\Container\ContainerInterface as ContainerInterface; 

konstrukcja kontrolera powinno się wyglądać jak poniżej

public function __construct(ContainerInterface $container) { 
     parent::__construct($container); 

    } 

Myślę, że popełniacie błąd, udzielając namep spacja w kontrolerze dla ContainerInterface.