The problem with the thin controller: there must be an instance of ContainerInterface, an instance of Slim \\ Container

I'm trying to use the controller in Slim, but still getting an error

PHP Catchable fatal error: argument 1 passed to TopPageController :: __ construct () must be an instance of ContainerInterface,
Slim \ Container instance specified

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

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

Thanks. I am using Slim 3.

+5
source share
2 answers

Your code is apparently based on Slim 3 documentation at http://www.slimframework.com/docs/objects/router.html , which does not contain all the necessary code to throw an exception thrown.

Basically you have two options to make it work.

Option 1:

Import the namespace into index.php , as is done for Request and Response :

 use \Interop\Container\ContainerInterface as ContainerInterface; 

Option 2:

Change the constructor of TopPageController to:

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

TL DR

The reason the exception is thrown is because the Slim\Container class uses the Interop\Container\ContainerInterface interface (see source code):

 use Interop\Container\ContainerInterface; 

Since Slim\Container extends Pimple\Container , everything should be a valid (working) type declaration for your controller method:

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

... or even...

 public function __construct(ArrayAccess $ci) { $this->ci = $ci; } 
+12
source

Just paste the below code into the controller

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

The controller design should look below

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

I think you are mistaken in providing a name in the controller for ContainerInterface.

0
source

All Articles