Slim-controller-Problem : muss eine Instanz von ContainerInterface, Instanz Slim\\Container gegeben

Ich versuche, mit controller in der Slim jedoch immer der Fehler

PHP Abfangbaren fatal error: Argument 1 übergeben

TopPageController::__construct() muss eine Instanz von ContainerInterface,

Instanz Slim\Container gegeben

Meine 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();
?>

Meine 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;
   }
}
?>

Dank. Ich bin mit Slim 3.

  • Sie haben nicht die richtige namespace auf Ihre ContainerInterface. Es sollte Interop\Container\ContainerIterface
InformationsquelleAutor lost111in | 2016-06-19
Schreibe einen Kommentar